AndroidstudioAlertDialog(对话框)详解
1.基本使⽤流程
Step 1:创建AlertDialog.Builder对象;
Step 2:调⽤setIcon()设置图标,setTitle()或setCustomTitle()设置标题;
Step 3:设置对话框的内容:setMessage()还有其他⽅法来指定显⽰的内容;
Step 4:调⽤setPositive/Negative/NeutralButton()设置:确定,取消,中⽴按钮;
Step 5:调⽤create()⽅法创建这个对象,再调⽤show()⽅法将对话框显⽰出来;
2.⼏种常⽤的对话框使⽤⽰例
运⾏效果图:
核⼼代码:
MainActivity.java:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_dialog_one;
private Button btn_dialog_two;
private Button btn_dialog_three;
private Button btn_dialog_four;
private Context mContext;
private boolean[] checkItems;
private AlertDialog alert = null;
private AlertDialog.Builder builder = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
bindView();
}
private void bindView() {
btn_dialog_one = (Button) findViewById(R.id.btn_dialog_one);
btn_dialog_two = (Button) findViewById(R.id.btn_dialog_two);
btn_dialog_three = (Button) findViewById(R.id.btn_dialog_three);
btn_dialog_four = (Button) findViewById(R.id.btn_dialog_four);
btn_dialog_one.setOnClickListener(this);
btn_dialog_two.setOnClickListener(this);
btn_dialog_three.setOnClickListener(this);
btn_dialog_four.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
//普通对话框
case R.id.btn_dialog_one:
alert = null;
builder = new AlertDialog.Builder(mContext);
alert = builder.setIcon(R.mipmap.ic_icon_fish)
.
setTitle("系统提⽰:")
.setMessage("这是⼀个最普通的AlertDialog,\n带有三个按钮,分别是取消,中⽴和确定")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, "你点击了取消按钮~", Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, "你点击了确定按钮~", Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("中⽴", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, "你点击了中⽴按钮~", Toast.LENGTH_SHORT).show();
}
}).create();            //创建AlertDialog对象
alert.show();                    //显⽰对话框
break;
/
/普通列表对话框
case R.id.btn_dialog_two:
final String[] lesson = new String[]{"语⽂", "数学", "英语", "化学", "⽣物", "物理", "体育"};
alert = null;
builder = new AlertDialog.Builder(mContext);
alert = builder.setIcon(R.mipmap.ic_icon_fish).setTitle("选择你喜欢的课程").setItems(lesson,newDialogInterface.OnClickListener(){@Overridepublicvoid onClick(DialogInterface dialog,int which){Toast.makeText(getApplicationContext(),                alert.show();break;//单选列表对话框case R.id.btn_dialog_three:finalString[] fruits =newString[]{"苹果","雪梨","⾹蕉","葡萄","西⽠"};
alert =null;
builder =newAlertDialog.Builder(mContext);
alert = builder.setIcon(R.mipmap.ic_icon_fish).setTitle("选择你喜欢的⽔果,只能选⼀个哦~").setSingleChoiceItems(fruits,0,newDialogInterface.OnClickListener(){@Overridepublicvoid onClick(DialogInterface dialog,int which){Toast.mak                alert.show();break;//多选列表对话框case R.id.btn_dialog_four:finalString[] menu =newString[]{"⽔煮⾖腐","萝⼘⽜腩","酱油鸡","胡椒猪肚鸡"};//定义⼀个⽤来记录个列表项状态的boolean数组
checkItems =newboolean[]{false,false,false,false};
alert =null;
builder =newAlertDialog.Builder(mContext);
alert = builder.setIcon(R.mipmap.ic_icon_fish).setMultiChoiceItems(menu, checkItems,newDialogInterface.OnMultiChoiceClickListener(){@Overridepublicvoid onClick(DialogInterface dialog,int which,boolean isChecked){                                checkItems[which]= isChecked;}}).setPositiveButton("确定",newDialogInterface.OnClickListener(){@Overridepublicvoid onClick(DialogInterface dialog,int which){String result ="";for(int i =0; i < checkItems.length                                        result += menu[i]+" ";}Toast.makeText(getApplicationContext(),"客官你点了:"+ result,Toast.LENGTH_SHORT).show();}}).create();
alert.show();break;}}}
布局就是四个简单的按钮,这⾥就不贴出来了,⽤法⾮常简单~⽆⾮就是创建⼀个Builder对象后,进⾏相关设置,然后create()⽣成⼀个AlertDialog对象,最后调⽤show()⽅法将AlertDialog 显⽰出来⽽已!另外,细⼼的你可能发现我们点击对话框的外部区域,对话框就会消失,我们可以为builder设置setCancelable(false)即可解决这个问题!
3.通过Builder的setView()定制显⽰的AlertDialog
我们可以⾃定义⼀个与系统对话框不同的布局,然后调⽤setView()将我们的布局加载到 AlertDialog上,上⾯我们来实现这个效果:
关键代码:
⾸先是两种不同按钮的selctor的drawable⽂件:
btn_l:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="schemas.android/apk/res/android">
<item android:state_pressed="true" android:drawable="@mipmap/iv_icon_exit_pressed"/>
<item android:drawable="@mipmap/iv_icon_exit_normal"/>
</selector>
btn_l:
<?xml version="1.0" encoding="utf-8"?>安卓intent用法
<selector xmlns:android="schemas.android/apk/res/android">
<item android:state_pressed="true" android:drawable="@mipmap/bg_btn_pressed"/>
<item android:drawable="@mipmap/bg_btn_normal"/>
</selector>
接着是⾃定义的Dialog布局:view_l:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="schemas.android/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/titlelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#53CC66"
android:padding="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="提⽰信息"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold"/>
<Button
android:id="@+id/btn_cancle"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:background="@drawable/btn_selctor_exit"/>
</RelativeLayout>
<LinearLayout
android:id="@+id/ly_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/titlelayout"
android:layout_centerInParent="true"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="通过setView()⽅法定制AlertDialog"
android:textColor="#04AEDA"
android:textSize="18sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="作者:Coder-pig"
android:textColor="#04AEDA"
android:textSize="18sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ly_detail"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_blog"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="@drawable/btn_selctor_choose"
android:text="访问博客"
android:textColor="#ffffff"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_close"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="@drawable/btn_selctor_choose"
android:text="关闭"
android:textColor="#ffffff"
android:textSize="20sp"/>
</LinearLayout>
</RelativeLayout>
最后是MainActivity.java:
public class MainActivity extends AppCompatActivity {
private Button btn_show;
private View view_custom;
private Context mContext;
private AlertDialog alert = null;
private AlertDialog.Builder builder = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
btn_show = (Button) findViewById(R.id.btn_show);
//初始化Builder
builder = new AlertDialog.Builder(mContext);
//加载⾃定义的那个View,同时设置下
final LayoutInflater inflater = LayoutInflater();
view_custom = inflater.inflate(R.layout.view_dialog_custom, null,false);
builder.setView(view_custom);
builder.setCancelable(false);
alert = ate();
view_custom.findViewById(R.id.btn_cancle).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alert.dismiss();
}
});
view_custom.findViewById(R.id.btn_blog).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "访问博客", Toast.LENGTH_SHORT).show();
Uri uri = Uri.parse("blog.csdn/coder_pig");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
alert.dismiss();
}
});
view_custom.findViewById(R.id.btn_close).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "对话框已关闭~", Toast.LENGTH_SHORT).show();                alert.dismiss();
}
});
btn_show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alert.show();
}
});
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。