android中提示&对话框----AlertDialog
AlertDialog(对话框)
一、对话框的基本使用流程
step1:创建AlertDialog.Buider;
step2:调用setIcon()设置图标,setTitle()或者setCustomerTitle设置标题
step3:设置对话框的内容setMessage()还有其他方式;
step4:setPosition/Negative/NaturalButton设置:确定、取消、中立
step5:调用create()方法创建这个对象,在调用show()方法将对话框显示出来
二、几种常用的对话框已经自定义view的对话框使用实例
package com.example.test3; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity implements View.OnClickListener{ private Button btn1; private Button btn2; private Button btn3; private Button btn4; private Button btn5; private AlertDialog.Builder builder; private AlertDialog alertDialog; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = (Button) findViewById(R.id.btn1); btn2 = (Button) findViewById(R.id.btn2); btn3 = (Button) findViewById(R.id.btn3); btn4 = (Button) findViewById(R.id.btn4); btn5 = (Button) findViewById(R.id.btn5); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); btn4.setOnClickListener(this); btn5.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btn1:{ 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) { } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }) .setNeutralButton("中立", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }).create(); alertDialog.show(); break; } case R.id.btn2:{ final String[] lesson = new String[]{"语文", "数学", "英语", "化学", "生物", "物理", "体育"}; builder = new AlertDialog.Builder(MainActivity.this); alertDialog= builder.setIcon(R.mipmap.ic_launcher) .setTitle("选择你喜欢的课程") .setItems(lesson, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "你选择了" + lesson[which], Toast.LENGTH_SHORT).show(); } }).create(); alertDialog.show(); break; } case R.id.btn3:{ final String[] fruits = new String[]{"苹果", "雪梨", "香蕉", "葡萄", "西瓜"}; builder = new AlertDialog.Builder(MainActivity.this); alertDialog = builder.setIcon(R.mipmap.ic_launcher) .setTitle("选择你喜欢的水果,只能选一个哦~") .setSingleChoiceItems(fruits, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "你选择了" + fruits[which], Toast.LENGTH_SHORT).show(); } }).create(); alertDialog.show(); break; } case R.id.btn4:{ final String[] menu = new String[]{"水煮豆腐", "萝卜牛腩", "酱油鸡", "胡椒猪肚鸡"}; //定义一个用来记录个列表项状态的boolean数组 final boolean[] checkItems = new boolean[]{false, false, false, false}; builder = new AlertDialog.Builder(MainActivity.this); alertDialog = builder.setIcon(R.mipmap.ic_launcher) .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(); alertDialog.show(); break; } case R.id.btn5:{ builder = new AlertDialog.Builder(MainActivity.this); View view1 = getLayoutInflater().inflate(R.layout.view_dialog,null); builder.setView(view1); alertDialog = builder.create(); alertDialog.show(); Button btn1 = (Button) view1.findViewById(R.id.btn_cancle); Button btn2 = (Button) view1.findViewById(R.id.btn_blog); Button btn3 = (Button) view1.findViewById(R.id.btn_close); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { alertDialog.dismiss(); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { alertDialog.dismiss(); } }); btn3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { alertDialog.dismiss(); } }); break; } } } }
效果图