0

tabhost 中的共享首选项不适用于按钮按下状态。

我正在按下状态下更改按钮的背景。但是当我重新启动(关闭和打开)手机时,共享首选项不会保存状态。

从这里裁判

tabhost 中按钮按下状态的共享首选项在重新启动时不起作用

在调试时,isclick 在 if-else 条件下显示为 true。我不明白这个问题。

当我第一次运行时,我得到了带有突出显示背景的按钮,即使在重新启动时我也得到了相同的突出显示背景。

任何帮助总是受到赞赏。

private boolean isclick ;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    seatdirnbtn = (Button) findViewById(R.id.seatdirnbtn);
    prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);

    isclick = prefs.getBoolean("prefName", false);
    System.out.println("bool? " + isclick);

    if (isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
    } else if (!isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4);
    } 
}


@Override
public void onRestart() {
    super.onRestart();

    if (isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4);
    } else if (!isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
    }
}

@Override
public void onStop() {
    super.onStop();

    prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
    editor = prefs.edit();

    editor.putBoolean("prefName", true);
    editor.commit();
}

private View.OnClickListener listner1 = new View.OnClickListener() {
    public void onClick(View v) {
        if (isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);

            editor = prefs.edit();
            editor.clear();
            editor.putBoolean("prefName", true);
            editor.commit();
        } else if (!isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4);
            editor = prefs.edit();
            editor.clear();
            editor.putBoolean("prefName", false);
            editor.commit();
        }
        isclick = !isclick;
    }
};

编辑1:

    private boolean isclick  ;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);

    isclick = prefs.getBoolean("prefName", true);

    System.out.println("bool? " + isclick);
    Log.i(TAG, " prefname");

    if (isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
    } else if (!isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4);
    } 

         public void onPause() {
    super.onPause();
    prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
    // boolean isclick = false;
    editor = prefs.edit();
    // editor.clear();
    editor.putBoolean("prefName", false);
    Log.i(TAG, " prefname");
    editor.commit();
}

@Override
public void onRestart() {
    super.onRestart();
      prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
        isclick = prefs.getBoolean("prefName", !isclick);

}

@Override
public void onStop() {
    super.onStop();
    getApplicationContext().unbindService(this);

      prefs = this.getSharedPreferences(prefName, MODE_PRIVATE); // boolean
      isclick = false; editor = prefs.edit(); // editor.clear();
      editor.putBoolean("prefName", !isclick); 
      editor.commit();

      }

         private View.OnClickListener listner1 = new View.OnClickListener() {

    public void onClick(View v) {


        if (isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);

            editor = prefs.edit();
            editor.clear();
            editor.putBoolean("prefName", false);
            editor.commit();
            isclick = false;
        } else if (!isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4);
            editor = prefs.edit();
            editor.clear();
            editor.putBoolean("prefName", true);
            editor.commit();
            isclick = true;
        }

    }
};
4

1 回答 1

2

尝试使用此编辑后的解决方案

public class SharedprefsActivity extends Activity  {

    protected static final String TAG = "HvacActivity";
    /** Called when the activity is first created. */
    private Button seatdirnbtn;
    private SharedPreferences prefs;
    private String prefName = "MyPref";
    private SharedPreferences.Editor editor;
    private boolean isclick;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        seatdirnbtn = (Button) findViewById(R.id.seatdirnbtn);      
        seatdirnbtn.setOnClickListener(listner1);       
    }

    public void onResume() {
        super.onResume();
        getPrefAndButtonState();        
    }

    public void onPause() {
        super.onPause();
        setPrefAndButtonState();
    }

    @Override
    public void onRestart() {
        super.onRestart();
        getPrefAndButtonState();
    }

    @Override
    public void onStop() {
        super.onStop();
        setPrefAndButtonState();
    }

    public void getPrefAndButtonState(){
        prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
        isclick = prefs.getBoolean("prefName", false);
        System.out.println("bool? " + isclick);
        if (isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
        } else if (!isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4);
        }
    }

    public void setPrefAndButtonState(){
        editor = prefs.edit();
        editor.putBoolean("prefName", isclick);
        editor.commit();        
        getPrefAndButtonState();
    }

    private View.OnClickListener listner1 = new View.OnClickListener() {    
        public void onClick(View v) {   
            if (isclick) {
                isclick = false;
                setPrefAndButtonState();            
            } else if (!isclick) {
                isclick = true;
                setPrefAndButtonState();
            }   
        }
    };
}

我的测试很好,对我来说效果很好。

希望它可以帮助你。

谢谢。

于 2012-12-18T11:42:13.063 回答