Java 创建一个带有单选按钮列表的自定义对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32520850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a custom dialog with radio buttons list
提问by End.Game
I've got a method in which i have a list of values:
我有一个方法,其中有一个值列表:
/**
* ISO
* */
public void getISO(View view) {
// Open dialog with radio buttons
List<String> supported_isos = preview.getSupportedISOs();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
String current_iso = sharedPreferences.getString(MainActivity.getISOPreferenceKey(), "auto");
}
This method is enjected on onClick() of a ImageButton
:
这个方法被注入到 a 的 onClick() 上ImageButton
:
android:onClick="getISO"
But i need to rapresent this list in a dialog with radio buttons. Possibly the preference values should be already selected in the dialog.. Is it possible?
但是我需要在带有单选按钮的对话框中显示此列表。可能已经在对话框中选择了首选项值.. 可能吗?
采纳答案by Rustam
Call showRadioButtonDialog()
from the button.
showRadioButtonDialog()
从按钮调用。
This is just an example:
这只是一个例子:
private void showRadioButtonDialog() {
// custom dialog
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_layout);
List<String> stringList=new ArrayList<>(); // here is list
for(int i=0;i<2;i++) {
if (i==0){
stringList.add("Number Mode");
}else {
stringList.add("Character Mode");
}
}
RadioGroup rg = (RadioGroup) dialog.findViewById(R.id.radioGroup);
for(int i=0;i<stringList.size();i++){
RadioButton rb=new RadioButton(this); // dynamically creating RadioButton and adding to RadioGroup.
rb.setText(stringList.get(i));
rg.addView(rb);
}
}
Your layout view might be:radiobutton_dialog.xml
您的布局视图可能是:radiobutton_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:orientation="vertical">
</RadioGroup>
</LinearLayout>
Note:you can customize your dialog view (like setting title, message etc.)
注意:您可以自定义对话框视图(如设置标题、消息等)
Edit:To retrieving value of the selected RadioButton
you have to implement setOnCheckedChangeListener
listener for your RadioGroup
as :
编辑:要检索选定的值,RadioButton
您必须setOnCheckedChangeListener
为您的RadioGroup
as实现侦听器:
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int childCount = group.getChildCount();
for (int x = 0; x < childCount; x++) {
RadioButton btn = (RadioButton) group.getChildAt(x);
if (btn.getId() == checkedId) {
Log.e("selected RadioButton->",btn.getText().toString());
}
}
}
});
回答by Sudheesh Mohan
Check this. This is the Custom row dialog_row.xml that you should use in CustomAdapter:
检查这个。这是您应该在 CustomAdapter 中使用的自定义行 dialog_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Then in onclick method:
然后在onclick方法中:
@Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_layout); //Your custom layout
dialog.setTitle("Title...");
Listview listview= (ListView) dialog.findViewById(R.id.listview);
CustomAdapter adapter=new CustomAdapter(context,your_list);
listview.setadapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Do something
}
});
dialog.show();
}
回答by JoxTraex
A clean way is like this:
一个干净的方法是这样的:
http://developer.android.com/guide/topics/ui/dialogs.html
http://developer.android.com/guide/topics/ui/dialogs.html
Excerpt from (Adding a persistent multiple-choice or single-choice list)
摘自(添加持久的多项选择或单项选择列表)
mSelectedItems = new ArrayList(); // Where we track the selected items
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Set the dialog title
builder.setTitle(R.string.pick_toppings)
// Specify the list array, the items to be selected by default (null for none),
// and the listener through which to receive callbacks when items are selected
.setMultiChoiceItems(R.array.toppings, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
mSelectedItems.add(which);
} else if (mSelectedItems.contains(which)) {
// Else, if the item is already in the array, remove it
mSelectedItems.remove(Integer.valueOf(which));
}
}
})
// Set the action buttons
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// User clicked OK, so save the mSelectedItems results somewhere
// or return them to the component that opened the dialog
...
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
...
}
});
return builder.create();
No custom view is necessary.
不需要自定义视图。
回答by Abhisek
best and easy way......
最好和最简单的方法......
void dialog(){
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
//alt_bld.setIcon(R.drawable.icon);
alt_bld.setTitle("Select a Group Name");
alt_bld.setSingleChoiceItems(grpname, -1, new DialogInterface
.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(),
"Group Name = "+grpname[item], Toast.LENGTH_SHORT).show();
dialog.dismiss();// dismiss the alertbox after chose option
}
});
AlertDialog alert = alt_bld.create();
alert.show();
///// grpname is a array where data is stored...
}
回答by saidsuchyar
when you want to show data from SQLIte database the
当你想显示来自 SQLIte 数据库的数据时
private void showRadioButtonDialog() {
// custom dialog
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.radiobutton_dialog);
List<String> stringList=new ArrayList<>(); // here is list
if (cursor.moveToFirst()) {
do {
String a=( cursor.getString(0).toString());
String b=(cursor.getString(1).toString());
String c=(cursor.getString(2).toString());
String d=(cursor.getString(3).toString());
stringList.add(d);
} while (cursor.moveToNext());
}
RadioGroup rg = (RadioGroup) dialog.findViewById(R.id.radio_group);
for(int i=0;i<stringList.size();i++) {
RadioButton rb=new RadioButton(this); // dynamically creating RadioButton and adding to RadioGroup.
rb.setText(stringList.get(i));
rg.addView(rb);
}
dialog.show();
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int childCount = group.getChildCount();
for (int x = 0; x < childCount; x++) {
RadioButton btn = (RadioButton) group.getChildAt(x);
if (btn.getId() == checkedId) {
Toast.makeText(getApplicationContext(), btn.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
}
});
}
回答by Akay
This worked for me:
这对我有用:
final CharSequence[] items = {"Option-1", "Option-2", "Option-3", "Option-4"};
AlertDialog.Builder builder = new AlertDialog.Builder(ShowDialog.this);
builder.setTitle("Alert Dialog with ListView and Radio button");
builder.setIcon(R.drawable.icon);
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(ShowDialog.this, "Success", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(ShowDialog.this, "Fail", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
回答by Hans
Kotlin version:
科特林版本:
fun dialog() {
val options = arrayOf("option1", "option2")
var selectedItem = 0
val builder = AlertDialog.Builder(this)
builder.setTitle("Select an option")
builder.setSingleChoiceItems(options
, 0, { dialogInterface: DialogInterface, item: Int ->
selectedItem = item
})
builder.setPositiveButton(R.string.accept, { dialogInterface: DialogInterface, p1: Int ->
Toast.makeText(getApplicationContext(),
"selected item = " + options[selectedItem], Toast.LENGTH_SHORT).show();
dialogInterface.dismiss()
})
builder.setNegativeButton(R.string.cancel, { dialogInterface: DialogInterface, p1: Int ->
dialogInterface.dismiss()
})
builder.create()
builder.show();
}