我有两项活动 MainActivity 和 AddressActivity 。在 MainActivity 中,我有三个editText视图和一个TextView。在TextView上我放了一个onclick,它启动了另一个Activity AddressActivity。
现在问题是当我从AddressActivity回到MainActivity时,MainActivity中的EditText字段正在重置。相反,我想要的是用户在Edittext中设置的任何东西,它应该保留在那里。
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void openAddress(View view) {
startActivity(new Intent(this, AddressActivity.class));
}
}
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#20ffffff">
<EditText
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Give a title"
/>
<EditText
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Give description"
/>
<TextView
android:id="@+id/issue_map_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Exact Address"
android:onClick="openAddress"/>
<Button
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:onClick="onsubmit"
/>
</LinearLayout>
AddressActivity.java
public class AddressActivity extends Activity{
// Async call
// IN this way i am sending the address string to the Main Activity
@Override
protected void onPostExecute(LatLng latLng) {
Intent i = new Intent(AddressActivity.this, MainActivity.class);
i.putExtra("address", userAddress.toString());
startActivity(i);
}
}
问题是来自AdressActivity的数据被设置到MainActivity中,但所有EditText也会被重置。 我想保持输入的EditText数据...如何去做...
答案 0 :(得分:3)
使用此代码
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void openAddress(View view) {
startActivityForResult(new Intent(this, AddressActivity.class),123);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
mTextView.setText(data.getStringExtra("address"));
}
}
和地址活动
public class AddressActivity extends Activity{
// Async call
// IN this way i am sending the address string to the Main Activity
@Override
protected void onPostExecute(LatLng latLng) {
Intent intent = getIntent();
intent.putExtra("address",userAddress.toString());
setResult(123,intent);
finish();
}
答案 1 :(得分:1)
在onCreate
的{{1}}中添加此代码:
MainActivity
问题是您已发送&#34;地址&#34; :Bundle data = getIntent().getExtras();
if(data != null)
{
address = data.getString("address");
myEditText.setText(address);
}
,但你不接受它。上面的代码是采用&#34;地址&#34;值。
答案 2 :(得分:0)
@ 你必须使用共享的首选项.....你可以在共享首选项中保存edittext值并在MainActivity的onCreate方法中加载首选项.......如需更多帮助,请点击此链接... http://examples.javacodegeeks.com/android/core/content/android-sharedpreferences-example/
你也可以用其他方式做到......正如@MeenalSharma解释......