【问题标题】:Android : if else statement not working on CheckboxAndroid:if else语句不适用于复选框
【发布时间】:2014-05-15 04:35:01
【问题描述】:

我有一个 CheckBoxButton 的活动。我使用SharedPreferences 来保存复选框值。

按钮保存复选框的值并打开另一个活动。复选框的文字会更好地解释它。

check.setText("Show This Page On Start");

如果选中复选框,则此活动将在开始时显示,否则将打开另一个活动。

当我使用它时,应用程序强制关闭。

if (sharedPreferences.getBoolean("check_box_value", "false"))

    {  //launch another activity
    }
    else
    {
// do nothing
}

【问题讨论】:

  • 具体的问题是什么?您正在经历什么让您认为 if-else 不起作用?
  • 你为什么给 false 作为字符串..?
  • 你能发布错误/logcat吗?
  • 我编辑了我的问题,请回头看看。
  • 检查复选框时保存布尔变量。

标签: android checkbox sharedpreferences


【解决方案1】:
// Try this way,hope this will help you...

**activity_main.xml**

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <CheckBox
        android:id="@+id/chkShowThisPage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show This Page On Start"
        android:checked="true"/>

    <Button
        android:id="@+id/btnGetCheckBoxValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout

**MainActivity.java**

public class MainActivity extends Activity{

    private CheckBox  chkShowThisPage;
    private Button btnGetCheckBoxValue;
    SharedPreferences sharedPreferences;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        chkShowThisPage = (CheckBox) findViewById(R.id.chkShowThisPage);
        btnGetCheckBoxValue = (Button) findViewById(R.id.btnGetCheckBoxValue);
         sharedPreferences = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);
        if(!sharedPreferences.getBoolean("showThisPage",true)){
            Intent intent = new Intent(this,YourAnotherActivity.class);
            startActivity(intent);
            finish();
        }


        btnGetCheckBoxValue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean("showThisPage",chkShowThisPage.isChecked());
                editor.commit();
                Intent intent = new Intent(MainActivity.this,YourAnotherActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
}

【讨论】:

  • 这很有效,感谢 Haresh 的帮助。
  • @RashedLone,很高兴为您提供帮助,如果对您有用,请点赞,谢谢。
【解决方案2】:
if (sharedPreferences.getBoolean("check_box_value", false))

    {  //launch another activity
    }
    else
    {
// do nothing
}

这是访问布尔值的正确方法

【讨论】:

    【解决方案3】:

    您在 getBoolean 中传递字符串(请参阅代码中的“false”。这意味着这是您需要在没有“”的情况下传递 false 的字符串)。这不是正确的方法。试试这种方式

    boolean isChecked = sharedPreferences.getBoolean("check_box_value", false)
    
    if (isChecked)
    { 
         //launch another activity
    }
    else
    {
       // do nothing
    }
    

    【讨论】:

      【解决方案4】:

      如果未设置,您将传递一个字符串作为 SharedPreference 使用的默认值,这可能会导致您的应用使用 FC。

      尝试编写您的条件,默认传递一个布尔值:

       if (sharedPreferences.getBoolean("check_box_value", false)
           {...}
      

      【讨论】:

      • 是的,我这里弄错了,我的代码中实际上是false,而不是"false"
      • 请分享有关您遇到的错误的 logcat 输出,以便我们更深入地了解您的问题。
      • 如果条件正确,则应用程序会尝试启动一项或另一项活动。你有没有在你的 androidManifest.xml 中声明它们?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-26
      • 1970-01-01
      • 2013-09-20
      相关资源
      最近更新 更多