对话框是程序运行中的弹出窗口,例如,当用户要删除一个联系方式时,会弹出一个对话框,让用户确认是否真的要删除。
Android系统提供了多种对话框:
package com.example.test_alertdialog;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void test1(View view){
//如何创建一个alertdialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示信息!");
builder.setMessage("我的第一个对话框!");
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="test1"
tools:layout_editor_absoluteX="44dp"
tools:layout_editor_absoluteY="52dp"
/>

包括不同性质的按钮:setPositiveButton、setNegativeButton、setNeutralButton
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void test1(View view){
//如何创建一个alertdialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示信息!");
builder.setMessage("我的第一个对话框!");
builder.setPositiveButton("非常满意", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "非常满意", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("不满意", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "不满意", Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("一般", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "一般", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}


public void test2(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示信息!");
builder.setItems(ss, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, ss[i], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:layout_below="@id/button1"
android:onClick="test2"
/>

解析:
builder.setSingleChoiceItems(ss, **0**, new DialogInterface.OnClickListener(){}
//其中的0,表示初始点开时选中数组中第0个
代码:
public void test3(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示信息!");
builder.setSingleChoiceItems(ss, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, ss[i], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"
android:layout_below="@id/button2"
android:onClick="test3"
/>

解析:
builder.setMultiChoiceItems(ss, null, new DialogInterface.OnMultiChoiceClickListener() {}
//其中的null,表示初始点开时不选中数组中的任意值
//null也可换成new boolean[]{true,false,true},true为选中,false为不选中
代码:
public void test4(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示信息!");
builder.setMultiChoiceItems(ss, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
Toast.makeText(MainActivity.this, ss[i], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4"
android:onClick="test4"
android:layout_below="@id/button3"/>
