Java 创建带有单选按钮列表的自定义对话框
我有一个方法,其中有一个值列表:Java 创建带有单选按钮列表的自定义对话框,java,android,android-listview,android-dialog,Java,Android,Android Listview,Android Dialog,我有一个方法,其中有一个值列表: /** * ISO * */ public void getISO(View view) { // Open dialog with radio buttons List<String> supported_isos = preview.getSupportedISOs(); SharedPreferences sharedPreferences = Prefere
/**
* 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");
}
但我需要在带有单选按钮的对话框中重新呈现此列表。可能已在对话框中选择了首选项值。。可以吗?通过按钮调用
showRadioButtonDialog()
这只是一个例子:
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);
}
}
注意:您可以自定义对话框视图(如设置标题、消息等)
编辑:
要检索所选的单选按钮的值
,您必须为您的放射组实现设置checkedChangeListener
监听器,如下所示:
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());
}
}
}
});
rg.setOnCheckedChangeListener(新的RadioGroup.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(RadioGroup组,int checkedId){
int childCount=group.getChildCount();
对于(int x=0;x”,btn.getText().toString());
}
}
}
});
检查此项。
这是应在CustomAdapter中使用的自定义行对话框\u 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>
然后在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();
}
@覆盖
公共void onClick(视图arg0){
//自定义对话框
最终对话框=新对话框(上下文);
dialog.setContentView(R.layout.custom_layout);//您的自定义布局
对话框。设置标题(“标题…”);
Listview=(Listview)dialog.findViewById(R.id.Listview);
CustomAdapter=新的CustomAdapter(上下文,您的_列表);
setadapter(适配器);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
//做点什么
}
});
dialog.show();
}
清洁的方法是这样的:
摘录自(添加持久的多选或单选列表)
了解
无需自定义视图。最佳且简单的方法
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...
}
当您想要显示来自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();
}
}
}
});
}
private void showRadioButtonDialog(){
//自定义对话框
最终对话框=新对话框(本);
对话框.requestWindowFeature(窗口.FEATURE\u无\u标题);
setContentView(R.layout.radiobutton_对话框);
List stringList=new ArrayList();//下面是列表
if(cursor.moveToFirst()){
做{
字符串a=(cursor.getString(0.toString());
字符串b=(cursor.getString(1.toString());
字符串c=(cursor.getString(2.toString());
字符串d=(cursor.getString(3.toString());
增加(d);
}while(cursor.moveToNext());
}
RadioGroup rg=(RadioGroup)dialog.findViewById(R.id.radio\u组);
对于(int i=0;i这对我来说很有效:
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();
Kotlin版本:
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();
}
这是可能的,一切都取决于你如何填充对话框。创建一个对话框并为其设置自定义视图。你能给我一个使用我的列表的示例吗?从这里开始@如果你正在寻找一个不需要自定义视图的更简单的解决方案,请查看我的解决方案,它将为你提供与标准android警报对话框相同的体验,如果是的话您不想做超出此范围的事情。我想说,对于他的用例来说,创建自定义对话框似乎有点过火。但肯定还是有可能的。检查编辑以检索所选单选按钮的值。这不是自定义的,它是带有单选项的默认警报对话框。如果您想要“确认”按钮,只需将其正常添加到带有egsetPositiveButton
的对话框,并从上述代码中删除dialog.disease()
。作者提到了自定义
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();
}
}
}
});
}
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();
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();
}