国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android AlertDialog對(duì)話(huà)框詳解及實(shí)例

2019-12-12 04:12:39
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Android  AlertDialog

關(guān)系圖如下:

Android主要提供四種對(duì)話(huà)框:

1:AlertDialog:功能最豐富,實(shí)際應(yīng)用最廣的對(duì)話(huà)框。
2:ProgressDialog:進(jìn)度條對(duì)話(huà)框
3:DatePickerDialog:日期選擇器對(duì)話(huà)框
4:TimePickerDialog:時(shí)間選擇器對(duì)話(huà)框

創(chuàng)建一個(gè)對(duì)話(huà)框的步驟:

AlertDialog.Builder builder = new AlertDialog.Builder(this)        // 1:設(shè)置對(duì)話(huà)框標(biāo)題        .setTitle("自定義列表項(xiàng)對(duì)話(huà)框")            // 2:設(shè)置圖標(biāo)        .setIcon(R.drawable.tools)            // 3:設(shè)置內(nèi)容        .setMessage("對(duì)話(huà)框的測(cè)試內(nèi)容/n第二行內(nèi)容");    // 為AlertDialog.Builder添加“確定”按鈕    setPositiveButton(builder);    // 為AlertDialog.Builder添加“取消”按鈕    setNegativeButton(builder)        .create()        .show();

代碼區(qū):

main.xml代碼區(qū):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:gravity="center_horizontal"><!-- 顯示一個(gè)普通的文本編輯框組件 --><EditText   android:id="@+id/show"  android:layout_width="match_parent"   android:layout_height="wrap_content"   android:editable="false"/><!-- 定義一個(gè)普通的按鈕組件 --><Button  android:layout_width="match_parent"   android:layout_height="wrap_content"   android:text="簡(jiǎn)單對(duì)話(huà)框"  android:onClick="simple"  /><!-- 定義一個(gè)普通的按鈕組件 --><Button  android:layout_width="match_parent"   android:layout_height="wrap_content"   android:text="簡(jiǎn)單列表項(xiàng)對(duì)話(huà)框"  android:onClick="simpleList"  /> <!-- 定義一個(gè)普通的按鈕組件 --><Button  android:layout_width="match_parent"   android:layout_height="wrap_content"   android:text="單選列表項(xiàng)對(duì)話(huà)框"  android:onClick="singleChoice"  /> <!-- 定義一個(gè)普通的按鈕組件 --><Button  android:layout_width="match_parent"   android:layout_height="wrap_content"   android:text="多選列表項(xiàng)對(duì)話(huà)框"  android:onClick="multiChoice"  /> <!-- 定義一個(gè)普通的按鈕組件 --><Button  android:layout_width="match_parent"   android:layout_height="wrap_content"   android:text="自定義列表項(xiàng)對(duì)話(huà)框"  android:onClick="customList"  /> <!-- 定義一個(gè)普通的按鈕組件 --><Button  android:layout_width="match_parent"   android:layout_height="wrap_content"   android:text="自定義View對(duì)話(huà)框"  android:onClick="customView"  />         </LinearLayout>

Activity代碼區(qū):

