Android新手入門2016(12)--基於Layout檔案的AlertDialog

來源:互聯網
上載者:User

Android新手入門2016(12)--基於Layout檔案的AlertDialog

上一章學習了AlertDialog,後來發現還有基於Layout檔案的AlertDialog。可以自己排好位置,相對複雜一點。

先看看效果

圖中已經按布局檔案排好位置了。

新加了個Layout檔案:dialog_layout.xml

 

              

這個布局檔案只加入了一個輸入框

然後看看代碼:直接在上一章的基礎上改的代碼,好像有點多。

package com.fable.helloworld; import android.app.Activity;  import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.os.Bundle;   import android.view.LayoutInflater;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.EditText;import android.widget.GridView;import android.widget.SimpleAdapter; import java.util.*;public class HelloWorldActivity extends Activity {      @Override      public void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);        setContentView(R.layout.activity_hello_world);  //設定主布局檔案        GridView gridview = (GridView) findViewById(R.id.gridview);                  //創造資料來源        ArrayList> images = new ArrayList>();          for(int i=1;i<10;i++)          {          String imageName = "";        switch(i)        {        case 1:         imageName = "AlertDialog";//普通的AlertDialog        break;        case 2:        imageName = "AlertDialog2";//基於布局的AlertDialog        break;        default:        imageName = "app"+String.valueOf(i);        }        HashMap map = new HashMap();          map.put("ItemImage", R.drawable.ic_launcher);//添加映像資源的ID,標識符,值        map.put("ItemText", imageName);//按序號做ItemText,標識符,值          images.add(map);          }          //把資料傳入適配器,轉換成布局需要的資料        SimpleAdapter simpleAdapter = new SimpleAdapter(this, //上下文為當前Activity        images,//資料來源           R.layout.my_list_item,//每一項的布局的XML實現            new String[] {"ItemImage","ItemText"},//動態數組與ImageItem對應的子項         new int[] {R.id.ItemImage,R.id.ItemText});  //ImageItem的XML檔案裡面的一個ImageView,兩個TextView ID          //添加並且顯示          gridview.setAdapter(simpleAdapter);                   //添加訊息處理          gridview.setOnItemClickListener(new ItemClickListener());      }             //當AdapterView被單擊(觸控螢幕或者鍵盤),則返回的Item單擊事件      class  ItemClickListener implements OnItemClickListener      {      public void onItemClick(AdapterView arg0,//父視圖                                      View arg1,//當前視圖                                    int arg2,//點擊的位置                                    long arg3//id                                    ) {           HashMap item = (HashMap) arg0.getItemAtPosition(arg2); //擷取點擊的item    //setTitle((String)item.get("ItemText")); //這個只是把標題改一改,    String itemStr = (String)item.get("ItemText");    if(itemStr.equals("AlertDialog")){    showDialog(HelloWorldActivity.this, itemStr);      }    else if (itemStr.equals("AlertDialog2")){showDialogLayout(HelloWorldActivity.this);}        }//=========================AlertDialog====================================================    private void showDialog(Context context, String itemStr) {         //AlertAialog的建構函式是protected的,只能通過Builder函數來構建一個新的對象    AlertDialog.Builder builder = new AlertDialog.Builder(context);                  builder.setIcon(R.drawable.ic_launcher);  //設定表徵圖                builder.setTitle("我是標題");  //設定標題                builder.setMessage("這裡是內容啊啊啊啊!!!");//設定內容                  builder.setPositiveButton("Button1",  //確認按鈕                    new DialogInterface.OnClickListener() {//為了方便,不顯式聲明一個類了                          public void onClick(DialogInterface dialog, int whichButton) {                          setTitle("點擊了對話方塊上的Button1");                          }                      });                  builder.setNeutralButton("Button2",  //中性按鈕                        new DialogInterface.OnClickListener() {                              public void onClick(DialogInterface dialog, int whichButton) {                                  setTitle("點擊了對話方塊上的Button2");                              }                          });                  builder.setNegativeButton("Button3",  //否認按鈕                        new DialogInterface.OnClickListener() {                              public void onClick(DialogInterface dialog, int whichButton) {                                  setTitle("點擊了對話方塊上的Button3");                              }                          });                  builder.show();  //顯式這個對話方塊      }//===================基於Layout的AlertDialog================================================        private void showDialogLayout(Context context) {          //LayoutInflater的作用是用來動態載入Layout檔案的             LayoutInflater inflater = LayoutInflater.from(context);              final View textEntryView = inflater.inflate( R.layout.dialog_layout, null);//動態載入Layout檔案              final EditText edtInput=(EditText)textEntryView.findViewById(R.id.edtInput);//載入之後可以找到其中的控制項了            final AlertDialog.Builder builder = new AlertDialog.Builder(context);              builder.setCancelable(false);              builder.setIcon(R.drawable.ic_launcher);              builder.setTitle("Title");              builder.setView(textEntryView);              builder.setPositiveButton("確認",  //這裡又手動加入了按鈕,可以看出,可以混著用的                    new DialogInterface.OnClickListener() {                          public void onClick(DialogInterface dialog, int whichButton) {                              setTitle(edtInput.getText());                          }                      });              builder.setNegativeButton("取消",                      new DialogInterface.OnClickListener() {                          public void onClick(DialogInterface dialog, int whichButton) {                              setTitle("");                          }                      });              builder.show();          }              } }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.