直接进入主题 :
使用AlertDialog的多种方式
要创建一个AlertDialog,就要用到AlertDialog.Builder中的Create()方法,
其中AlertDialog.Builder提供了这几种常用的方法:
setTile()为对话框设置一个标题
setMessage()为对话框设置内容
setIcon()为对话框设置显示图片
setView()为对话框自定义样式
setItems()为对话框显示的一个list,一般用于显示几个命令时
setPositiveButton()给对话框添加“确认”按钮
setNegativeButton()给对话框添加“取消”按钮
第一种: 确认对话框
怎么实现呢 ?
protected void Affirmdialog() { // TODO Auto-generated method stub AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("this is a title"); builder.setIcon(R.drawable.ic_launcher); builder.setMessage("this is a message"); //是否可撤销 builder.setCancelable(false); builder.setPositiveButton("OK",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "Onclick is Ok", Toast.LENGTH_LONG).show(); } }); builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "Onclick is NO", Toast.LENGTH_LONG).show(); } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); }第二种: 单选按钮
setSengleCoiceItems():设置单选按钮
private String[] Singledata = {"boy", "girl"};
protected void Singledialog() { // TODO Auto-generated method stub AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setSingleChoiceItems(Singledata, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { // TODO Auto-generated method stub String value = Singledata[i]; Toast.makeText(MainActivity.this, value, Toast.LENGTH_SHORT).show(); } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); }
第三种 :多选按钮
setMultiChoiceItems():设置多选对话框
private String[] Multidata = {"Basketball","foolball","badminton"};
protected void Multipledialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("this is a MultipleDialogButton"); builder.setMultiChoiceItems(Multidata, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i, boolean b) { if(b){ String value = Multidata[i]; Toast.makeText(MainActivity.this, "I Like" + value, Toast.LENGTH_SHORT).show(); }else{ String value = Multidata[i]; Toast.makeText(MainActivity.this, "I No Like " + value, Toast.LENGTH_SHORT).show(); } } }); builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int arg1) { // TODO Auto-generated method stub //取消 dialogInterface.dismiss(); } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); }第四种 : 自定义对话框
从而我们可以在自定义的过程加入广告
<?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:orientation="vertical" > <TextView android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this is a customdialog"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="submit"/> </LinearLayout> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/guangkao"/> </LinearLayout>
protected void Mustomdialog() { // TODO Auto-generated method stub View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.customdialog, null); AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setView(view); AlertDialog alertDialog = builder.create(); alertDialog.show(); }
好了今天的四种AlertDialog对话框实现了,希望大家多多支持,谢谢!