android的一些提示框 以及activity之間資料的傳遞 .

來源:互聯網
上載者:User

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 還有很多複雜的用法,有確定和取消的對話方塊

new AlertDialog.Builder(this)
         .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() {
        public void onClick(DialogInterface dialog, int whichButton) {
         //取消按鈕事件
         }
         })
         .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:
        
           return true;
       case 3:
         
           return true;
        }
       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 intent = new Intent();
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"));
        }
}

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 還有很多複雜的用法,有確定和取消的對話方塊

new AlertDialog.Builder(this)
         .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() {
        public void onClick(DialogInterface dialog, int whichButton) {
         //取消按鈕事件
         }
         })
         .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:
        
           return true;
       case 3:
         
           return true;
        }
       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 intent = new Intent();
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"));
        }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.