在上一篇文章Android中带有SingleItemSelection的Alert对话框中,我们看到了如何为单个项目选择构建警报对话框。在本文中,已经讨论了如何构建具有多个项目选择的警报对话框。当用户想要一次选择多个项目时,将使用“多个项目选择”对话框。查看下图,以区分“单项选择”和“多项选择”警告对话框。
实现具有多个项目选择的警报对话框的步骤
步骤1:创建一个空的活动项目
- 创建一个空的活动Android Studio项目。并选择Java作为编程语言。
- 要创建一个空的活动Android Studio项目,请参考Android |如何在Android Studio中创建/启动新项目?
步骤2:使用activity_main.xml
- 主要布局包括一个简单的Button和一个TextView小部件。实现TextView可以预览列表中的选定项目。
- 调用以下代码以构建UI:
XML
Java
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// UI widgets button and
Button bOpenAlertDialog = findViewById(R.id.openAlertDialogButton);
final TextView tvSelectedItemsPreview = findViewById(R.id.selectedItemPreview);
// initialise the list items for the alert dialog
final String[] listItems = new String[]{"C", "C++", "JAVA", "PYTHON"};
final boolean[] checkedItems = new boolean[listItems.length];
// copy the items from the main list to the selected item list
// for the preview if the item is checked then only the item
// should be displayed for the user
final List selectedItems = Arrays.asList(listItems);
// handle the Open Alert Dialog button
bOpenAlertDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// initially set the null for the text preview
tvSelectedItemsPreview.setText(null);
// initialise the alert dialog builder
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// set the title for the alert dialog
builder.setTitle("Choose Items");
// set the icon for the alert dialog
builder.setIcon(R.drawable.image_logo);
// now this is the function which sets the alert dialog for multiple item selection ready
builder.setMultiChoiceItems(listItems, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checkedItems[which] = isChecked;
String currentItem = selectedItems.get(which);
}
});
// alert dialog shouldn't be cancellable
builder.setCancelable(false);
// handle the positive button of the dialog
builder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < checkedItems.length; i++) {
if (checkedItems[i]) {
tvSelectedItemsPreview.setText(tvSelectedItemsPreview.getText() + selectedItems.get(i) + ", ");
}
}
}
});
// handle the negative button of the alert dialog
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
// handle the neutral button of the dialog to clear
// the selected items boolean checkedItem
builder.setNeutralButton("CLEAR ALL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < checkedItems.length; i++) {
checkedItems[i] = false;
}
}
});
// create the builder
builder.create();
// create the alert dialog with the
// alert dialog builder instance
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
}
}
输出界面:
步骤3:使用MainActivity。 Java文件
- 有必要了解多项目选择警报对话框的各个部分。查看下图以了解各部分:
- 下面讨论需要为警报对话框选择多个项目的函数。
Syntax:
setMultiChoiceItems(listItems, checkedItems, new DialogInterface.OnMultiChoiceClickListener()
Parameters:
listItems: are the items to be deisplayed on the alert dialog.
checkedItems: it is the boolean array which maintains the selected valus as true, and unselected values as false.
DialogInterface.OnMultiChoiceClickListener(): which is callback when change in the selection of items takes place.
- 调用以下代码实现事物。添加了注释以便更好地理解。
Java
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// UI widgets button and
Button bOpenAlertDialog = findViewById(R.id.openAlertDialogButton);
final TextView tvSelectedItemsPreview = findViewById(R.id.selectedItemPreview);
// initialise the list items for the alert dialog
final String[] listItems = new String[]{"C", "C++", "JAVA", "PYTHON"};
final boolean[] checkedItems = new boolean[listItems.length];
// copy the items from the main list to the selected item list
// for the preview if the item is checked then only the item
// should be displayed for the user
final List selectedItems = Arrays.asList(listItems);
// handle the Open Alert Dialog button
bOpenAlertDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// initially set the null for the text preview
tvSelectedItemsPreview.setText(null);
// initialise the alert dialog builder
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// set the title for the alert dialog
builder.setTitle("Choose Items");
// set the icon for the alert dialog
builder.setIcon(R.drawable.image_logo);
// now this is the function which sets the alert dialog for multiple item selection ready
builder.setMultiChoiceItems(listItems, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checkedItems[which] = isChecked;
String currentItem = selectedItems.get(which);
}
});
// alert dialog shouldn't be cancellable
builder.setCancelable(false);
// handle the positive button of the dialog
builder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < checkedItems.length; i++) {
if (checkedItems[i]) {
tvSelectedItemsPreview.setText(tvSelectedItemsPreview.getText() + selectedItems.get(i) + ", ");
}
}
}
});
// handle the negative button of the alert dialog
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
// handle the neutral button of the dialog to clear
// the selected items boolean checkedItem
builder.setNeutralButton("CLEAR ALL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < checkedItems.length; i++) {
checkedItems[i] = false;
}
}
});
// create the builder
builder.create();
// create the alert dialog with the
// alert dialog builder instance
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
}
}
输出:在模拟器上运行
想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处,前往由我们的专家精心策划的指南,以使您立即做好行业准备!