1. 点击按钮,弹出窗口,2s后自动消失
Button button1 = findViewById(R.id.button1);button1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Popup1();}});
private void Popup1() {//创建弹窗构建器AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle(\"这个是标题!\").setMessage(\"信息内容...(2s后自动消失)\")//点击窗口以外的区域,窗口消失 (默认为true).setCancelable(false);//创建弹窗final AlertDialog dlg = builder.create();//窗口显示dlg.show();//时间线程,2s后执行里面的代码Timer t = new Timer();t.schedule(new TimerTask() {public void run() {//窗口消失dlg.dismiss();}}, 2000);}
2. 点击按钮,弹窗才消失
Button button2 = findViewById(R.id.button2);button2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Popup2();}});
private void Popup2() {AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle(\"窗口二\").setMessage(\"点击按钮才消失\")//点击窗口以外的区域,窗口消失 (默认为true).setCancelable(false)//点击按钮,自动调用 dismiss(),弹窗消失.setPositiveButton(\"点我点我\", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, \"窗口消失\", Toast.LENGTH_SHORT).show();}});builder.show();}
3. 选择其中一个按钮, 弹窗才消失
Button button3 = findViewById(R.id.button3);button3.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Popup3();}});
private void Popup3() {AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle(\"窗口三\").setMessage(\"你喜欢吃苹果吗?\")//点击窗口以外的区域,窗口消失 (默认为true).setCancelable(false)//点击其中一个按钮才消失弹窗//一般有三个 Button类型:位置、命名不同,但方法一样// PositiveButton(确定,位置在最右边),NegativeButton(否定,位置在最右边的左边),NeutralButton(中立,位置在最左边).setPositiveButton(\"喜欢\", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, \"你喜欢吃苹果\", Toast.LENGTH_SHORT).show();}}).setNegativeButton(\"不喜欢\", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this,\"你不喜欢吃苹果\",Toast.LENGTH_SHORT).show();}}).setNeutralButton(\"不清楚\", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this,\"你纠结了\",Toast.LENGTH_SHORT).show();}});builder.show();}
4. 自定义布局,显示在弹窗上
- 多新建一个 xml自定义布局 (activity_dialog1.xml)
<?xml version=\"1.0\" encoding=\"utf-8\"?><LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"android:layout_width=\"320dp\"android:layout_height=\"wrap_content\"android:orientation=\"vertical\"android:padding=\"10dp\"><TextViewandroid:id=\"@+id/title\"android:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:text=\"窗口四\"android:textSize=\"20dp\"/><TextViewandroid:id=\"@+id/message\"android:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:text=\"信息内容...(这里是xml)\"android:textSize=\"15dp\"android:layout_marginTop=\"10dp\"/><TextViewandroid:id=\"@+id/determine\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\"android:text=\"确定\"android:textSize=\"15dp\"android:textColor=\"@color/colorPrimary\"android:gravity=\"right\"/></LinearLayout>
- 点击按钮,显示自定义布局的弹窗(此按钮的布局在 activity_main.xml设置)
Button button4 = findViewById(R.id.button4);button4.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Popup4();}});
private void Popup4() {// 创建弹窗构建器final AlertDialog dialog = new AlertDialog.Builder(this).create();// 注:必须在 window.setContentView之前 show, 否则会崩dialog.show();//点击窗口以外的区域,窗口消失 (默认为true)dialog.setCancelable(false);// 弹窗获取自定义的布局dialog.getWindow().setContentView(R.layout.activity_dialog1);//* 这里设置了,就显示这里的文字。把这段代码隐蔽就显示 xml那边的文字TextView dialogMessage = dialog.findViewById(R.id.message); //*dialogMessage.setText(\"信息内容...(这里是java)\"); //*TextView tvDetermine = dialog.findViewById(R.id.determine);//点击确定, 对话框才消失tvDetermine.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//窗口消失dialog.dismiss();Toast.makeText(MainActivity.this, \"你点击了确定\", Toast.LENGTH_SHORT).show();}});}
5. 创建字符串数组作为弹出菜单列表
Button button5 = findViewById(R.id.button5);button5.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Popup5();}});
private void Popup5() {// 创建字符串数组作为弹出菜单列表里的文本final String[] items = new String[]{\"one\",\"two\",\"three\",\"four\",\"five\"};AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);// 点击窗口以外的区域,窗口消失 (默认为true)builder.setCancelable(false);// 设置弹出窗口标题builder.setTitle(\"选择其中一个\");builder.setItems(items, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {//gender[which]: 为输出文本,传入数组 items(一定要写后面的which序号)Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();}});builder.show();}
6. 单选列表项
Button button6 = findViewById(R.id.button6);button6.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Popup6();}});
private void Popup6() {final String[] item = new String[]{\"one\", \"two\", \"three\", \"four\",\"five\"};AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle(\"选择其中一个\")// checkedItem,默认选择:0表示选中第一个(以此类推).setSingleChoiceItems(item, 0, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, \"你单选了\" + item[which], Toast.LENGTH_SHORT).show();}}).setPositiveButton(\"确认\", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, \"确定\",Toast.LENGTH_SHORT).show();}}).setNegativeButton(\"取消\", null).create();alertDialog.show();}
7. 多选项列表
Button button7 = findViewById(R.id.button7);button7.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Popup7();}});
private void Popup7() {// 创建数据final String[] item = new String[]{\"one\", \"two\", \"three\", \"four\",\"five\"};// 创建对话框构建器AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);builder.setTitle(\"可以选择一个或多个\").setMultiChoiceItems(item,// 列表里有几个 item,这里的数组长度几个就有几个,true为默认勾选,false则默认不勾选new boolean[]{true, false, false, true, false},new DialogInterface.OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which, boolean isChecked) {// TODO Auto-generated method stubif (isChecked) {Toast.makeText(MainActivity.this, item[which], Toast.LENGTH_SHORT).show();}}}).setPositiveButton(\"确认\", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, \"确定\",Toast.LENGTH_SHORT).show();}}).setNegativeButton(\"取消\", null).create();builder.create().show();}
Git下载