Android 对话框 AlertDialog
openkk
13年前
<p>本讲介绍一下Android基本组件:对话框AlertDialog。</p> <p>API:</p> <p></p> <table class="ke-zeroborder1" border="0"> <tbody> <tr> <td>java.lang.Object</td> </tr> <tr> <td> ↳</td> <td>android.app.AlertDialog.Builder</td> </tr> </tbody> </table> <p></p> <p>使用AlertDialog.Builder创建对话框需要了解以下几个方法:</p> <ul> <li>setTitle :为对话框设置标题</li> <li>setIcon :为对话框设置图标</li> <li>setMessage:为对话框设置内容</li> <li>setView : 给对话框设置自定义样式</li> <li>setItems :设置对话框要显示的一个list,一般用于显示几个命令时。</li> <li>setMultiChoiceItems :用来设置对话框显示一系列的复选框。</li> <li>setNeutralButton :</li> <li>setPositiveButton :给对话框添加"Yes"按钮</li> <li>setNegativeButton :对话框添加"No"按钮</li> <li>create : 创建对话框</li> <li>show :显示对话框</li> </ul> <p>下面我们来看一下最简单对话框。</p> <p><img alt="" src="https://simg.open-open.com/show/5d65194f315da4d203141c48a3046027.jpg" width="292" height="165" /></p> <p>这个对话框只是简单的显示内容,使用默认图标,没有按钮,不多说,贴代码:</p> <pre class="brush:java; toolbar: true; auto-links: false;">new AlertDialog.Builder(MyActivity.this).setTitle("提示标题").setMessage("这是提示内容").show(); </pre> <p></p> <p>(MyActivity 是类名,请换成自己的!!)</p> <p>下面我们为这个对话框加个按钮,效果:</p> <p><img alt="" src="https://simg.open-open.com/show/8079b2b4a8e1be32126aebb35b91032f.jpg" width="280" height="184" /></p> <p>代码:</p> <pre class="brush:java; toolbar: true; auto-links: false;"> new AlertDialog.Builder(MyActivity.this) .setTitle("这是标题") .setMessage("这是提示内容") .setPositiveButton("确定", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialoginterface, int i){ //按钮事件 Toast.makeText(Lesson_01_Pic.this, "确定",Toast.LENGTH_LONG).show(); } }).show(); </pre> <p>添加按钮时,需要同时为该按钮指定监听器。</p> <p>下面,我们修改一个图标,添加两个按钮,同时显示多个选项,先看下效果:</p> <p><img alt="" src="https://simg.open-open.com/show/f1f06974bf95dec9cdbfbf7af429eb5a.jpg" width="309" height="266" /></p> <p>代码:</p> <pre class="brush:java; toolbar: true; auto-links: false;">package com.yfz; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MyActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button)findViewById(R.id.b01); button.setText("对话框"); button.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { //选项数组 String[] choices={"非死book","推ter"}; //Check判断数组,与选项对应 boolean[] chsBool = {true,false}; //包含多个选项及复选框的对话框 AlertDialog dialog = new AlertDialog.Builder(Lesson_02_Dia.this) .setIcon(android.R.drawable.btn_star_big_on) .setTitle("调查") .setMultiChoiceItems(choices, chsBool, multiClick) .setPositiveButton("Yes", onclick) .setNegativeButton("No", onclick).create(); dialog.show(); } }); } /** * 对话框复选框事件监听器 */ OnMultiChoiceClickListener multiClick = new OnMultiChoiceClickListener(){ @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(Lesson_02_Dia.this, "第"+(which+1)+"项,选中结果:"+isChecked,Toast.LENGTH_SHORT).show(); } }; /** * 对话框按钮点击事件监听器 */ OnClickListener onclick = new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case Dialog.BUTTON_NEGATIVE: Toast.makeText(Lesson_02_Dia.this, "No..", Toast.LENGTH_LONG).show(); break; case Dialog.BUTTON_NEUTRAL: Toast.makeText(Lesson_02_Dia.this, "I don't know.", Toast.LENGTH_LONG).show(); break; case Dialog.BUTTON_POSITIVE: Toast.makeText(Lesson_02_Dia.this, "Yes!!", Toast.LENGTH_LONG).show(); break; } } }; }</pre> <p></p> <p>说明已经写在注释中了。</p> <p>下面再介绍一种比较常用的式样,如图:</p> <p><img alt="" src="https://simg.open-open.com/show/d781a794e520e48861897e66ca978056.jpg" width="303" height="266" /></p> <p>代码:</p> <pre class="brush:java; toolbar: true; auto-links: false;"> @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button)findViewById(R.id.b01); button.setText("对话框"); button.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { //选项数组 String[] choices={"新浪微博","校内","街旁"}; //包含多个选项的对话框 AlertDialog dialog = new AlertDialog.Builder(Lesson_02_Dia.this) .setIcon(android.R.drawable.btn_star) .setTitle("分享") .setItems(choices, onselect).create(); dialog.show(); } }); } /** * 选项的事件监听器 */ OnClickListener onselect = new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub switch (which) { case 0: Toast.makeText(Lesson_02_Dia.this, "您选择了新浪微博.",Toast.LENGTH_SHORT).show(); break; case 1: Toast.makeText(Lesson_02_Dia.this, "您选择了校内",Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(Lesson_02_Dia.this, "您选择了街旁",Toast.LENGTH_SHORT).show(); break; } } };</pre> <p></p> <p>如果在弹出框中做一个登陆界面。</p> <p>继续补充...先上图...</p> <p><img alt="" src="https://simg.open-open.com/show/fdfa7ce3e38d8d2e7754f49d5c632fa7.jpg" width="274" height="262" /></p> <p>页面login.xml: 示例写的比较简单,布局大家可以自己完善、修改。</p> <pre class="brush:xml; toolbar: true; auto-links: false;"><?xml version="1.0" encoding="utf-8"?> <TableLayout android:id="@+id/widget36" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" > <TextView android:id="@+id/widget37" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" > </TextView> <EditText android:id="@+id/widget38" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textSize="18sp" > </EditText> <TextView android:id="@+id/widget39" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码:" > </TextView> <EditText android:id="@+id/widget40" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textSize="18sp" > </EditText> </TableLayout></pre> <p></p> 代码 : (也比较简单) <pre class="brush:java; toolbar: true; auto-links: false;">LayoutInflater factory = LayoutInflater.from(MyActivity.this); //获得自定义对话框 View view = factory.inflate(R.layout.login, null); AlertDialog dialog02 = new AlertDialog.Builder(MyActivity.this) .setIcon(android.R.drawable.btn_star) .setTitle("登录") .setView(view) .setPositiveButton("Yes", onclick) .setNegativeButton("No", onclick).create(); dialog02.show();</pre> <p></p> <p>有问题欢迎大家交流。</p> <p>文章出处:<a href="/misc/goto?guid=4959499365575132194" rel="nofollow">http://blog.csdn.net/feng88724/article/details/6171450</a></p>