Java 如果反压,该怎么办
我有一个代码,可以根据上一个布局>>中edittext(et)的值更改布局中textview的文本 我无法评论,因此我将帮助您编辑。使用SharedReferences存储edittext中的字符串以获得持久性。然后从SharedReferences获取字符串,并执行与您所做的相同的操作(将其传递给intent) 这里有晨曦博士概述:Java 如果反压,该怎么办,java,android,Java,Android,我有一个代码,可以根据上一个布局>>中edittext(et)的值更改布局中textview的文本 我无法评论,因此我将帮助您编辑。使用SharedReferences存储edittext中的字符串以获得持久性。然后从SharedReferences获取字符串,并执行与您所做的相同的操作(将其传递给intent) 这里有晨曦博士概述: public class MorningDrsGeneral extends ActionBarActivity { Button button ;
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:/or您有两个选项:
Intent\putExtra(“键”、“值”)
) 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);
}*/
}
注意:SharedReferences将保存该值,这样,即使您关闭应用程序并重新启动它。这些值仍将保持不变。因此,请确保在处理完数据后将其清除。您可以阅读更多有关它的信息。它不是一个常量值。这是一个用户输入的值。你能告诉我怎么做吗?查找意图开始活动。您还应该从启动此活动的活动中设置bookingKamal变量。我想帮助您,但我不打算为您编写完整的代码。这不应该是答案,因为在父活动中使用intent和一个简单的变量可以更容易地完成。这样可以确保在关闭应用程序时不会保存值,并且您不必担心清除数据。@EngineerCodeing这是解决问题的方法之一。是的,还有其他的方法!也没有理由否决投票:(这个问题还涉及到共享参考,所以我想我也会指出同样的问题。祝你有愉快的一天:)