如何将edittext值从一个活动提取到另一个字符串活动?
答案 0 :(得分:0)
传递intent中的值...但是如果该活动没有出现在同一个流中使用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");
意图解决方案如果两个活动都在一个流中,带有editText的IN activity1:
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("editTextVal", editText.getText().toString);
startActivity(intent);
在activity2中需要editText值:
Bundle extras = getIntent().getExtras();
String editTextVal= null;
if(extras !=null && extras.containsKey("editTextVal"))
{
editTextVal= extras.getString("editTextVal");
}
答案 1 :(得分: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");
希望这对你有所帮助