问题描述
目前,当用户打开我的应用时,会打开一个 AlertDialog
,询问他们是否要升级到专业版.
Currently when the user opens my app, an AlertDialog
opens, asking them if they would like to upgrade to the pro version.
我需要向 AlertDialog
添加一个 CheckBox
,这将使应用在用户打开应用时不再显示 AlertDialog
.
I need to add a CheckBox
to the AlertDialog
that will make the app no longer show the AlertDialog
when the user opens the app.
这是我现在为 AlertDialog
准备的:
Here is what I have for the AlertDialog
now:
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();
如何将 CheckBox
添加到 AlertDialog
中,使应用在用户打开应用时不再显示 AlertDialog
?
How do I add a CheckBox
to the AlertDialog
that will make the app no longer show the AlertDialog
when the user opens the app?
推荐答案
您必须在 AlertDialog.Builder
对象上使用方法 setView(View)
.这会将传入的 View
放在消息区域和按钮之间.只需使用 CheckBox
填充 View
并将其传入.这是一个示例:
You have to use the method setView(View)
on the AlertDialog.Builder
object. This will put the passed in View
between the message area and buttons. Simply inflate a View
with a CheckBox
and pass that in. Here's an example:
checkbox.xml
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>
活动中的代码
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();
这篇关于如何将复选框添加到警报对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!