天天看點

Android 的一些提示框

1.在測試時,如何實作一個提示

可以使用

Toast.makeText(this, "這是一個提示", Toast.LENGTH_SHORT).show();

//從資源檔案string.xml 裡面取提示資訊

Toast.makeText(this, getString(R.string.welcome), Toast.LENGTH_SHORT).show();

這個提示會幾秒鐘後消失

2.可以使用AlertDialog.Builder 才産生一個提示框.

   例如像messagebox那樣的

   new AlertDialog.Builder(this)

     .setTitle("Android 提示")

     .setMessage("這是一個提示,請确定")

     .show();

帶一個确定的對話框

new AlertDialog.Builder(this)

          .setMessage("這是第二個提示")

          .setPositiveButton("确定",

                         new DialogInterface.OnClickListener(){

                                 public void onClick(DialogInterface dialoginterface, int i){

                                     //按鈕事件

                                  }

                          })

          .show();

AlertDialog.Builder 還有很多複雜的用法,有确定和取消的對話框

         .setTitle("提示")

         .setMessage("确定退出?")

         .setIcon(R.drawable.quit)

         .setPositiveButton("确定", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int whichButton) {

         setResult(RESULT_OK);//确定按鈕事件

         finish();

         }

         })

         .setNegativeButton("取消", new DialogInterface.OnClickListener() {

         //取消按鈕事件

         .show();

3.menu 的用法.

public static final int ITEM_1_ID = Menu.FIRST;

public static final int ITEM_2_ID = Menu.FIRST + 1;

public static final int ITEM_3_ID = Menu.FIRST + 2;

public boolean onCreateOptionsMenu(Menu menu) {

         super.onCreateOptionsMenu(menu);

//不帶圖示的menu

         menu.add(0, ITEM_1_ID, 0, "item-1");       

//帶圖示的menu

         menu.add(0, ITEM_2_ID, 1, "item-2").setIcon(R.drawable.editbills2);

         menu.add(0, ITEM_3_ID, 2, "item-3").setIcon(R.drawable.billsum1);

        return true;

}

public boolean onOptionsItemSelected(MenuItem item){

       switch (item.getItemId()) {

       case 1:

            Toast.makeText(this, "menu1",Toast.LENGTH_SHORT).show();            

           return true;

       case 2:

       case 3:

        }

       return false;

     }

4.Activity 的切換

     2個Activity 的切換,沒有資料傳遞

//從A到B

Intent intent = new Intent();

                 intent.setClass(A.this, B.class);

                 startActivity(intent);

2個Activity 之間傳遞資料

    相關的幾個函數

     startActivityForResult

    public final void setResult(int resultCode, String data)

    回調函數

    protected void onActivityResult(int requestCode, int resultCode, Intent data)

    例如A到B,從B得到資料  

//A到B

static final int RG_REQUEST = 0;

intent.setClass(A.this, B.class);

startActivityForResult(intent,RG_REQUEST);

//在B中處理

Bundle bundle = new Bundle();

bundle.putString("DataKey", edittext.getText().toString());//給bundle 寫入資料

Intent mIntent = new Intent();

mIntent.putExtras(bundle);

setResult(RESULT_OK, mIntent);

finish();

//最後在A的回調函數裡面接收資料

if (requestCode == RG_REQUEST) {

     if (resultCode == RESULT_CANCELED)

           setTitle("Canceled...");

     else if(resultCode == RESULT_OK) {

          setTitle((String)data.getCharSequenceExtra("DataKey"));

繼續閱讀