布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.edu.jereh.jreduch01.AlertActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bt1" android:text="对话框"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bt2" android:text="单选对话框" android:layout_below="@+id/bt1" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bt3" android:text="多选对话框" android:layout_below="@+id/bt2" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bt4" android:text="自定义对话框" android:layout_below="@+id/bt3" android:layout_alignParentStart="true" /> </RelativeLayout>
监听
package com.edu.jereh.jreduch01; import android.app.AlertDialog; 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.ImageView; import android.widget.Toast; public class AlertActivity extends AppCompatActivity { private Button bt1; private Button bt2; private Button bt3; private Button bt4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alert); bt1 = (Button) findViewById(R.id.bt1); bt2 = (Button) findViewById(R.id.bt2); bt3 = (Button) findViewById(R.id.bt3); bt4 = (Button) findViewById(R.id.bt4); bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(AlertActivity.this); builder.setTitle("提示"); builder.setIcon(R.mipmap.ic_launcher); builder.setMessage("确定。。。。吗"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(getBaseContext(), "点击了确定", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(getBaseContext(), "点击了取消", Toast.LENGTH_SHORT).show(); }//.show()是显示" "中的字 }); builder.setCancelable(false);//点击其他空白位置不取消 AlertDialog alertDialog = builder.create(); alertDialog.show(); } }); bt2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(AlertActivity.this); builder.setTitle("请选择:"); builder.setIcon(R.mipmap.ic_launcher); final String[] items = {"北京", "上海", "杭州", "烟台"}; builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(getBaseContext(), items[which].toString(), Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); builder.setCancelable(false); AlertDialog alertDialog = builder.create(); alertDialog.show(); } }); bt3.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(AlertActivity.this); builder.setTitle("请选择"); builder.setIcon(R.mipmap.ic_launcher); final String[] items = {"北京", "上海", "青岛", "广州", "烟台"}; // boolean[] choice = {true, false, true, false, true}; final boolean[] itemChoice = new boolean[items.length]; itemChoice[0] = true; itemChoice[1] = true; builder.setMultiChoiceItems(items, itemChoice, new DialogInterface.OnMultiChoiceClickListener() { public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked) { Toast.makeText(getBaseContext(), items[which].toString(), Toast.LENGTH_SHORT).show(); itemChoice[which] = true; } else { itemChoice[which] = false; } } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { String str = ""; for (int i = 0; i < items.length; i++) { str += (itemChoice[i] == true) ? items[i] : " "; } Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT).show(); } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } }); bt4.setOnClickListener(new View.OnClickListener(){ public void onClick(View view){ View v = getLayoutInflater().inflate(R.layout.toast_layout,null); ImageView iv = (ImageView) v.findViewById(R.id.toast_iv); iv.setOnClickListener(new View.OnClickListener(){ public void onClick(View view){ Toast.makeText(AlertActivity.this,"你点击了图片",Toast.LENGTH_SHORT).show(); } }); AlertDialog.Builder builder = new AlertDialog.Builder(AlertActivity.this); builder.setView(v); AlertDialog alertDialog = builder.create(); alertDialog.show(); } }); } }