您好,登錄后才能下訂單哦!
本篇文章為大家展示了如何在Android應(yīng)用中使用AlertDialog實(shí)現(xiàn)一個(gè)對(duì)話(huà)框,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
一、確定對(duì)話(huà)框
AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("確認(rèn)對(duì)話(huà)框"); builder.setIcon(R.drawable.icon_72); builder.setMessage("這里是對(duì)話(huà)框內(nèi)容"); builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub Toast.makeText(AlertDialog_text.this, "點(diǎn)擊了確定按鈕", 1).show(); } }); AlertDialog dialog = builder.create(); dialog.show(); //顯示、
二、普通列表
final String[] items = new String[]{"語(yǔ)文","數(shù)學(xué)","英語(yǔ)","物理","化學(xué)"}; //列表項(xiàng) Builder alertdialog = new AlertDialog.Builder(this); alertdialog.setTitle("你喜歡的課程").setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(AlertDialog_lianxi.this, items[which], Toast.LENGTH_SHORT).show(); } }); alertdialog.create().show(); //創(chuàng)建顯示列表
三、單選列表
final String[] items_fruit = new String[]{"蘋(píng)果","香蕉","橘子","西瓜","梨"}; Builder alerdialog = new AlertDialog.Builder(this); //設(shè)置列表標(biāo)題 alerdialog.setTitle("你喜歡的水果"); //設(shè)置單選列表 alerdialog.setSingleChoiceItems(items_fruit, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(AlertDialog_lianxi.this, items_fruit[which], Toast.LENGTH_SHORT).show(); } }); //設(shè)置取消按鈕并且設(shè)置響應(yīng)事件 alerdialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub //取消按鈕響應(yīng)事件 } }); //添加確定按鈕 并且設(shè)置響應(yīng)事件 alerdialog.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub //確定按鈕響應(yīng)事件 } }); alerdialog.create().show();//創(chuàng)建顯示列表
四、多選列表
final String[] items_fruit1 = new String[]{"蘋(píng)果","香蕉","橘子","西瓜","梨"}; //設(shè)置項(xiàng) final boolean[] items_fruit_selected = new boolean[]{true,false,false,false,false}; Builder alerdialog1 = new AlertDialog.Builder(this); //設(shè)置列表標(biāo)題 alerdialog1.setTitle("你喜歡的水果"); //設(shè)置多選列表 alerdialog1.setMultiChoiceItems(items_fruit1, items_fruit_selected, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { // TODO Auto-generated method stub items_fruit_selected[which] = isChecked; } }); //設(shè)置取消按鈕并且設(shè)置響應(yīng)事件 alerdialog1.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub //取消按鈕響應(yīng)事件 } }); //添加確定按鈕 并且設(shè)置響應(yīng)事件,將選擇的項(xiàng)顯示 alerdialog1.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub //確定按鈕響應(yīng)事件 StringBuilder stringBuilder = new StringBuilder(); for(int i=0;i<items_fruit_selected.length;i++) { if(items_fruit_selected[i] == true) { stringBuilder.append(items_fruit1[i]+"、"); } } Toast.makeText(AlertDialog_lianxi.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show(); } }); alerdialog1.create().show();//創(chuàng)建顯示列表
5、自定義布局對(duì)話(huà)框
對(duì)話(huà)框布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false" android:text="自定義對(duì)話(huà)框"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="這里是自定義對(duì)話(huà)框的內(nèi)容" android:textSize="20dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="確定" /> </LinearLayout> layout_dialog
package com.example.demo1; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button btn_openDialog; private View view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_openDialog = (Button) findViewById(R.id.id_submit); btn_openDialog.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_dialog, null); AlertDialog dialog = new AlertDialog.Builder(MainActivity.this) .setTitle("主題") .setIcon(R.drawable.ic_launcher) .setView(view) .setPositiveButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }) .create(); dialog.show(); } }); } } MainActivity.class
上述內(nèi)容就是如何在Android應(yīng)用中使用AlertDialog實(shí)現(xiàn)一個(gè)對(duì)話(huà)框,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。