0

我有一个对话框自定义布局,我包含了一些文本视图和图像,但是我有一个复选框,如果用户选中应用程序/活动的第一个初始启动器上的复选框,我希望能够弹出这个对话框.

我不知道从哪里获取我当前的代码,任何建议或更正都会非常有帮助和赞赏。

你开悟了吗

public class AreYouEnlighten extends Activity{

Button yes;
Button no;

public static final String PREFS_NAME = "MyPrefsFile1";
public CheckBox dontShowAgain;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_areyouenlighten);

    dontShowAgain = (CheckBox) findViewById(R.id.checkBox1);




    final Button yes = (Button) findViewById(R.id.continuebutton);
    yes.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click

            String checkBoxResult = "NOT checked";
            if (dontShowAgain.isChecked())
                checkBoxResult = "checked";
            SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("skipMessage", checkBoxResult);
            // Commit the edits!
            editor.commit();
            return;


        }
    });


    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    String skipMessage = settings.getString("skipMessage", "NOT checked");
    if (!skipMessage.equals("checked")) {
        // if (skipMessage !=("checked") )




    final Button no = (Button) findViewById(R.id.learnmore);
    no.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click


            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:3S6PTpCyW1I"));
         List<ResolveInfo> list = getPackageManager().queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
         if (list.size() == 0) {
          // default youtube app not present or doesn't conform to the standard we know
          // use our own activity
          i = new Intent(getApplicationContext(), AreYouEnlighten.class);
        //  i.putExtra("3S6PTpCyW1I", videoID);
         }
         startActivity(i);
        }
    });
}

activity_areyouenlighten.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="8dp"
    android:paddingTop="10dp"
    android:src="@drawable/logoheader" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="3dp"
    android:layout_weight="1"
    android:src="@color/dialog_text" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="15dp"
    android:text="Hello, World" />



<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:paddingRight="10dp"
    android:text="Disable This Notification" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:paddingTop="15dp" >

    <Button
        android:id="@+id/continuebutton"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="Continue" />

    <Button
        android:id="@+id/learnmore"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="Learn More" />
  </LinearLayout>

</LinearLayout>
4

1 回答 1

0

你的yes-Button有什么用?现在,它只保存当前的 CheckBox 状态。你最好在你的 CheckBox 上设置一个 OnCheckedChangeListener。

如果用户之前启用了跳过框,则将执行 if 子句。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_areyouenlighten);

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();

    if (settings.getBoolean("skipMessage", false) {
         // skipping message, so eg. start another activity here
         Intent in = ...
         startActivity(in);
         return;
    }

    CheckBox dontShowAgain = (CheckBox) findViewById(R.id.checkBox1);
    dontShowAgain.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            editor.putBoolean("skipMessage", isChecked);
            editor.commit();
        }
    });


    ...

}
于 2013-01-30T00:16:03.253 回答