He wrote four dialog
Layout file
<?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:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定取消对话框" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="列表对话框"/> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="单选列表框"/> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="多选列表框"/> </LinearLayout>
java code
package com.example.mydialogi; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); the setContentView (R.layout.activity_main); / * * with "OK", "Cancel" button dialog * * / the Button Button1 = (the Button) the findViewById (R.id.button1); button1.setOnClickListener ( new new View.OnClickListener ( ) { @Override public void the onClick (View V) { // create a AlterDialog objects AlertDialog AlertDialog = new new AlertDialog.Builder ( the MainActivity. the this ) .create (); alertDialog.setIcon (R.drawable.b); alertDialog.setTitle ( "title" ); alertDialog.setMessage ( "content" ); // set the button and in response alertDialog.setButton ( DialogInterface.BUTTON_NEGATIVE, "cancel", new new DialogInterface.OnClickListener () { @Override public void the onClick (DialogInterface Dialog, int Which) { Toast. makeText ( . the MainActivity the this , "choose cancel" , Toast.LENGTH_SHORT ) the .Show (); } } ); alertDialog.setButton( DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText( MainActivity.this,"选择了确定",Toast.LENGTH_SHORT ).show(); } } ); alertDialog.show(); } }); / * ); List dialog * * * / the Button Button2 = (the Button) the findViewById (R.id.button2); button2.setOnClickListener ( new new View.OnClickListener () { @Override public void the onClick (View V) { // with four list items the list dialog Final String [] = the Items new new String [] { "a", "B", "C", "D" }; // Create a Builder objects AlertDialog.Builder Builder = new new AlertDialog.Builder (the MainActivity. the this builder.setIcon (R.drawable.b); builder.setTitle ( "title" ); // add a list item builder.setItems (the Items, new new DialogInterface.OnClickListener () { @Override public void onClick (DialogInterface Dialog, int Which) { Toast.makeText ( MainActivity. the this , "select the [ "+ Items [which] +"] " , Toast.LENGTH_SHORT ) .Show (); } }); Once builder.create () show ();. // }Create and display the dialog box }); the Button Button3 = (the Button) the findViewById (R.id.button3); button3.setOnClickListener ( new new View.OnClickListener () { @Override public void the onClick (View V) { // band selection list item dialog Final String [] items = new new String [] { "a", "B", "C", "D" }; AlertDialog.Builder Builder = new new AlertDialog.Builder (the MainActivity. the this ); builder.setTitle ( "title" ); builder.setIcon (R.drawable.b); // add a list item builder.setSingleChoiceItems (items, 0, new new DialogInterface.OnClickListener () { @Override public void the onClick (DialogInterface Dialog, int Which) { Toast.makeText ( the MainActivity. the this , "choice is [" + items [which] + "]" , Toast.LENGTH_SHORT ) the .Show (); } }); // add OK button builder.setPositiveButton ( "OK", null ); Once builder.create () Show ();. } }); / * * With multiple selection list box item * * / the Button Button4 = (the Button) the findViewById (R.id.button4); button4.setOnClickListener ( new new View.OnClickListener () { @Override public void the onClick (View V) { Final Boolean [] = the CheckedItems new new Boolean [] { to false , to true , to false , to true , to false }; Final String [] items = new new String[]{ "A","B","C","D","E" }; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.drawable.b); builder.setTitle("标题"); builder.setMultiChoiceItems( items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { checkedItems[which]=isChecked;//改变被操作选项的状态 } } ); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String result = ""; for(int i=0;i<items.length;i++){ if(checkedItems[i]==to true ){ Result + = items [I] + "" ; } } IF ( "!" .equals (Result)) { Toast.makeText ( . the MainActivity the this , "has been selected [" + result + "]" , Toast.LENGTH_SHORT ) the .Show (); } } }); // add OK button Once builder.create () Show ();. } }); } }