在此程序中,我有三个活动,主要,第二和第三。当应用程序打开时,将显示一个空白的textview和按钮。选择该按钮将带您进入第二个活动,用户可以在其中输入姓名,电话号码和电子邮件地址。填写完这些表格后,用户将点击一个按钮来存储该信息并将其带回主要活动。回到主要活动后,用户将看到他们输入的名称显示在曾经为空白的文本视图中。然后,他们可以选择该文本视图,将其带到第三活动,显示他们在第二活动中输入的姓名,电话号码和电子邮件地址。
我希望所有工作都可以将数据从第二个活动传递到第三个活动。我做了一些研究,发现捆绑和共享首选项可以做很多事情。我对它们不太熟悉,因为这对我来说是很新的。我确实尝试实现它们,但是没有任何运气。无论如何,代码都在下面,任何帮助,反馈或指导将不胜感激。先感谢您!
主要活动:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
startActivityForResult(new Intent(getApplicationContext(),SecondActivity.class),999);
}
public void onClickText(View v)
{
startActivityForResult(new Intent(getApplicationContext(),ThirdActivity.class),999);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 999 && resultCode == RESULT_OK)
{ TextView contactView = (TextView) findViewById(R.id.contactDisplay);
contactView.setText(data.getStringExtra("Name"));
}
}}
第二活动:
public class SecondActivity extends AppCompatActivity {
public String textName;
public String emailAddress;
public int phoneNumber;
private TextView textNameView2;
EditText textPersonName;
EditText number;
EditText textEmailAddress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
public void onClickTwo(View view)
{
textPersonName = (EditText) findViewById(R.id.name);
number = (EditText) findViewById(R.id.number);
textEmailAddress = (EditText) findViewById(R.id.email);
textName = textPersonName.getText().toString();
emailAddress = textEmailAddress.getText().toString();
phoneNumber = Integer.valueOf(number.getText().toString());
showToast(textName +"added");
Intent backMain = new Intent(this, MainActivity.class);
// Intent backMain = new Intent();
backMain.putExtra("Name",textName);
// backMain.putExtra("Email", emailAddress);
// backMain.putExtra("Phone", phoneNumber);
setResult(RESULT_OK, backMain);
Intent thirdMain = new Intent(this, ThirdActivity.class);
thirdMain.putExtra("Name",textName);
thirdMain.putExtra("Email",emailAddress);
thirdMain.putExtra("Phone",phoneNumber);
setResult(RESULT_OK,thirdMain);
finish();
}
private void showToast(String text)
{
Toast.makeText(this,text, Toast.LENGTH_LONG).show();
}}
第三项活动:
public class ThirdActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 999 && resultCode == RESULT_OK) {
TextView contact = (TextView) findViewById(R.id.contactNameView);
contact.setText(data.getStringExtra("Name"));
TextView phone = (TextView) findViewById(R.id.phoneView);
phone.setText(data.getStringExtra("Phone"));
TextView email = (TextView) findViewById(R.id.emailView);
email.setText(data.getStringExtra("Email"));
}
}
}
答案 0 :(得分:0)
请使用意图活动之间传递数据的主题:
How do I pass data between Activities in Android application?
但是,如果要使用共享首选项传递数据,请使用以下代码段:
public class SharedPreferenceManager {
private static final String PREFS_NAME = Config.SHARED_PREF_NAME;
/**
* @param context
* @param key
* @param value
* @return
*/
public static boolean saveToPreference(Context context, String key, String value) {
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value);
return editor.commit();
}
/**
* @param context
* @param key
* @return
*/
public static String loadFromPreference(Context context, String key) {
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
return settings.getString(key, "");
}
}
要保存到你的应用程序上下文传递偏好,你想要的键和字符串保存。要从SharedPreference加载保存的字符串,请在Context中传递,然后在活动中输入Key,让我说我希望在一个活动中将字符串电子邮件保存到我的共享首选项,然后在另一个活动中加载它,这就是我的做法:>
//Save to shared Preference in Activity A
SharedPreferenceManager.saveToPreference(this, "email", emailAddress.getText().toString());
//Load from shared preference in activity b
String emailAddress = SharedPreferenceManager.loadFromPreference(this, "email");