public class MainActivity extends Activity {  TextView show;  String[] items = new String[] {      "aserbao", "Android",      " Java",      "IOS" };  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    show = (TextView) findViewById(R.id.show);  }  public void simple(View source)  {    AlertDialog.Builder builder = new AlertDialog.Builder(this)      // 設(shè)置對(duì)話(huà)框標(biāo)題      .setTitle("簡(jiǎn)單對(duì)話(huà)框")      // 設(shè)置圖標(biāo)      .setIcon(R.drawable.tools)      .setMessage("對(duì)話(huà)框的測(cè)試內(nèi)容/n第二行內(nèi)容");    // 為AlertDialog.Builder添加“確定”按鈕    setPositiveButton(builder);    // 為AlertDialog.Builder添加“取消”按鈕    setNegativeButton(builder)      .create()      .show();  }  public void simpleList(View source)  {    AlertDialog.Builder builder = new AlertDialog.Builder(this)        // 設(shè)置對(duì)話(huà)框標(biāo)題        .setTitle("簡(jiǎn)單列表對(duì)話(huà)框")            // 設(shè)置圖標(biāo)        .setIcon(R.drawable.tools)            // 設(shè)置簡(jiǎn)單的列表項(xiàng)內(nèi)容        .setItems(items, new OnClickListener()        {          @Override          public void onClick(DialogInterface dialog, int which)          {            show.setText("你選中了《" + items[which] + "》");          }        });    // 為AlertDialog.Builder添加“確定”按鈕    setPositiveButton(builder);    // 為AlertDialog.Builder添加“取消”按鈕    setNegativeButton(builder)        .create()        .show();  }  public void singleChoice(View source)  {    AlertDialog.Builder builder = new AlertDialog.Builder(this)        // 設(shè)置對(duì)話(huà)框標(biāo)題        .setTitle("單選列表項(xiàng)對(duì)話(huà)框")            // 設(shè)置圖標(biāo)        .setIcon(R.drawable.tools)            // 設(shè)置單選列表項(xiàng),默認(rèn)選中第二項(xiàng)(索引為1)        .setSingleChoiceItems(items, 1, new OnClickListener()        {          @Override          public void onClick(DialogInterface dialog, int which)          {            show.setText("你選中了《" + items[which] + "》");          }        });      // 為AlertDialog.Builder添加“確定”按鈕      setPositiveButton(builder);      // 為AlertDialog.Builder添加“取消”按鈕      setNegativeButton(builder)        .create()        .show();  }  public void multiChoice(View source)  {    AlertDialog.Builder builder = new AlertDialog.Builder(this)        // 設(shè)置對(duì)話(huà)框標(biāo)題        .setTitle("多選列表項(xiàng)對(duì)話(huà)框")            // 設(shè)置圖標(biāo)        .setIcon(R.drawable.tools)            // 設(shè)置多選列表項(xiàng),設(shè)置勾選第2項(xiàng)、第4項(xiàng)        .setMultiChoiceItems(items            , new boolean[]{false , true ,false ,true}, null);    // 為AlertDialog.Builder添加“確定”按鈕    setPositiveButton(builder);    // 為AlertDialog.Builder添加“取消”按鈕    setNegativeButton(builder)        .create()        .show();  }  public void customList(View source)  {    AlertDialog.Builder builder = new AlertDialog.Builder(this)        // 設(shè)置對(duì)話(huà)框標(biāo)題        .setTitle("自定義列表項(xiàng)對(duì)話(huà)框")            // 設(shè)置圖標(biāo)        .setIcon(R.drawable.tools)            // 設(shè)置自定義列表項(xiàng)        .setAdapter(new ArrayAdapter<String>(this            , R.layout.array_item            , items), null);    // 為AlertDialog.Builder添加“確定”按鈕    setPositiveButton(builder);    // 為AlertDialog.Builder添加“取消”按鈕    setNegativeButton(builder)        .create()        .show();  }  public void customView(View source)  {    // 裝載app/src/main/res/layout/login.xml界面布局文件    TableLayout loginForm = (TableLayout)getLayoutInflater()        .inflate( R.layout.login, null);    new AlertDialog.Builder(this)        // 設(shè)置對(duì)話(huà)框的圖標(biāo)        .setIcon(R.drawable.tools)        // 設(shè)置對(duì)話(huà)框的標(biāo)題        .setTitle("自定義View對(duì)話(huà)框")        // 設(shè)置對(duì)話(huà)框顯示的View對(duì)象        .setView(loginForm)        // 為對(duì)話(huà)框設(shè)置一個(gè)“確定”按鈕        .setPositiveButton("登錄", new OnClickListener() {          @Override          public void onClick(DialogInterface dialog,            int which) {            // 此處可執(zhí)行登錄處理          }        })        // 為對(duì)話(huà)框設(shè)置一個(gè)“取消”按鈕        .setNegativeButton("取消", new OnClickListener()        {          @Override          public void onClick(DialogInterface dialog,                    int which)          {            // 取消登錄,不做任何事情          }        })        // 創(chuàng)建并顯示對(duì)話(huà)框        .create()        .show();  }  private AlertDialog.Builder setPositiveButton(      AlertDialog.Builder builder)  {    // 調(diào)用setPositiveButton方法添加“確定”按鈕    return builder.setPositiveButton("確定", new OnClickListener()    {      @Override      public void onClick(DialogInterface dialog, int which)      {        show.setText("單擊了【確定】按鈕!");      }    });  }  private AlertDialog.Builder setNegativeButton(      AlertDialog.Builder builder)  {    // 調(diào)用setNegativeButton方法添加“取消”按鈕    return builder.setNegativeButton("取消", new OnClickListener()    {      @Override      public void onClick(DialogInterface dialog, int which)      {        show.setText("單擊了【取消】按鈕!");      }    });  }}

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 锡林浩特市| 永丰县| 陆良县| 谷城县| 湖口县| 哈密市| 海林市| 阜南县| 布拖县| 云阳县| 金昌市| 德令哈市| 襄垣县| 东乡族自治县| 行唐县| 桐城市| 米林县| 桐乡市| 武山县| 镇雄县| 通辽市| 泸溪县| 灵台县| 抚顺市| 黎川县| 阿坝县| 波密县| 正定县| 伊吾县| 江北区| 永福县| 平远县| 蒙自县| 宣汉县| 慈利县| 五原县| 集安市| 东明县| 资兴市| 汝州市| 邵武市|