如何将变量的数据从一个 Activity 传输到另一个
How to transfer the data of a variable from one Activity to another
我想将一个 EditText
的值或一个变量的值从 Activity
转移到另一个 Activity
。
我应该使用 Handler
或什么来执行以下操作?
如果使用 Handler
那么我该如何实现呢?
您可以使用共享首选项将数据存储在前 activity 中,然后在子 activity 中检索数据。
使用共享首选项,您可以像这样存储 editText 的值
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "Elena");
editor.putInt("idName", 12);
editor.commit();
现在,在另一个 activity 中,您可以将其检索为
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
int idName = prefs.getInt("idName", 0); //0 is the default value.
}
检查此 video tutorial out. Or you can also check this tutorial。
首先 activity 你应该像这样为意图添加额外的参数:
EditText editText;
edittextIntent intent = new Intent(this, Second.class);
intent.putExtra("arg",editText.getText());
startActivity(intent);
然后在第二个 activity 你检索这样的参数:
String passedArg = getIntent().getExtras().getString("arg");
另一种方法是使用 intent extras。
要发送数据,使用类似这样的东西
Intent intent = new Intent(getBaseContext(), NewActivity.class);
intent.putExtra("Edit Text data", data);
startActivity(intent);
然后检索为
String s = getIntent().getStringExtra("Edit Text data");
如果使用 Handler
我认为只需要使用 hasExtra()
进行简单验证
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
String value = editText.getText().toString().trim();
intent.putExtra("data", value);
startActivity(intent);
第二个Activity
if (getIntent().hasExtra("value")) {
String value = getIntent().getStringExtra("data");
Log.d("MI_VALUE", value);
}
我想将一个 EditText
的值或一个变量的值从 Activity
转移到另一个 Activity
。
我应该使用 Handler
或什么来执行以下操作?
如果使用 Handler
那么我该如何实现呢?
您可以使用共享首选项将数据存储在前 activity 中,然后在子 activity 中检索数据。
使用共享首选项,您可以像这样存储 editText 的值
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "Elena");
editor.putInt("idName", 12);
editor.commit();
现在,在另一个 activity 中,您可以将其检索为
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
int idName = prefs.getInt("idName", 0); //0 is the default value.
}
检查此 video tutorial out. Or you can also check this tutorial。
首先 activity 你应该像这样为意图添加额外的参数:
EditText editText;
edittextIntent intent = new Intent(this, Second.class);
intent.putExtra("arg",editText.getText());
startActivity(intent);
然后在第二个 activity 你检索这样的参数:
String passedArg = getIntent().getExtras().getString("arg");
另一种方法是使用 intent extras。
要发送数据,使用类似这样的东西
Intent intent = new Intent(getBaseContext(), NewActivity.class);
intent.putExtra("Edit Text data", data);
startActivity(intent);
然后检索为
String s = getIntent().getStringExtra("Edit Text data");
如果使用 Handler
我认为只需要使用 hasExtra()
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
String value = editText.getText().toString().trim();
intent.putExtra("data", value);
startActivity(intent);
第二个Activity
if (getIntent().hasExtra("value")) {
String value = getIntent().getStringExtra("data");
Log.d("MI_VALUE", value);
}