Luc*_*man 40 checkbox android android-alertdialog
目前,当用户打开我的应用程序时,会AlertDialog打开询问他们是否要升级到专业版.我需要添加一个CheckBox,AlertDialog这将使应用程序不再显示AlertDialog用户打开应用程序时.
这就是我AlertDialog现在所拥有的:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(" MY_TEXT");
builder.setMessage(" MY_TEXT ")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Uri uri = Uri.parse("market://details?id=MY_APP_PACKAGE");
Intent intent = new Intent (Intent.ACTION_VIEW, uri);
startActivity(intent); }
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).show();
Run Code Online (Sandbox Code Playgroud)
如果有人能告诉我如何将添加CheckBox到AlertDialog,这将使应用不再显示AlertDialog当用户打开应用程序,这将是巨大的.提前致谢!
Jas*_*son 88
您必须setView(View)在AlertDialog.Builder对象上使用该方法.这将传入View消息区域和按钮之间.只需View用a 充气CheckBox并传入即可.这是一个例子:
checkbox.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/checkbox"
style="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
您的活动中的代码
View checkBoxView = View.inflate(this, R.layout.checkbox, null);
CheckBox checkBox = (CheckBox) checkBoxView.findViewById(R.id.checkbox);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Save to shared preferences
}
});
checkBox.setText("Text to the right of the check box.");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(" MY_TEXT");
builder.setMessage(" MY_TEXT ")
.setView(checkBoxView)
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Uri uri = Uri.parse("market://details?id=MY_APP_PACKAGE");
Intent intent = new Intent (Intent.ACTION_VIEW, uri);
startActivity(intent); }
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).show();
Run Code Online (Sandbox Code Playgroud)
Sur*_*gch 13
The way to make a checkbox list is to use setMultiChoiceItems in the AlertDialog.
// Set up the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Choose some animals");
// Add a checkbox list
String[] animals = {"horse", "cow", "camel", "sheep", "goat"};
boolean[] checkedItems = {true, false, false, true, false};
builder.setMultiChoiceItems(animals, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// The user checked or unchecked a box
}
});
// Add OK and Cancel buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// The user clicked OK
}
});
builder.setNegativeButton("Cancel", null);
// Create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
Run Code Online (Sandbox Code Playgroud)
在这里,我硬编码了列表中的哪些项目已经被检查过。您更有可能希望在ArrayList<Integer>. 有关更多详细信息,请参阅文档示例。您还可以将选中的项目设置为null如果您始终希望所有内容都未选中。
对于context,this如果您在活动中,则可以使用。
我更完整的答案在这里。
// Set up the alert builder
val builder = AlertDialog.Builder(context)
builder.setTitle("Choose some animals")
// Add a checkbox list
val animals = arrayOf("horse", "cow", "camel", "sheep", "goat")
val checkedItems = booleanArrayOf(true, false, false, true, false)
builder.setMultiChoiceItems(animals, checkedItems) { dialog, which, isChecked ->
// The user checked or unchecked a box
}
// Add OK and Cancel buttons
builder.setPositiveButton("OK") { dialog, which ->
// The user clicked OK
}
builder.setNegativeButton("Cancel", null)
// Create and show the alert dialog
val dialog = builder.create()
dialog.show()
Run Code Online (Sandbox Code Playgroud)
小智 6
您可以使用只有一项的多选列表:
final boolean[] checked = new boolean[] {false};
builder.setMultiChoiceItems(new String[]{"Remember decision"}, checked, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
checked[i] = b;
}
});
Run Code Online (Sandbox Code Playgroud)
然后在OnClick()警报对话框按钮中,您可以检查 的值checked[0]并将该值保存在您的应用程序中Sharedpreferences:
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if(checked[0]){
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("preferences_never_buy_pro", true);
editor.apply();
}
dialog.cancel();
}
});
Run Code Online (Sandbox Code Playgroud)
通过此首选项,您可以决定将来是否应再次显示该对话框。
| 归档时间: |
|
| 查看次数: |
39079 次 |
| 最近记录: |