End*_*ame 31 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 = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
String current_iso = sharedPreferences.getString(MainActivity.getISOPreferenceKey(), "auto");
}
Run Code Online (Sandbox Code Playgroud)
此方法在onClick()上显示ImageButton:
android:onClick="getISO"
Run Code Online (Sandbox Code Playgroud)
但我需要在带有单选按钮的对话框中显示此列表.可能已在对话框中选择了首选项值.是否可能?
小智 64
最好的方式......
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...
}
Run Code Online (Sandbox Code Playgroud)
Rus*_*tam 38
showRadioButtonDialog()从按钮拨打电话.
这只是一个例子:
private void showRadioButtonDialog() {
// custom dialog
final Dialog dialog = new Dialog(mActivity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.radiobutton_dialog);
List<String> stringList=new ArrayList<>(); // here is list
for(int i=0;i<5;i++) {
stringList.add("RadioButton " + (i + 1));
}
RadioGroup rg = (RadioGroup) dialog.findViewById(R.id.radio_group);
for(int i=0;i<stringList.size();i++){
RadioButton rb=new RadioButton(mActivity); // dynamically creating RadioButton and adding to RadioGroup.
rb.setText(stringList.get(i));
rg.addView(rb);
}
dialog.show();
}
Run Code Online (Sandbox Code Playgroud)
您的布局视图:radiobutton_dialog.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">
<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>
Run Code Online (Sandbox Code Playgroud)
注意:您可以自定义对话框视图(如设置标题,消息等)
编辑:
要检索所选的值,RadioButton您必须setOnCheckedChangeListener为您的RadioGroupas 实现侦听器:
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());
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
干净的方式是这样的:
http://developer.android.com/guide/topics/ui/dialogs.html
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();
Run Code Online (Sandbox Code Playgroud)
无需自定义视图.
这对我有用:
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();
Run Code Online (Sandbox Code Playgroud)
科特林版本:
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();
}
Run Code Online (Sandbox Code Playgroud)