今天,统计记录一下AlertDialog提示框的常见用法:
1、标准提示框
2、下拉列表提示框
3、下拉单选框提示框
4、下拉多选框提示框
5、自定义提示框(重要)
效果如下:
activity_alertdialog.xml 布局文件如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:id="@+id/alertdialog_btnStandardAlertDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="标准提示框"/> <Button android:id="@+id/alertdialog_btnListAlertDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="下拉列表提示框"/> <Button android:id="@+id/alertdialog_btnRadioButtonListAlertDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="下拉单选框提示框"/> <Button android:id="@+id/alertdialog_btnCheckBoxListAlertDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="下拉多选框提示框"/> <Button android:id="@+id/alertdialog_btnCustomAlertDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定义提示框"/> </LinearLayout>
自定义提示框布局文件item_customalertdialog.xml如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp" xmlns:android="http://schemas.android.com/apk/res/android"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="请输入用户名" android:inputType="textPersonName" android:textSize="14dp" android:layout_marginBottom="5dp"/> <EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="请输入密码" android:textSize="14dp" android:inputType="textPassword" /> </LinearLayout>
AlertDialogDemo.java的代码如下:
public class AlertDialogDemo extends AppCompatActivity implements View.OnClickListener { public String[] cities = {"广州", "上海", "北京", "香港", "澳门"}; public String toastText = ""; public HashSet<String> hashSet; public Button btnStandardAlertDialog, btnListAlertDialog, btnRadioButtonListAlertDialog , btnCheckBoxListAlertDialog, btnCustomAlertDialog; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alertdialog); componentInit(); componentSetOnClickListener(); } public void componentInit(){ btnStandardAlertDialog = findViewById(R.id.alertdialog_btnStandardAlertDialog); btnListAlertDialog = findViewById(R.id.alertdialog_btnListAlertDialog); btnRadioButtonListAlertDialog = findViewById(R.id.alertdialog_btnRadioButtonListAlertDialog); btnCheckBoxListAlertDialog = findViewById(R.id.alertdialog_btnCheckBoxListAlertDialog); btnCustomAlertDialog = findViewById(R.id.alertdialog_btnCustomAlertDialog); } public void componentSetOnClickListener(){ btnStandardAlertDialog.setOnClickListener(this); btnListAlertDialog.setOnClickListener(this); btnRadioButtonListAlertDialog.setOnClickListener(this); btnCheckBoxListAlertDialog.setOnClickListener(this); btnCustomAlertDialog.setOnClickListener(this); } @Override protected void onDestroy() { super.onDestroy(); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.alertdialog_btnStandardAlertDialog:{ AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogDemo.this); builder.setTitle("提示"); builder.setIcon(R.drawable.ic_launcher_foreground); builder.setMessage("这是一个标准提示框!"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.show(); break; } case R.id.alertdialog_btnListAlertDialog:{ AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogDemo.this); builder.setTitle("提示"); builder.setIcon(R.mipmap.ic_launcher); builder.setItems(cities, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogDemo.this, cities[which], Toast.LENGTH_SHORT).show(); } }); builder.show(); break; } case R.id.alertdialog_btnRadioButtonListAlertDialog:{ AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogDemo.this); builder.setTitle("提示"); builder.setIcon(R.drawable.ic_launcher_foreground); builder.setSingleChoiceItems(cities, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { toastText = cities[which]; } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogDemo.this, "您选择了:" + toastText, Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.show(); break; } case R.id.alertdialog_btnCheckBoxListAlertDialog:{ hashSet = new HashSet<>(); AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogDemo.this); builder.setTitle("提示"); builder.setIcon(R.drawable.ic_launcher_foreground); builder.setMultiChoiceItems(cities, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked){ hashSet.add(cities[which]); } else { hashSet.remove(cities[which]); } } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { StringBuilder stringBuilder = new StringBuilder(); for (String str : hashSet){ stringBuilder.append(str); } Toast.makeText(AlertDialogDemo.this, "您选择了:" + String.valueOf(stringBuilder), Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.show(); break; } case R.id.alertdialog_btnCustomAlertDialog:{ AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogDemo.this); builder.setTitle("提示"); builder.setIcon(R.drawable.ic_launcher_foreground); View view = LayoutInflater.from(this).inflate(R.layout.item_customalertdialog, null); builder.setView(view); final EditText etUserName = view.findViewById(R.id.editText); final EditText etPassword = view.findViewById(R.id.editText2); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String userName = etUserName.getText().toString().trim(); String Password = etPassword.getText().toString().trim(); Toast.makeText(AlertDialogDemo.this, "用户名:" + userName + "\r\n" + "密码:" + Password, Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.show(); break; } } } }