将按钮的状态保存在android中 [英] save the state of button in android

查看:111
本文介绍了将按钮的状态保存在android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在相机应用程序上工作时,我有两个imageview,一个是自动,第二个是pro,我想当我单击auto auto并更改图像图标时,并且当我单击pro auto auto view取消选择并选择pro view时

am working on camera application i have two imageview one is auto and second is pro i want when i click on auto auto is selected and image icon changes and when i click on pro automatically auto view deselected and pro view is selected

autobtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            autobtn.setImageResource(R.drawable.autoactive);
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ModeActivity.this);
            SharedPreferences.Editor edit = sharedPreferences.edit();
            edit.putString("focus_value", "focus_mode_auto");
            Intent it = new Intent(ModeActivity.this, MainActivity.class);
            startActivity(it);
            edit.commit();
            //MainActivity.grid.setVisibility(View.VISIBLE);
        }
    });

在第二张专业照片中选择了

in the second picture pro is selected

推荐答案

您可以像这样使用SharedPreferences.

You can use the SharedPreferences like this.

SharedPreferences sharedPreferences; 

sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ModeActivity.this);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putBoolean("focus_value", false);
        edit.putBoolean("auto_value", false); 
        edit.commit();

在您的按钮事件中,

autobtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        boolean auto = edit.getBoolean("auto_value", false);
        boolean pro = edit.getBoolean("pro_value", false);

        if(!auto){
            edit.putBoolean("auto_value", true);
            autobtn.setImageResource(R.drawable.autoactive);
            edit.putBoolean("pro_value",false);
            probtn.setImageResource(R.drawable.xxxxx);
        }

        Intent it = new Intent(ModeActivity.this, MainActivity.class);
        startActivity(it);

    }
});

这篇关于将按钮的状态保存在android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