0

こんにちは、アプリを開くたびにチェックボックスを同じ状態に保ちたい..「ja/nein」文字列でこれを取得します.文字列は、アプリケーションを閉じて再度開くと...しかし、私のチェックボックス.setchecked(true/false) 機能しません..助けてください

public void changeVisitStatus(){
    SharedPreferences visitStatus = mData.getVisitStatus();
    SharedPreferences.Editor editor = visitStatus.edit();

    if(visitStatus.getString(mData.getVisitKey(), "nein").equals("nein")){
        editor.putString(mData.mVisitKey, "ja");
        editor.commit();
        mGUI.mBtnVisit.setChecked(true);
    }
    else{
        editor.putString(mData.mVisitKey, "nein");
        editor.commit();
        mGUI.mBtnVisit.setChecked(false);
    }
    mGUI.getVisitStatus().setText(visitStatus.getString(mData.mVisitKey, "Nein"));
}

編集:別の方法で試しました..もっと良いと思いましたが、うまくいきません..

public void changeVisitStatus(){
    SharedPreferences visitStatus = mData.getVisitStatus();
    SharedPreferences.Editor editor = visitStatus.edit();

    if(visitStatus.getString(mData.getVisitKey(), "nein").equals("nein")){
        editor.putString(mData.mVisitKey, "ja");
        editor.putBoolean("isChecked", true);
        editor.commit();

    }
    else{
        editor.putString(mData.mVisitKey, "nein");
        editor.putBoolean("isChecked", false);
        editor.commit();

    }
    mGUI.getVisitStatus().setText(visitStatus.getString(mData.mVisitKey, "Nein"));
}

これをアクティビティの onCreate(Bundle savedInstanceState) に入れます

mGUI.mBtnVisit.setChecked(mData.getVisitStatus().getBoolean("isChecked", false));
4

2 に答える 2

0

このようにしてみてください:

   public void putBooleanInPreferences(boolean isChecked,String key){
        SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, isChecked);
        editor.commit();        
    }
    public boolean getBooleanFromPreferences(String key){
        SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
        Boolean isChecked = sharedPreferences.getBoolean(key, false);
        return isChecked;       
    }

そして onCreate() で

 CheckBox checkBox = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        checkBox = (CheckBox) findViewById(R.id.my_check_box);
        boolean isChecked = getBooleanFromPreferences("isChecked");
        Log.i("start",""+isChecked);
        checkBox.setChecked(isChecked);
        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton view, boolean isChecked) {
                Log.i("boolean",""+isChecked);
                TestActivity.this.putBooleanInPreferences(isChecked,"isChecked");
            }
        });
    }

これがあなたを助けることを願っています!

于 2014-09-01T10:29:24.200 に答える
0

ステータスを変更するためのコードのみを表示しています。おそらく、チェック ボックスの OnClick リスナーから呼び出されます。

また、SharedPreferences からステータスのみを読み取り、それに応じてチェック ボックスの状態を設定するコードを追加する必要があります (同じコードである可能性がありますが、if 条件が否定されます)。

OnCreate イベントからそのコードを呼び出す必要があります。

public void setVisitStatus(){
    SharedPreferences visitStatus = mData.getVisitStatus();
    mGUI.getVisitStatus().setText(visitStatus.getString(mData.mVisitKey, "Nein"));
}
于 2014-09-01T10:43:14.363 に答える