【问题标题】:Checkbox Preferences and Skipping Activities复选框首选项和跳过活动
【发布时间】:2013-01-29 23:45:55
【问题描述】:

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

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

你开悟了吗

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>

【问题讨论】:

  • 当用户禁用通知时要做什么或显示什么?
  • @ottel142 当前唯一发生的事情是复选框被启用,然后单击时,我的对话框仍然每次都填充。
  • @ottel142 我正在尝试使用此复选框为用户保存首选项,以便他们看不到此对话框活动。或者在初始安装时仅启动此活动 1 次。

标签: android checkbox sharedpreferences android-linearlayout


【解决方案1】:

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


    ...

}

【讨论】:

  • 我想做什么,打开应用程序时会弹出此对话框(Activity),然后用户可以单击按钮 a 或 b 来处理其他活动(a 和 b),复选框的目的是跳过第一个活动并直接进入活动 a。此对话框仅在首次启动时弹出以允许用户禁用它,然后在下次重新安装应用程序之前他们永远不会看到它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-07
  • 2011-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-03
  • 2012-10-27
相关资源
最近更新 更多