AlertDialog系统原生对话框
代码参照菜鸟教程—AlertDialog(对话框)详解,看完了后就自己对着打了一遍,在这里做个记录。
MainActivity.java
package com.example.alertdialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
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 button1,button2,button3,button4;
private boolean[] checkItems;
private AlertDialog alertDialog = null;
private AlertDialog.Builder builder = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
}
private void initview(){
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
//普通对话框
case R.id.button1:
alertDialog = null;
builder = new AlertDialog.Builder(MainActivity.this);
alertDialog = builder.setIcon(R.mipmap.ic_launcher)
.setTitle("提示")
.setMessage("这是一个最普通的AlertDialog,\n带有三个按钮,分别是取消、中立和确定")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "你点击了取消按钮", Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "你点击了确定按钮", Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("中立", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "你点击了中立按钮", Toast.LENGTH_SHORT).show();
}
}).create();//创建AlertDialog对象
alertDialog.show();//显示对话框
break;
//普通列表对话框
case R.id.button2:
final String[] lesson = new String[]{"语文","数学","英语","政治","历史","地理"};
alertDialog = null;
builder = new AlertDialog.Builder(MainActivity.this);
alertDialog = builder.setIcon(R.mipmap.ic_launcher)
.setTitle("选择你最喜欢的学科")
.setItems(lesson, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "你选择了"+lesson[i], Toast.LENGTH_SHORT).show();
}
}).create();
alertDialog.show();
break;
//单选按钮列表
case R.id.button3:
final String[] fruits = new String[]{"苹果","香蕉","梨子","西瓜","菠萝"};
alertDialog = null;
builder = new AlertDialog.Builder(this);
alertDialog = builder.setIcon(R.mipmap.ic_launcher)
.setTitle("选择你喜欢的水果,单选")
.setSingleChoiceItems(fruits, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "你选择了"+fruits[i], Toast.LENGTH_SHORT).show();
}
}).create();
alertDialog.show();
break;
//多选列表对话框
case R.id.button4:
final String[] menu = new String[]{"水煮豆腐", "萝卜牛腩", "酱油鸡", "胡椒猪肚鸡"};
//定义一个用来记录个列表项状态的boolean数组
checkItems = new boolean[]{false, false, false, false};
alertDialog = null;
builder = new AlertDialog.Builder(this);
alertDialog = builder.setIcon(R.mipmap.ic_launcher)
.setMultiChoiceItems(menu, checkItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
checkItems[i] = true;
}
})
.setCancelable(false)//点击对话框外面,设为false对话框不会消失
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String result = "";
for (int j = 0; j < checkItems.length; j++) {
if(checkItems[j]){
result += menu[j] + " ";
}
}
Toast.makeText(MainActivity.this, "客官你点了:"+result, Toast.LENGTH_SHORT).show();
}
}).create();
alertDialog.show();
break;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示一个普通的Dialog"
android:textAllCaps="false"
android:id="@+id/button1"
android:layout_marginTop="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示一个带普通列表的Dialog"
android:textAllCaps="false"
android:id="@+id/button2"
android:layout_marginTop="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示一个带单选按钮的Dialog"
android:textAllCaps="false"
android:id="@+id/button3"
android:layout_marginTop="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示一个带复选框的Dialog"
android:textAllCaps="false"
android:id="@+id/button4"
android:layout_marginTop="20dp"/>
</LinearLayout>
效果展示