我正在编写一个 android 应用程序,我想保存用户单击首选项中的按钮的次数,然后在另一个类中检索该首选项。在这一点上,我是一个完整的初学者,任何帮助将不胜感激。
4 回答
            1        
        
		
试试下面的东西:
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);
于 2013-10-16T04:20:59.527   回答
    
    
            1        
        
		
这样做。。
**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);
于 2013-10-16T04:56:22.397   回答
    
    
            0        
        
		
// 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..
于 2013-10-16T04:13:03.667   回答
    
    
            0        
        
		
保存前一个按钮状态的一种方法是使用 Android 中的共享首选项。共享首选项允许存储数据的键值对,以便以后轻松检索。Android 中有一种数据访问机制。其他是 SqlLite 数据库和文件。
关于共享偏好的 Android 文档
关于共享偏好的视频
现在再次回到您的问题。我曾经不得不保存 checkbutton 的状态。然后稍后再次访问它(这似乎也与您想要做的类似)
    Part 1 Accessing Share preference Values : 
        public static final String PREFS_FILE = "MyPreferences";
        public static final String PREFS_NAME = "USER_NAME";
        public static final String PREFS_CHOICE = "USER_CHOICE";
        SharedPreferences sp;
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            chkChoice = (CheckBox)findViewById(R.id.chkChoice);
            btnMain = (Button)findViewById(R.id.btnMain);
            btnMain.setOnClickListener(this);
            // Here i access the shared preference file .
            sp = this.getSharedPreferences(PREFS_FILE, MODE_PRIVATE);
            // If i have a preference for my checkbox saved then load it else FALSE(unchecked) 
            chkChoice.setChecked(sp.getBoolean(PREFS_CHOICE, false));
        }
   Part 2 Setting Share preference from your activity :
    sp = this.getSharedPreferences(PREFS_FILE, MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    editor.putString(PREFS_NAME, txtName.getText().toString());
    editor.putBoolean(PREFS_CHOICE, chkChoice.isChecked());
    editor.commit();
    // Close the activity to show that the data is still saved
    finish();
以上是一个复选框。您必须根据您要保存的按钮信息类型对其进行调整。希望这能让你开始。
于 2013-10-16T04:19:19.507   回答