使用对话框的时候有些点击了并不会自己消失需要自己添加.heid()来隐藏,这样用户体验会更好。
下面来看看效果:
1.基本使用流程
-
Step 1:创建AlertDialog.Builder对象;
-
Step 2:调用setIcon()设置图标,setTitle()或setCustomTitle()设置标题;
-
Step 3:设置对话框的内容:setMessage()还有其他方法来指定显示的内容;
-
Step 4:调用setPositive/Negative/NeutralButton()设置:确定,取消,中立按钮;
-
Step 5:调用create()方法创建这个对象,再调用show()方法将对话框显示出来;
附上代码:
MainActivity.java
-
package nguhyw.alertdialog;
-
-
import android.app.AlertDialog;
-
import android.content.Context;
-
import android.content.DialogInterface;
-
import android.support.v7.app.AppCompatActivity;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.widget.Button;
-
import android.widget.Toast;
-
-
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
-
-
private Button btn_normal;
-
private Button btn_List1;
-
private Button btn_List2;
-
private Button btn_List3;
-
-
private Context mContext;
-
private boolean[] checkItems;
-
-
private AlertDialog alert = null;
-
private AlertDialog.Builder builder = null;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
-
mContext = MainActivity.this;
-
binView();
-
}
-
-
public void binView(){
-
btn_normal = (Button)findViewById(R.id.btn_normal);
-
btn_List1 = (Button)findViewById(R.id.btn_List1);
-
btn_List2 = (Button)findViewById(R.id.btn_List2);
-
btn_List3 = (Button)findViewById(R.id.btn_List3);
-
-
btn_normal.setOnClickListener(this);
-
btn_List1.setOnClickListener(this);
-
btn_List2.setOnClickListener(this);
-
btn_List3.setOnClickListener(this);
-
}
-
-
-
@Override
-
public void onClick(View v) {
-
-
switch (v.getId()){
-
case R.id.btn_normal:
-
show_normal_dialog();
-
break;
-
-
case R.id.btn_List1:
-
show_List1_dialog();
-
break;
-
-
case R.id.btn_List2:
-
show_List2_dialog();
-
break;
-
-
case R.id.btn_List3:
-
show_List3_dialog();
-
break;
-
-
default:
-
break;
-
}
-
-
}
-
-
public void show_normal_dialog(){
-
-
alert = null;
-
builder = new AlertDialog.Builder(mContext);
-
alert = builder.setIcon(R.drawable.nguhyw)
-
.setTitle("系统提示")
-
.setMessage("这是一个很普通的对话框,你想打我也没用!")
-
.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();
-
alert.show();
-
}
-
-
public void show_List1_dialog(){
-
final String[] lesson = new String[]{"语文","数学","英语","物理","化学","生物","历史",};
-
alert = null;
-
builder = new AlertDialog.Builder(mContext);
-
alert = builder.setIcon(R.drawable.nguhyw)
-
.setTitle("选择你喜欢的课程")
-
.setItems(lesson, new DialogInterface.OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
Toast.makeText(getApplicationContext(),"你选择了"+lesson[which],Toast.LENGTH_SHORT).show();
-
alert.hide();
-
}
-
})
-
.create();
-
alert.show();
-
}
-
-
public void show_List2_dialog(){
-
final String[] fruits = new String[]{"苹果", "雪梨", "香蕉", "葡萄", "西瓜"};
-
alert = null;
-
builder = new AlertDialog.Builder(mContext);
-
alert = builder.setIcon(R.drawable.nguhyw)
-
.setTitle("选择你喜欢的水果")
-
.setSingleChoiceItems(fruits, 0, new DialogInterface.OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
Toast.makeText(getApplicationContext(),"你选择了"+fruits[which],Toast.LENGTH_SHORT).show();
-
alert.hide();
-
}
-
}).create();
-
alert.show();
-
}
-
-
public void show_List3_dialog(){
-
final String[] menu = new String[]{"水煮豆腐", "萝卜牛腩", "酱油鸡", "胡椒猪肚鸡"};
-
checkItems = new boolean[]{false, false, false, false};
-
-
alert = null;
-
builder = new AlertDialog.Builder(mContext);
-
alert = builder.setIcon(R.drawable.nguhyw)
-
.setMultiChoiceItems(menu, checkItems, new DialogInterface.OnMultiChoiceClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
-
checkItems[which] = isChecked;
-
}
-
})
-
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
String result = "";
-
for(int i=0; i<checkItems.length;i++){
-
if(checkItems[i])
-
result += menu[i] + " ";
-
}
-
Toast.makeText(getApplicationContext(), "客官你点了:" + result, Toast.LENGTH_SHORT).show();
-
}
-
}).create();
-
alert.show();
-
}
-
}
XML文件
-
<?xml version="1.0" encoding="utf-8"?>
-
<RelativeLayout xmlns:android=""
-
xmlns:tools="" android:layout_width="match_parent"
-
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
-
android:paddingRight="@dimen/activity_horizontal_margin"
-
android:paddingTop="@dimen/activity_vertical_margin"
-
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
-
-
<LinearLayout
-
android:orientation="vertical"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:layout_alignParentTop="true">
-
-
<Button
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="1、显示一个普通的对话框"
-
android:id="@+id/btn_normal" />
-
-
<Button
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="2、显示一个列表对话框"
-
android:id="@+id/btn_List1" />
-
-
<Button
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="3、显示一个单选的列表"
-
android:id="@+id/btn_List2" />
-
-
<Button
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="3、显示一个复选框的列表"
-
android:id="@+id/btn_List3" />
-
</LinearLayout>
-
</RelativeLayout>
本次介绍的对话框用法非常简单~无非就是创建一个Builder对象后, 进行相关设置,然后create()生成一个AlertDialog对象,最后调用show()方法将AlertDialog 显示出来而已!另外,细心的你可能发现我们点击对话框的外部区域,对话框就会消失,我们 可以为builder设置setCancelable(false)即可解决这个问题!
阅读(1403) | 评论(0) | 转发(0) |