<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                AlertDialog生成的對話框可分為4個區域:圖標區,標題區,內容區,按鈕區 結構如圖: ![](https://box.kancloud.cn/2016-03-10_56e0d9af34ce4.jpg) AlertDialog對話框的使用: 1,**創建AlertDialog.Builder對象** 2,調用Builder對象的**setTitle()設置標題,setIcon設置圖標** 3,調用Builder對象的相關方法**設置內容**,AlertDialog提供如下六中設置指定對話框的內容: setMessage();設置簡單文本框的內容 setItems();設置簡單列表的內容,數組 setSingleChoiceItems();;設置單選列表的內容,內容參數可以是數組Cursor,ListAdapter setMultiChoiceItems();設置多選列表項的內容,內容參數可以是數組,Cursor setAdapter();設置內容,內容是ListAdapter,常用的BaseAdapter,SimpleAdapter,ArrayAdapter setView();設置內容,參數是自定義的View 4,調用Builder對象的**setPositiveButton()和 setNegativeButton()設置按鈕和監聽器** 5,調用Builder對象的**create()方法**創建AlertDialog對象,再調用AlertDialog對象的**show()方法**顯示對話框 總之:調用Builder對象設置圖標,標題,內容,按鈕,在create(),show() 代碼模板: ~~~ new AlertDialog.Builder(this) // 設置對話框標題 .setTitle("簡單對話框") // 設置圖標 .setIcon(R.drawable.tools) //設置內容,可代替 .setMessage("對話框的測試內容\n第二行內容").setPositiveButton("確定",listener).setNegativeButton("取消",listener).create().show(); ~~~ acitivity_main.xml ~~~ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView" /> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TableRow android:id="@+id/tableRow1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="simple" android:text="簡單文本" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="simpleList" android:text="數組列表" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="singleChoice" android:text="單選列表" /> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" > <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="multiChoice" android:text="多選列表" /> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="customList" android:text="自定義列表" /> <Button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="customView" android:text="自定義View" /> </TableRow> </TableLayout> </LinearLayout> ~~~ ![](https://box.kancloud.cn/2016-03-10_56e0d9af45e91.jpg) MainActivity.java ~~~ package com.hust.alertdialogtest; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.TableLayout; import android.widget.TextView; /*AlertDialog對話框的使用: * AlertDialog對話框有四個區域:圖標區,標題區,內容區,按鈕區 * 1,創建AlertDialog.Builder對象 * 2,調用Builder對象的setTitle()設置標題,setIcon設置圖標 * 3,調用Builder對象的相關方法設置內容 * setMessage();設置簡單文本框的內容 * setItems();設置簡單列表的內容,數組 * setSingleChoiceItems();;設置單選列表的內容,內容參數可以是數組,Cursor,ListAdapter * setMultiChoiceItems();設置多選列表項的內容,內容參數可以是數組,Cursor * setAdapter();設置內容,內容是ListAdapter,常用的BaseAdapter,SimpleAdapter,ArrayAdapter * setView();設置內容,參數是自定義的View * 4,調用Builder對象的setPositiveButton()和 setNegativeButton()設置按鈕和監聽器 * 5,調用Builder對象的create()方法創建AlertDialog對象,再調用AlertDialog對象的show()方法顯示對話框 * * * 總之:調用Builder對象設置圖標,標題,內容,按鈕,在create(),show() * */ public class MainActivity extends Activity { TextView show; String[] items=new String[]{ "湖北省","福建省","貴州省","四川省" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); show=(TextView) findViewById(R.id.textView1); } /*簡單文本對話框:builder.setMessage()設置內容*/ public void simple(View v){ AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("簡單對話框"); builder.setIcon(R.drawable.tools); builder.setMessage("這是簡單文本對話框\n第二行內容"); //為builder對象添加確定按鈕,不過這里嵌套了一個函數 setPositiveButton(builder); //為builder對象添加取消按鈕 builder=setNegativeButton(builder); //builder創建對話框對象AlertDialog AlertDialog simpledialog=builder.create(); simpledialog.show(); } /*簡單列表對話框(數組對話框):builder.setItems()設置內容*/ public void simpleList(View v){ AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("數組列表對話框"); builder.setIcon(R.drawable.tools); builder.setItems(items, new OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub show.setText("你單擊了 "+items[which]); } }); setPositiveButton(builder); builder=setNegativeButton(builder); AlertDialog simplelistdialog=builder.create(); simplelistdialog.show(); } /*單選列表項對話框:builder.setSingleChoiceItems()設置內容,內容參數可以是數組,Cursor,ListAdapter*/ public void singleChoice(View v){ AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("單選列表項對話框"); builder.setIcon(R.drawable.tools); //設置單選列表項,默認選中第二項 builder.setSingleChoiceItems(items,1, new OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub show.setText("你單擊了 "+items[which]); } }); setPositiveButton(builder); builder=setNegativeButton(builder); AlertDialog simplechoicedialog=builder.create(); simplechoicedialog.show(); } /* * *多選列表項對話框: builder.setMultiChoiceItems()設置內容,參數可以是數組,Cursor數據庫返回結果集 * * AlertDialog.Builder.setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, OnMultiChoiceClickListener listener)*/ public void multiChoice(View v){ AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("多選列表項對話框"); builder.setIcon(R.drawable.tools); //設置單多選列表項,默認選中第二項,第四項 builder.setMultiChoiceItems(items,new boolean[]{false,true,false,true},new OnMultiChoiceClickListener(){ @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { // TODO Auto-generated method stub //添加處理方法 } }); setPositiveButton(builder); builder=setNegativeButton(builder); AlertDialog simplechoicedialog=builder.create(); simplechoicedialog.show(); } /*自定義Adapter對話框:builder.setAdapter設置內容,內容是ListAdapter,常用的BaseAdapter,SimpleAdapter,ArrayAdapter*/ public void customList(View v){ AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("自定義Adapter對話框"); builder.setIcon(R.drawable.tools); builder.setAdapter(new ArrayAdapter<String>(this,R.layout.array_item,items), new OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub show.setText("你單擊了 "+items[which]); } }); //為builder對象添加確定按鈕,不過這里嵌套了一個函數 setPositiveButton(builder); //為builder對象添加取消按鈕 builder=setNegativeButton(builder); //builder創建對話框對象AlertDialog AlertDialog adapterdialog=builder.create(); adapterdialog.show(); } /*自定義View對話框:builder.setView()設置View*/ public void customView(View v){ //獲取xml布局文件對象 TableLayout loginform=(TableLayout) getLayoutInflater().inflate(R.layout.login, null); AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("自定義View對話框"); builder.setIcon(R.drawable.tools); //設置對話框顯示的View組件 builder.setView(loginform); //為builder對象添加確定按鈕,不過這里嵌套了一個函數 setPositiveButton(builder); //為builder對象添加取消按鈕 builder=setNegativeButton(builder); //builder創建對話框對象AlertDialog AlertDialog viewdialog=builder.create(); viewdialog.show(); } //返回對象是原來的Builder對象 private AlertDialog.Builder setPositiveButton(AlertDialog.Builder builder) { // TODO Auto-generated method stub return builder.setPositiveButton("確定", new OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub show.setText("單擊了【確定】按鈕!"); } }); } //返回對象是Builder對象 private AlertDialog.Builder setNegativeButton(AlertDialog.Builder builder) { // TODO Auto-generated method stub return builder.setNegativeButton("取消", new OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub show.setText("單擊了【取消】按鈕!"); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } ~~~ ![](https://box.kancloud.cn/2016-03-10_56e0d9af5ad4a.jpg) ![](https://box.kancloud.cn/2016-03-10_56e0d9af6f20f.jpg) ![](https://box.kancloud.cn/2016-03-10_56e0d9af913ea.jpg) ![](https://box.kancloud.cn/2016-03-10_56e0d9afab17c.jpg) ![](https://box.kancloud.cn/2016-03-10_56e0d9afca5c9.jpg) ![](https://box.kancloud.cn/2016-03-10_56e0d9afe773c.jpg)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看