如何保存onClickListener的状态呢? [英] How to save state of onClickListener?

查看:158
本文介绍了如何保存onClickListener的状态呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现几个onClickListeners程序。因此,作为用户过程通过按钮的点击。反正是有保存它的onclick监听用户是在他们离开该应用程序或被毁之前?

I have a program that implements several onClickListeners. So as the user progresses through the button clicks. Is there anyway to save which onClick listener the user was on before they left the application or is was destroyed?

推荐答案

使用共享preference实现这一目标。存储每当你点击任何按钮,该按钮的名称和它的值。

Use sharedpreference to achieve this. store the button name and its value whenever you click on any button.

例如

SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);   

        passwordInString = password.getText().toString();
        userNameInString = username.getText().toString();

        getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
        .edit()
        .putString(PREFS_USERNAME, passwordInString)
        .putString(PREFS_PASSWORD, userNameInString)
        .commit();

在OnCreate中()总是用下面的code得到按钮的状态 样品例如

and in oncreate() always get the state of button using the following code Sample example

String usernameName = pref.getString(PREFS_USERNAME, "");
    String upassWord = pref.getString(PREFS_PASSWORD, "");

根据不同的值,你可以设置按钮的状态

depending on the value you can set the state of button

这篇关于如何保存onClickListener的状态呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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