先写布局,来四个按钮
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:id="@+id/btn1" android:text="点击显示" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:id="@+id/btn2" android:text="单选显示" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:id="@+id/btn3" android:text="多选显示" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:id="@+id/btn4" android:text="列表" /> </LinearLayout>
然后主文件
package com.example.deemo; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; public class MainActivity extends Activity { String[] single_list = {"man","woman","女博士","程序员"}; String[] multi_list = {"小鸟","静流","千早","露西亚","朱音","篝"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initEvent(); } private void initEvent(){//点击事件 findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {//确定对话框 @Override public void onClick(View arg0) { showDialog1(); } }); findViewById(R.id.btn2).setOnClickListener(new OnClickListener() {//单选对话框 @Override public void onClick(View arg0) { showDialog2(); } }); findViewById(R.id.btn3).setOnClickListener(new OnClickListener() {//多选对话框 @Override public void onClick(View arg0) { showDialog3(); } }); findViewById(R.id.btn4).setOnClickListener(new OnClickListener() {//列表对话框 @Override public void onClick(View arg0) { showDialog4(); } }); } private void showDialog1(){//显示确认对话框 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("确认dialog"); builder.setIcon(R.drawable.ic_launcher);//图标 builder.setMessage("提示内容"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {//确定按钮 @Override public void onClick(DialogInterface dialog, int arg1) { Toast.makeText(MainActivity.this, "点击确定", Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {//取消按钮 @Override public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(MainActivity.this, "点击取消", Toast.LENGTH_SHORT).show(); } }); AlertDialog dialog = builder.create();//获取dialog dialog.show();//显示dialog } private void showDialog2(){//单选对话框 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("选择Dialog"); builder.setIcon(R.drawable.ic_launcher);//图标 builder.setSingleChoiceItems(single_list, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) {//第二个为传入第几个选择 String str = single_list[arg1]; Toast.makeText(MainActivity.this, "this is a"+str, Toast.LENGTH_SHORT).show(); } });//单选按钮描述,默认选中 AlertDialog dialog = builder.create();//获取dialog dialog.show();//显示dialog } private void showDialog3(){//多选对话框 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("选择Dialog"); builder.setIcon(R.drawable.ic_launcher);//图标 builder.setMultiChoiceItems(multi_list, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface arg0, int arg1, boolean arg2) { if(arg2){ Toast.makeText(MainActivity.this, "我的老婆们是"+multi_list[arg1], Toast.LENGTH_SHORT).show(); }else { Toast.makeText(MainActivity.this, "还不是我的老婆们是"+multi_list[arg1], Toast.LENGTH_SHORT).show(); } } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {//取消按钮,其实是隐藏 @Override public void onClick(DialogInterface arg0, int arg1) { arg0.dismiss(); } }); AlertDialog dialog = builder.create();//获取dialog dialog.show();//显示dialog } private void showDialog4(){//单选对话框 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("列表"); builder.setIcon(R.drawable.ic_launcher);//图标 builder.setItems(multi_list, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(MainActivity.this, "我老婆"+multi_list[arg1], Toast.LENGTH_SHORT).show(); } }); //什么取消啊确定啊什么功能根据上面自己一加 AlertDialog dialog = builder.create();//获取dialog dialog.show();//显示dialog } }
还有一个自定义布局
首先写一个自定义布局文件,名字比如dialog_layout.xml,随意写的不要介意
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内容" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提交" android:layout_marginLeft="10dp" /> </LinearLayout> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/four" /> </LinearLayout>
然后java文件的一部分
点击事件
findViewById(R.id.btn5).setOnClickListener(new OnClickListener() {//列表对话框 @Override public void onClick(View arg0) { showDialog5(); } });
然后对话框
private void showDialog5(){//自定义对话框 LayoutInflater inflater = LayoutInflater.from(this);//获取布局 View view = inflater.inflate(R.layout.dialog_layout, null); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("自定义Dialog"); builder.setIcon(R.drawable.ic_launcher);//图标 builder.setView(view);//传入view AlertDialog dialog = builder.create();//获取dialog dialog.show();//显示dialog }