我有一个代码,可以根据之前布局>>中edittext(et)的值更改布局中textview的文本。像这样的事情
我无法发表评论,所以我会帮你编辑。使用SharedPreferences存储edittext中的字符串以获得持久性。然后从SharedPreferences获取字符串并执行与您正在执行的操作相同的事情(通过意图传递)
有MorningDrsGeneral:
public class MorningDrsGeneral extends ActionBarActivity {
Button button ;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.morningdrs);
et = (EditText) findViewById(R.id.et);
addListenerOnButton1();
}
public void addListenerOnButton1() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, bookingKamal.class);
intent.putExtra("fn" , et.getText().toString());
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
并且有bookingKamal.java:
public class bookingKamal extends ActionBarActivity {
Button button;
TextView textView3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bookingkamal);
textView3 = (TextView) findViewById(R.id.textView3);
String A = textView3.getText().toString();
String N = " ";
if (A.equals(N)){
Intent intent = getIntent();
String texx = intent.getStringExtra("fn");
textView3.setText(texx);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
所以我必须将文本保留在bookingKamal活动中..这意味着当我从这个布局返回并回到它时,文本应该与之前相同。在此代码中,它返回null:/或
答案 0 :(得分:0)
这里有两个选项:
Intent#putExtra("KEY", "VALUE")
)我没有描述你应该怎么做,因为那里有足够的文档,这不是这个答案的重点。这应该会让你知道如何做到这一点。
答案 1 :(得分:-1)
您可以使用sharedpreferences
。
上述链接将帮助您了解如何使用它。
要将数据添加到SharedPreferences,请使用
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString("text", mSaved.getText().toString());
editor.putInt("selection-start", mSaved.getSelectionStart());
editor.putInt("selection-end", mSaved.getSelectionEnd());
editor.apply();
并检索
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null)
{
//mSaved.setText(restoredText, TextView.BufferType.EDITABLE);
int selectionStart = prefs.getInt("selection-start", -1);
int selectionEnd = prefs.getInt("selection-end", -1);
/*if (selectionStart != -1 && selectionEnd != -1)
{
mSaved.setSelection(selectionStart, selectionEnd);
}*/
}
注意:SharedPreferences将保存该值,即使您关闭应用程序并重新启动它也是如此。价值仍然存在。因此,请确保在完成数据后清除数据。您可以阅读更多相关信息here at the SharedPreferences docs。