保存按钮启用禁用状态并在创建时获取最后一个状态

时间:2017-10-23 08:35:30

标签: sharedpreferences state

我是android的新手,我正在尝试开发一些应用程序。但是过去两周我一直坚持使用这段代码。简而言之,我在布局中有两个按钮。这些按钮是添加和删除按钮。如果我单击添加按钮,将禁用添加并启用删除。或者如果我点击删除,删除将被禁用添加将被启用。我想保存这两个按钮的状态(单击哪个按钮)并在创建时获得最后一次单击状态。但问题是它不起作用。当我打开应用程序时,两个按钮都显示禁用状态。或者也不保存。从不工作。

bool edit(int cmdchar, bool startln, long count)
{
  if (curbuf->terminal) {
    if (ex_normal_busy) {
      // Do not enter terminal mode from ex_normal(), which would cause havoc
      // (such as terminal-mode recursiveness). Instead set a flag to force-set
      // the value of `restart_edit` before `ex_normal` returns.
      restart_edit = 'i';
      force_restart_edit = true;
    } else {
      terminal_enter();
    }
    return false;
  }
  ...

    

public class UserVideoInfoActivity2 extends AppCompatActivity {


    SharedPreferences.Editor editor;
    SharedPreferences preferences;
    SharedPreferences.Editor editor2;
    SharedPreferences preferences2;

    Button buttonadd;
    Button buttondelete;


    private static final String ADD = "ADD_KEY";
    private final static String DELETE = "DELETE_KEY";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_video_info_layout_2);

        buttonadd = (Button) findViewById(R.id.buttonaddlayout);
        buttondelete = (Button) findViewById(R.id.buttondeletelayout);




        preferences = getSharedPreferences(ADD, Context.MODE_PRIVATE);
        preferences2 = getSharedPreferences(DELETE, Context.MODE_PRIVATE);

        buttonadd.setEnabled(GetState());

        buttondelete.setEnabled(GetState2());



        buttonadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                buttonadd.setEnabled(false);
                buttondelete.setEnabled(true);

                SaveState(buttonadd.isEnabled());


            }
        });

        buttondelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                buttonadd.setEnabled(true);
                buttondelete.setEnabled(false);

                SaveState2(buttondelete.isEnabled());


            }
        });


    }

    private void SaveState(boolean isChecked) {
        editor = preferences.edit();
        editor.putBoolean(ADD, isChecked);
        editor.commit();
    }

    private void SaveState2(boolean isChecked2) {
        editor2 = preferences2.edit();
        editor2.putBoolean(DELETE, isChecked2);
        editor2.commit();
    }


    public boolean GetState() {
        return preferences.getBoolean(ADD, false );

    }


    public boolean GetState2() {
        return preferences2.getBoolean(DELETE, false);
    }

}

0 个答案:

没有答案