保存`editText`可见性

时间:2015-06-23 06:38:25

标签: android android-edittext sharedpreferences

我有几个editTexts设置为button点击后可见,我还有另一个button,会将editText设置为不可见。当我自己保存可见性代码时,代码工作正常但是当我添加代码以将editText保存到invisibe时,我遇到了问题。我做错了什么?

    public void EditTextVisible() {
    visibility++;
    if(e1.getVisibility() == View.INVISIBLE){
        e1.setVisibility(View.VISIBLE);
    } else if(e2.getVisibility() == View.INVISIBLE){
        e2.setVisibility(View.VISIBLE);
    }
}
public void invisible(){
    invisible++;
    if(e2.getVisibility() == View.VISIBLE)
    {
        e2.setVisibility(View.INVISIBLE);
    }
    else if(e1.getVisibility() == View.VISIBLE){
        e1.setVisibility(View.INVISIBLE);
    }
}
@Override
  protected void onPause() {
    super.onPause();

    SharedPreferences.Editor editor = getPreferences(0).edit();

    editor.putInt(key1, visibility);//This code is working by itself.

    editor.putInt(key2, invisible); //When I add this code to save the editText to invisible nothing is being saved on exit.

    editor.commit();

}
@Override
protected void onResume() {
    super.onResume();
    SharedPreferences prefs = getPreferences(0);
        int val = prefs.getInt(key1, 0);
        for (int i = 0; i < val; i++)
            EditTextVisible();

        prefs.getInt(key2, 0);
        int inv = prefs.getInt(key2, 1);
        for (int a = 0; a < inv; a++)
              invisible();

    }
}

2 个答案:

答案 0 :(得分:0)

首先你的问题不明确。但这里有些不对劲

int inv = prefs.getInt(key 2, 1);

应该是

int inv = prefs.getInt(key2, 1);

答案 1 :(得分:0)

问题出在您的onResume()

@Override
protected void onResume() {
    super.onResume();
    SharedPreferences prefs = getPreferences(0);
        int val = prefs.getInt(key1, 0);
        for (int i = 0; i < val; i++)
            EditTextVisible();


        int inv = prefs.getInt(key2, 0);
        for (int a = 0; a < inv; a++)
              invisible();

    }

请检查此代码。