Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将按钮单击次数保存到首选项_Android_Onclick_Listener_Preferences - Fatal编程技术网

Android 将按钮单击次数保存到首选项

Android 将按钮单击次数保存到首选项,android,onclick,listener,preferences,Android,Onclick,Listener,Preferences,我正在编写一个android应用程序,我想保存用户在首选项中单击按钮的次数,然后在另一个类中检索该首选项。在这一点上,我完全是初学者,任何帮助都将不胜感激。保存上一个按钮状态的一种方法是使用Android中的共享首选项。共享首选项允许存储数据的键值对,以后可以轻松检索。Android中有一种数据访问机制。其他为SqlLite数据库和文件 // declare thsi class variable in class from where u will put the string u wanna

我正在编写一个android应用程序,我想保存用户在首选项中单击按钮的次数,然后在另一个类中检索该首选项。在这一点上,我完全是初学者,任何帮助都将不胜感激。

保存上一个按钮状态的一种方法是使用Android中的共享首选项。共享首选项允许存储数据的键值对,以后可以轻松检索。Android中有一种数据访问机制。其他为SqlLite数据库和文件

// declare thsi class variable in class from where u will put the string u wanna store in shared pref 

//class variables

    SharedPreferences pref;
    SharedPreferences.Editor editor;
-------------------

//in oncrete method
// declare this in oncreate method 

        pref = getSharedPreferences("testapp", MODE_PRIVATE);
        editor = pref.edit();

// the varibale u wanna put use the below statements 
// for string use putString
// for boolean as u need use putBoolean 
// have a look at the various option it offers..


editor.putString("selected", "nil");
editor.commit();


// here is the statement use this statement in class where u wanna retireve ur strings
// use getBoolean for Boolean variables 

pref.getString("selected", "nil")

// here in sceond parameter in above statement is : if the value u r requesting for that is specified in first parameter is not present then it will return the //value which is your second parameter..
关于股票优先权的Android文档

共享偏好视频

现在再回到你的问题上来。我曾经不得不保存选中按钮的状态。然后稍后再次访问它(这似乎与您想要做的事情相似)


以上是一个复选框。您必须根据需要保存的按钮信息对其进行调整。希望这能让您开始。

尝试以下方法:

Activity1.java

SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(Activity1.this);
SharedPreferences.Editor editor = app_preferences.edit();
int i=0;

    yourbutton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                                    i++;
                    editor.putInt("counter", i);
                    editor.commit();
                }
            });
Activity2.java

SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
String counter = app_preferences.getInt("counter", 0);
这样做

**Activity1.java**
------------------

SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
int myIntValue = sp.getInt("your_int_key",0);


        yourbutton.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                    editor.putInt("your_int_key",++myIntValue);
                                editor.commit();
                    }
                });

**Activity2.java**
-----------------

  SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
 int myIntValue = sp.getInt("your_int_key", 0);
**Activity1.java**
------------------

SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
int myIntValue = sp.getInt("your_int_key",0);


        yourbutton.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                    editor.putInt("your_int_key",++myIntValue);
                                editor.commit();
                    }
                });

**Activity2.java**
-----------------

  SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
 int myIntValue = sp.getInt("your_int_key", 0);