如何将edittext值从一个活动提取到另一个活动中以获取字符串?
问问题
1529 次
2 回答
2
传递意图中的值......但如果该活动没有出现在同一流中使用 SharedPreferences,将值存储在原始活动中,然后在以后的活动中检索它。共享首选项代码:
SharedPreferences pref = context.getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);
pref.edit().putString("NAME", editText.getText().toString()).commit();
在其他活动中
SharedPreferences pref = context.getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);
String ediTextVal = pref.getString("NAME", "anyDefaultValue");
如果两个活动都在一个流中,则意图解决方案,在活动 1 中使用 editText :
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("editTextVal", editText.getText().toString);
startActivity(intent);
在需要editText值的activity2中:
Bundle extras = getIntent().getExtras();
String editTextVal= null;
if(extras !=null && extras.containsKey("editTextVal"))
{
editTextVal= extras.getString("editTextVal");
}
于 2013-02-22T07:22:46.573 回答
0
你可以通过这种方式做到这一点
来自 FirstActivity
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("str", content1); //content1 is String you want to pass in another activity
startActivity(intent);
从第二个活动获取数据
Intent intent = getIntent();
String str = intent.getStringExtra("str");
希望这对你有帮助
于 2013-02-22T07:25:46.887 回答