从另一个 activity 接收共享 SharedPreferences 值的值
receive value of shared SharedPreferences value from another activity
我有这段代码可以从 edittext 中获取 SharedPreferences 的值,然后将它发送给其他人 activity 不知何故我无法从其他人那里使用它 activity 我在代码中遗漏的任何建议
提前致谢
-那是我的第一个 activity,用于从 edittext 获取 SharedPreferences,我想将值从它发送到第二个 activity
public class NationalId extends Activity {
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "nameKey";
SharedPreferences sharedpreferences;
final Context context = this;
private Button button;
private TextView result;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_national_id);
// components from main.xml
button = (Button) findViewById(R.id.button1);
result = (TextView) findViewById(R.id.tv1);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
// add button listener
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// get prompts.xml view
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.prompts, null);
android.app.AlertDialog.Builder alertDialogBuilder = new android.app.AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
String n = userInput.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Name, n);
editor.commit();
// edit text
result.setText(userInput.getText());
Toast.makeText(NationalId.this,"saved:"+n,Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
android.app.AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
}
这里是我想要的第二个 activity 在
中接收值
public class receive extends Activity {
private Button button;
private EditText etPhoneno;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opreator_mobily);
button = (Button) findViewById(R.id.buttonCall);
etPhoneno = (EditText) findViewById(R.id.editText1);
SharedPreferences prefs = getSharedPreferences("Name",
MODE_PRIVATE);
final String value = prefs.getString("n", "0");
// add button listener\
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(receive.this,"saved:"+value,Toast.LENGTH_LONG).show();
}
});
}
}
在你的第二个 activity 中,你试图用不同的名称获取你的 sharedpreferences 实例,并用不同的键获取你保存的值。
替换为:
SharedPreferences prefs = getSharedPreferences("Name", MODE_PRIVATE);
final String value = prefs.getString("n", "0");
和
SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
final String value = prefs.getString("nameKey", "0");
替换下面这两行
SharedPreferences prefs = getSharedPreferences("Name",MODE_PRIVATE);
final String value = prefs.getString("n", "0");
这些行
SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
final String value = prefs.getString("nameKey", "0");
我有这段代码可以从 edittext 中获取 SharedPreferences 的值,然后将它发送给其他人 activity 不知何故我无法从其他人那里使用它 activity 我在代码中遗漏的任何建议 提前致谢
-那是我的第一个 activity,用于从 edittext 获取 SharedPreferences,我想将值从它发送到第二个 activity
public class NationalId extends Activity {
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "nameKey";
SharedPreferences sharedpreferences;
final Context context = this;
private Button button;
private TextView result;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_national_id);
// components from main.xml
button = (Button) findViewById(R.id.button1);
result = (TextView) findViewById(R.id.tv1);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
// add button listener
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// get prompts.xml view
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.prompts, null);
android.app.AlertDialog.Builder alertDialogBuilder = new android.app.AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
String n = userInput.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Name, n);
editor.commit();
// edit text
result.setText(userInput.getText());
Toast.makeText(NationalId.this,"saved:"+n,Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
android.app.AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
}
这里是我想要的第二个 activity 在
中接收值public class receive extends Activity {
private Button button;
private EditText etPhoneno;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opreator_mobily);
button = (Button) findViewById(R.id.buttonCall);
etPhoneno = (EditText) findViewById(R.id.editText1);
SharedPreferences prefs = getSharedPreferences("Name",
MODE_PRIVATE);
final String value = prefs.getString("n", "0");
// add button listener\
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(receive.this,"saved:"+value,Toast.LENGTH_LONG).show();
}
});
}
}
在你的第二个 activity 中,你试图用不同的名称获取你的 sharedpreferences 实例,并用不同的键获取你保存的值。
替换为:
SharedPreferences prefs = getSharedPreferences("Name", MODE_PRIVATE);
final String value = prefs.getString("n", "0");
和
SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
final String value = prefs.getString("nameKey", "0");
替换下面这两行
SharedPreferences prefs = getSharedPreferences("Name",MODE_PRIVATE);
final String value = prefs.getString("n", "0");
这些行
SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
final String value = prefs.getString("nameKey", "0");