安卓總結


2017.3.24

xml布局

1.我們學的是LinearLayout線性布局,布局前先確定好界面的整體排版,豎着的用android:orientation="vertical",橫的用android:orientation="horizontal",利用android:padding="xxdp"來美化邊框,使得界面不挨邊

2.使用界面控件的時候,有開就得有關,例如 xxxx表示屬性,在最后加上/>;像 結束時要用。

跑馬燈效果

    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:scrollbars="horizontal"
    android:singleLine="true" --這些為創建跑馬燈關鍵語句

.java

1.如何編寫.java事件
在.java下編寫時,我們先得定義變量,例如private EditText extizhong;
再建立相關的事件響應extizhong=(EditText)findViewBy
Id(R.id.tizhong);(extizhong為我們定義的變量名,tizhong為xml界面中我們定義的一個EditText組件的id,id和控件一定要保持一致,不然無法運行或者閃退)
最后添加相應的監聽事件,來實現自己想要實現的指令,例如:

btnjisuan.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//實現指令的方法代碼
}
});

2.運用相關的方法
在protected void最后點擊右鍵

選中Generate...

選中Override Methods...

最后在app目錄下找

常用控件及其用法

TextView

1.常用屬性:
2.常用於:(1)用於顯示標題或者文字,例如在.xml下輸入 android:text="你想輸入的文字"(2)用於輸出你想要的文字,例如在.java下輸入txbiaoti.setText("你想輸出的文字");

EditText

1.常用屬性
2.常用於:用戶可以輸入數據,從而傳給android應用

Button

1.常用於:確定,取消,退出等按鈕
2.按鈕的單擊事件
(1)在xml布局文件中設置Button的屬性,即android:onClick="myclick",然后在該布局文件對應的Acitivity中實現該方法。例如:

public void myclick(View view){
//方法
}

(2)使用setOnClickListener添加監聽事件,例如:

btnjisuan.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//實現指令的方法代碼
}
});

ImageView

1.常用屬性
2.常用於顯示圖片。常用的有2種方法,一種是在xml界面android:src="@drawable/圖片名",一種是在.java中ivtupian.setImageResource(R.drawable.圖片名)

RadioButton

使用多個RadioButton而確保單選時,可以用RadioGroup,例如:

        <RadioButton

            android:id="@+id/meihua"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="@dimen/activity_vertical_margin"
            android:checked="true"
            android:text="梅花 "
            android:textSize="20sp" />

        <RadioButton

            android:id="@+id/shinanhua"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="石楠花   "
            android:textSize="20sp" />

        <RadioButton

            android:id="@+id/xiangyahua"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="象牙花"
            android:textSize="20sp" />
    </RadioGroup>

    <RadioGroup

        android:id="@+id/rg2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <RadioButton

            android:id="@+id/xiuqiuhua"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="綉球花"
            android:textSize="20sp" />

        <RadioButton

            android:id="@+id/shilanhua"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="石蘭花   "
            android:textSize="20sp" />

        <RadioButton

            android:id="@+id/mudanhua"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="牡丹花"
            android:textSize="20sp" />
    </RadioGroup>

而2個或2個以上RadioGroup組時保證單選時,則要在.java中編寫,可以通過監聽RadioGroup來實現單選,例如:
//在定義變量和建立相關的事件響應基礎上

rgrg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
            if (rbmeihua.isChecked()) {
                ivtupian.setImageResource(R.drawable.meihua);
                rgrg2.clearCheck();
            }
            if (rbshinanhua.isChecked()) {
                ivtupian.setImageResource(R.drawable.shinanhua);
                rgrg2.clearCheck();
            }
            if (rbxiangyahua.isChecked()) {
                ivtupian.setImageResource(R.drawable.iangyahua);
                rgrg2.clearCheck();
            }
        }
    });

rgrg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
            if (rbxiuqiuhua.isChecked()) {
                ivtupian.setImageResource(R.drawable.xiuqiuhua);
                rgrg.clearCheck();
            }
            if (rbshilanhua.isChecked()) {
                ivtupian.setImageResource(R.drawable.shilanhua);
                rgrg.clearCheck();
            }
            if (rbmudanhua.isChecked()) {
                ivtupian.setImageResource(R.drawable.mudanhua);
                rgrg.clearCheck();
            }
        }
    });

CheckBox

用於多選或者只有一個選項的情況。建立方法如下:

它的監聽事件:

cbjava.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                                     boolean isChecked) {
            // TODO Auto-generated method stub
           //方法
        }
    });

Android常見事件

onTouchEvent事件

常用方法:

1.MotionEvent.getAction()
(1)屏幕按下時,MotionEvent.getAction()值為MotionEvent.ACTION_DOWN
(2)屏幕抬起時,MotionEvent.getAction()值為MotionEvent.ACTION_UP
(3)屏幕滑動時,MotionEvent.getAction()值為MotionEvent.ACTION_MOVE
2.MOtionEvent.getX()||MOtionEvent.getY()
例如:

public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
        float x = event.getX();
        float y = event.getY();
        String pos = "x坐標" + x + "y坐標" + y;//獲取鼠標的x,y坐標值
        Toast.makeText(this, pos, Toast.LENGTH_LONG).show();//通過Toast文本方法實現輸出坐標值
    }
    return super.onTouchEvent(event);
}

鍵盤事件

鍵盤事件主要用於對鍵盤事件的監聽,根據用戶輸入內容對鍵盤事件進行跟蹤。
例如監聽手機上的退出:

private long exitTime = 0;

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {//判斷手機上退出鍵
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
        return true;
    }
    return super.onKeyDown(keyCode, event);
}//參考於百度http://www.cnblogs.com/jxgxy/archive/2012/08/23/2653404.html

菜單與消息通知

菜單

1.在res上面右鍵->New->Android resource directory

2.點擊之后,出現下圖Resource type選擇menu

3.menu上面右擊,如下圖選擇:

4.file name隨便取名字

進入mune.xml界面中后,一種是直接寫代碼
還有一種如下如圖

拖動左上角MenuItem,然后在屬性見面編輯

5.最后在.java下輸入重載方法即可

@Override
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.mune,menu);
    return true;
}

對話框

利用重載方法onOptionsItemSelected,例如:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.item_about:
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("關於");//為對話框設置標題
            builder.setIcon(R.mipmap.ic_launcher);//為對話框設置圖標
            builder.setMessage("版本為1.0");//為對話框設置內容
            builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //把自己關掉
                    dialogInterface.dismiss();
                    //整個關掉
                    MainActivity.this.finish();
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            });
            builder.setNeutralButton("退出", null);
            //創建並顯示
            builder.create().show();
            break;
    }
    return false;
}

結果如下:

錯誤總結

1.閃退情況

解決方案:
定義變量:private RadioButton rbmeihua;
建立相關事件響應:rbmeihua = (RadioButton) findViewById(R.id.meihua);
要注意xml布局中id名相對應的控件和相對應的變量,千萬不能搞錯.

2.

xml布局中所有的控件有頭必有尾,例如 xxxx表示屬性,在最后加上/>;像 結束時要用。

3.

重載方法最好在

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}

的后面點擊右鍵找,不然不同的位置有不同的重載方法,容易找不到。

4.

跑馬燈效果必須要在字超過一行的情況下,不然用不了。

5.

想不出啥了,有問題的一起分享解決。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM