这是代码的效果图,当然我在程序中提供了3个方法,调用不同的方法会弹出不同的alterDialog。

主要的实现是在activity当中。

package cn.com.chenzheng_java; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.TextView; /** * * @author chenzheng_java * */ public class AlertDialogActivity extends Activity { private TextView textView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textView = (TextView) findViewById(R.id.textView); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showAlertDialog2(); } }); } /** * 弹出系统自带的alertDialog * setNegativeButton setPositiveButton setNeutralButton三种方法添加的按钮的唯一不同就是表示符号不一样 * 这三种方法如果调用多次,并不会添加多个按钮,而是,同类型的会进行覆盖,后者覆盖前者。 */ private void clickSimpleAlertDialog() { AlertDialog.Builder alertDialog = new AlertDialog.Builder(this); // 设置标题 alertDialog.setTitle("AlertDialog测试"); Resources r = this.getResources(); Drawable d = r.getDrawable(R.drawable.icon); alertDialog.setIcon(d); alertDialog.setMessage("花儿花儿满天飞"); alertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { textView.setText("确定" + which);//whitch=-1 } }); alertDialog.setNeutralButton("hello", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { textView.setText("hello" + which);//whitch=-3 } }); alertDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { textView.setText("取消" + which);//whitch=-2 } }); alertDialog.show(); } /** * 加载自定义的布局格式到AlertDialog */ private void showCustomLayoutAlertDialog(){ /* * 根据用户提供的布局文件,获取一个view * */ LayoutInflater flater = LayoutInflater.from(this); View alertDialogView = flater.inflate(R.layout.alertdialog, null); new AlertDialog.Builder(this).setTitle("title").setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { textView.setText("确定" + which);//whitch=-1 } }).setView(alertDialogView).show(); } private void showAlertDialog2(){ AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle(R.string.hello); String [] dataArray = new String[]{"hello","baby","word"}; dialog.setItems(dataArray, new DialogInterface.OnClickListener() { /*** * 我们这里传递给dialog.setItems方法的参数为数组,这就导致了我们下面的 * onclick方法中的which就跟数组下标是一样的,点击hello时返回0;点击baby返回1…… */ @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); textView.setText("点击了按钮:which="+which+" 按钮:"); } }).show(); } }

在这里,我们的alertDialog.xml代码为<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <EditText android:id="@+id/editText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:minLines="1" /> <EditText android:id="@+id/editText2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:minLines="1" /><EditText android:id="@+id/editText3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:minLines="1" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello" /> </LinearLayout>

main.xml代码

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="show AlertDialog" /> <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> </LinearLayout>

运行便可得到相应的结果。

----------------------------------------------------------------------------------

用法解析:

这里实例化AlterDialog时一定要注意了,它本身是没有可供直接调用的构造方法的,我们是通过new AlterDialog.Builder()来创建实例的。

AlterDialog上不光可以放置按钮,还可以放置很多东西,在代码中我们用到的就有,数组信息列表、按钮、图片、EditText等等等等……

用户要想知道我们点击了alertDialog上的哪一个元素,是通过DialogInterface.OnClickListener的onclick方法里的which参数来决定的,想要AlterDialog消失则调用dialog.dismiss()方法!

如果我们设置cancelable()为true的话,我们就可以通过返回键来关闭他。

setSingleChoiceItems,设置为单选项对话框

setMultiChoiceItems设置为多选选项对话款

setItems,设置为选项对话框,不区分多选单选

setCustomTitle(View customTitleView),设置自定义的Title视图

setView(View view),设置对话框内容为自定义的视图

更多相关文章

  1. android sdk 下载失败解决方法
  2. android Button字体设置颜色
  3. Android(安卓)IllegalArgumentException: Cannot draw recycled
  4. Android(安卓)签名打包出现的错误的解决方法以及代码中获取应用
  5. Android中onTouch方法的执行过程以及和onClick执行发生冲突的解
  6. 关于android camera2 图像格式的一些问题
  7. Android下的数据储存方式
  8. Ubuntu12.04编译ANDROID ICS出错解决方法
  9. android adt jni开发步骤

随机推荐

  1. Android Untold Stories
  2. Android基本数据存储方法
  3. Android 快速启动
  4. AES Android 加密
  5. Android Dependencies 是什么?
  6. android 入门
  7. Android点击效果加边框
  8. Android VideoView orientation change w
  9. 76 Android BroadcastReceiver简单使用
  10. Android(安卓)Studio代码自动提示无效(not