我想在SecondActivity中重用MainActivity中的共享首选项获取。 客户应该有一个URL并且它被记录在共享首选项中,我希望我们可以重复使用存储在我的SecondActivity中的这个将它放在webview中(例如http:// MySharedPreferenceURL / mypage< - mypage是静态的,并且是动态的MySharedPreferenceURL
我的MainActivity代码是:
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class Settings extends Activity implements OnClickListener {
CheckBox checkBox;
EditText editText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
checkBox = (CheckBox) findViewById(R.id.checkBox1);
editText = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
loadSavedPreferences();
}
private void loadSavedPreferences() {
SharedPreferences sharedPreferences = getSharedPreferences("Some name", 0);
Editor editor = sharedPreferences.edit();
boolean checkBoxValue = sharedPreferences.getBoolean("CheckBox_Value", false);
String name = sharedPreferences.getString("storedName", "YourName");
if (checkBoxValue) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
editText.setText(name);
}
private void savePreferences(String key, boolean value) {
SharedPreferences sharedPreferences = getSharedPreferences("Some name", 0);
Editor editor = sharedPreferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
private void savePreferences(String key, String value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
savePreferences("CheckBox_Value", checkBox.isChecked());
if (checkBox.isChecked())
savePreferences("storedName", editText.getText().toString());
finish();
}
}
编辑:
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class Settings extends Activity implements OnClickListener {
CheckBox checkBox;
EditText editText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
checkBox = (CheckBox) findViewById(R.id.checkBox1);
editText = (EditText) findViewById(R.id.editText1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
loadSavedPreferences();
}
private void loadSavedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
boolean checkBoxValue = sharedPreferences.getBoolean("CheckBox_Value", false);
String name = sharedPreferences.getString("storedName", "YourName");
if (checkBoxValue) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
editText.setText(name);
}
private void savePreferences(String key, boolean value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
private void savePreferences(String key, String value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
savePreferences("CheckBox_Value", checkBox.isChecked());
if (checkBox.isChecked())
savePreferences("storedName", editText.getText().toString());
finish();
}
}
而且,我不知道如何在我的SecondActivty中展示,你能帮我解释一下吗
答案 0 :(得分:0)
不要使用getDefaultSharedPreferences(this)
,为什么不尝试
SharedPreference sp = getSharedPreferences("Some name", 0);
Editor editor = sp.edit();´
(...)
"some name"
将是存储数据的文件的名称。 (如果不存在,它将自动创建)
在其他活动中,您只需致电SharedPreference
,指向同名。
<强>示例:强>
一项活动:
public class Activity1 extends Activity
{
(...)
SharedPreferences sp = getSharedPreferences("my_profile", 0);
Editor editor = sp.edit();
editor.putString("Name", "PedroHawk");
editor.putString("Hobby", "making samples on stackoverflow");
editor.commit();
(...)
}
其他活动:
public class Activity2 extends Activity
{
(...)
SharedPreferences sp = getSharedPreferences("my_profile", 0);
String myName = sp.getString("Name", "empty");
String myHobby = sp.getString("Hobby", "empty");
(...)
}