复选框首选项和跳过活动

时间:2013-01-29 23:45:56

标签: android checkbox sharedpreferences android-linearlayout

我有一个对话框自定义布局,我已经包含了一些textview和图像,但是我有一个复选框,我希望能够在用户选中第一个初始启动器上的复选框时弹出此对话框。应用程序/活动。

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

AreYouEnlighten

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>

1 个答案:

答案 0 :(得分:0)

你的按钮有什么用?现在,它只保存当前的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();
        }
    });


    ...

}