Ale*_*Feh 20 android android-alertdialog
AlertDialog当我添加消息时,有人知道为什么不显示项目列表.setMessage()吗?将显示负面和正面按钮,但不会显示列表.当我删除所有行的行.setMessage().
这是我的代码:
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this.getActivity());
myAlertDialog.setTitle("Options");
myAlertDialog.setMessage("Choose a color.");
CharSequence[] items = {"RED", "BLUE", "GREEN" };
myAlertDialog.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do stuff
}
});
myAlertDialog.setNegativeButton("NO",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do stuff
}
});
myAlertDialog.setPositiveButton("YES",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do stuff
}
});
myAlertDialog.create();
myAlertDialog.show();
Run Code Online (Sandbox Code Playgroud)
AlertDialog通过检查mMessage您是否通过setMessage()is设置来动态创建其内容null。如果mMessage是NOT null(这意味着你叫setMessage()方法),然后设置其内容只 TextView显示消息和跳过 listview设置。如果您还没有调用setMessage(),那么它会删除TextView然后设置listview。
因此,要在对话框中显示消息和列表,我建议您使用以下解决方案,为 AlertDialog 设置自定义视图。
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Dialog title");
final List<String> items = new ArrayList<>();
items.add("Item 1");
items.add("Item 2");
items.add("Item 3");
final ArrayAdapter<String> arrayAdapterItems = new ArrayAdapter<String>(
getContext(), android.R.layout.simple_list_item_single_choice, items);
View content = getLayoutInflater().inflate(R.layout.list_msg_dialog, null);
//this is the TextView that displays your message
TextView tvMessage = content.findViewById(R.id.tv_msg);
tvMessage.setText("Dialog message!!!");
//this is the ListView that lists your items
final ListView lvItems = content.findViewById(R.id.lv_items);
lvItems.setAdapter(arrayAdapterItems);
lvItems.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
builder.setView(content);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//do something
Toast.makeText(MainActivity.this,
items.get(lvItems.getCheckedItemPosition()),
Toast.LENGTH_SHORT).show();
}
});
final AlertDialog dialog = builder.create();
lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//when you need to act on itemClick
}
});
dialog.show();
Run Code Online (Sandbox Code Playgroud)
这是list_msg_dialog.xml布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3dp">
<TextView
android:id="@+id/tv_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="15dp"
android:text="some text"
android:textAppearance="?android:attr/textAppearanceListItem"
android:textColor="@android:color/black"
android:textSize="18sp" />
<ListView
android:id="@+id/lv_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@null" /></LinearLayout>
Run Code Online (Sandbox Code Playgroud)
GrI*_*sHu -2
尝试在警报对话框中设置项目如下:
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this.getActivity());
myAlertDialog.setTitle("Options");
myAlertDialog.setMessage("Choose a color.");
myAlertDialog.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
CharSequence[] items = {"RED","BLUE","GREEN"};
myAlertDialog.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d, int choice) {
}
});
............
myAlertDialog.create();
myAlertDialog.show();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5243 次 |
| 最近记录: |