2

我正在尝试创建一个对话框以在我的应用程序中显示介绍消息,并在其下方带有“不再显示”复选框。我不知道如何编写代码。

< 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 代码

<对话框.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>

祝你有美好的一天 :)

4

1 回答 1

1

海对不起迟到的答案,但在您的问题中
,您可以使用共享首选项轻松解决它
链接如下:

和代码:

          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();

快乐编码!

于 2015-10-18T14:07:02.733 回答