我正在尝试创建一个Dialog来在我的应用程序中显示一条介绍消息,并在其下面显示“不再显示此内容”CheckBox。 我不知道如何制作代码。
<的 MainActivity.java >
@Override
protected void onStart()
{
super.onStart();
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Warning!");
Button button = (Button) dialog.findViewById(R.id.okay);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
这是我的java代码
这是我的layout.xml代码
<的 dialog.xml >
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10.0dip" >
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="@string/brick_warning"
android:textSize="15dp" />
</RelativeLayout>
<CheckBox
android:id="@+id/dont_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dont_show"
android:textSize="13dp" />
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/okay"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/dialog_continue" />
</LinearLayout>
</LinearLayout>
度过了美好的一天:)
答案 0 :(得分:1)
对不起迟到的回答,但在你的问题中 您可以使用共享偏好轻松解决它 继承人link:
和代码:
AlertDialog.Builder adb= new
AlertDialog.Builder(this);
LayoutInflater adbInflater =
LayoutInflater.from(this);
View eulaLayout = adbInflater.inflate
(R.layout.activity_main, null);
check = (CheckBox)
eulaLayout.findViewById(R.id.skip);
adb.setView(eulaLayout);
adb.setTitle("Example:");
adb.setMessage(Html.fromHtml("Type your
text here: "));
adb.setPositiveButton("Ok", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface
dialog, int which) {
String checkBoxResult = "NOT
checked";
if (check.isChecked())
checkBoxResult = "checked";
SharedPreferences settings =
getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor
editor = settings.edit();
editor.putString("noshow",
checkBoxResult);
// Commit the edits!
// sunnovalthesis();
editor.commit();
return;
} });
adb.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface
dialog, int which) {
String checkBoxResult = "NOT
checked";
if (check.isChecked())
checkBoxResult = "checked";
SharedPreferences settings =
getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor =
settings.edit();
editor.putString("noshow",
checkBoxResult);
// Commit the edits!
// sunnovalthesis();
editor.commit();
return;
} });
SharedPreferences settings =
getSharedPreferences(PREFS_NAME, 0);
String noshow = settings.getString
("noshow", "NOT checked");
if (noshow != "checked" ) adb.show();
快乐的编码!