首页 > 代码库 > AndroidUI组件之AlertDialog
AndroidUI组件之AlertDialog
package com.gc.alertdialogdemo; /** * AlertDialog: * 1、AlertDialog的功能很强大,它可以生成各种内容的对话框,实际上AlertDialog生成的 * 的对话框总可以分为4个区域:(1)图标区(2)标题区(3)内容区(3)按钮区。 * 2、创建一个对话框需要经过如下几个步骤: * (1)使用创建AlertDialog.Builder对象。 * (2)调用AlertDialog.Builder的setTitle()或setCustomTitle()方法设置标题 * (3)调用AlertDialog.Builder的setIcon()方法设置标题 * (4)调用AlertDialog.Builder的相关设置方法设置对话框内容 * (5)调用AlertDialog.Builder的setPositiveButton()、setNegativeButton()或setNeutralButton()方法添加多个按钮 * (6)调用AlertDialog.Builder的create()方法创建AlertDialog对象,再调用AlertDialog对象的show()方法将该对话框显示出来。 * 3、AlertDialog提供了如下6种方法来指定对话框内容: * (1)setMessage():设置对话内容为简单文本内容。 * (2)setItems():设置对话框内容为简单列表项 * (3)setSingleChoiceItems():设置对话框内容为单选列表项 * (4)setMultiChoiceItems():设置对话框内容为多选列表项 * (5)setAdapter():设置对话框内容为自定义列表项 * (6)setView():设置对话框内容为自定义View. * */ import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.view.Menu; import android.view.View; import android.widget.ArrayAdapter; import android.widget.TableLayout; import android.widget.TextView; /** * * @author Android将军 * */ public class MainActivity extends Activity { public TextView show; private String[] items=new String[] { "Android将军", "ios将军", "Cocos2d-x将军", "将军" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); show=(TextView)findViewById(R.id.show); } public void customView(View source) { TableLayout loginForm=(TableLayout)getLayoutInflater().inflate(R.layout.login, null); new AlertDialog.Builder(this) .setTitle("自定义View对话框") //设置图标 .setIcon(R.drawable.ic_launcher) .setView(loginForm) .setPositiveButton("登录", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }) .setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }) .create() .show(); } public void customList(View source) { AlertDialog.Builder builder=new AlertDialog.Builder(this) //设置对话框标题 .setTitle("自定义列表对话框") //设置图标 .setIcon(R.drawable.ic_launcher) .setAdapter(new ArrayAdapter<String>(this, R.layout.array_item, items), null); setPositiveButton(builder); setNeutralButton(builder); //为AlertDialog.Builder添加“取消”按钮 setNegativeButton(builder).create().show(); } public void multiChoice(View source) { AlertDialog.Builder builder=new AlertDialog.Builder(this) //设置对话框标题 .setTitle("多选列表对话框") //设置图标 .setIcon(R.drawable.ic_launcher) .setMultiChoiceItems(items, new boolean[]{false,true,false,true}, null); setPositiveButton(builder); setNeutralButton(builder); //为AlertDialog.Builder添加“取消”按钮 setNegativeButton(builder).create().show(); } public void singleChoice(View source) { AlertDialog.Builder builder=new AlertDialog.Builder(this) //设置对话框标题 .setTitle("单选列表对话框") //设置图标 .setIcon(R.drawable.ic_launcher) .setSingleChoiceItems(items, 1, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub show.setText("你选中了《"+items[which]+"》"); } }); setPositiveButton(builder); setNeutralButton(builder); //为AlertDialog.Builder添加“取消”按钮 setNegativeButton(builder).create().show(); } public void simpleList(View source) { AlertDialog.Builder builder=new AlertDialog.Builder(this) //设置对话框标题 .setTitle("简单列表对话框") //设置图标 .setIcon(R.drawable.ic_launcher) //设置简单的列表项内容 .setItems(items, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub show.setText("你选中了《"+items[which]+"》"); } }); setPositiveButton(builder); setNeutralButton(builder); //为AlertDialog.Builder添加“取消”按钮 setNegativeButton(builder).create().show(); } public void simple(View source) { AlertDialog.Builder builder=new AlertDialog.Builder(this) //设置对话框标题 .setTitle("简单对话框") //设置图标 .setIcon(R.drawable.ic_launcher) .setMessage("对话框的测试内容\n第二行内容"); //为AlertDialog.Builder添加“确定”按钮 setPositiveButton(builder); setNeutralButton(builder); //为AlertDialog.Builder添加“取消”按钮 setNegativeButton(builder).create().show(); } private Builder setNeutralButton(Builder builder) { // TODO Auto-generated method stub return builder.setNeutralButton("修饰", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub show.setText("单击了【修饰】按钮!"); } }); } private Builder setNegativeButton(Builder builder) { // 调用setNegativeButton方法添加“确定”按钮 return builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub show.setText("单击了【取消】按钮!"); } }); } private Builder setPositiveButton(Builder builder) { // 调用setPositiveButton方法添加“确定”按钮 return builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub show.setText("单击了【确定】按钮!"); } }); } }
几个布局文件如下:
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/show" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:onClick="simple" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="显示简单对话框" /> <Button android:onClick="simpleList" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="简单列表项对话框" /> <Button android:onClick="singleChoice" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="单选列表项对话框" /> <Button android:onClick="multiChoice" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="多选列表项对话框" /> <Button android:onClick="customList" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="自定义列表项对话框" /> <Button android:onClick="customView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="自定义View对话框" /> </LinearLayout>
login.xml
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/loginForm" android:orientation="vertical" > <TableRow> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="用户名:" android:textSize="10pt" /> <!-- 输入用户名的文本框 --> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请填写登录账号" android:selectAllOnFocus="true" /> </TableRow> <TableRow> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="密码:" android:textSize="10pt" /> <!-- 输入密码的文本框 --> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请填写密码" android:password="true" /> </TableRow> <TableRow> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="电话号码:" android:textSize="10pt" /> <!-- 输入电话号码的文本框 --> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请填写你的电话号码" android:selectAllOnFocus="true" android:phoneNumber="true" /> </TableRow> </TableLayout>
array_item.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > </TextView>
部分效果截图:
转载请注明出处:http://blog.csdn.net/android_jiangjun/article/details/25739587
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。