如何在textview android中保存我的文本

时间:2015-05-09 15:29:15

标签: java android textview

我有一个代码可以根据之前版面中edittext(et)的值更改布局中textview的文本

有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);
}
   }

我必须将文本保留在预定的布局中。

这意味着当我从这个布局返回并回到它时,文本应该与之前相同。

2 个答案:

答案 0 :(得分:0)

然后,应该是这样的

<强> bookingKamal.java

String texx;
void onBackPressed() {
         Intent intent = new Intent(getApplicationContext(), MorningDrsGeneral.class);
         intent.putExtra("texx" , texx);
         startActivity(intent);
}

<强> MorningDrsGeneral.java

    protected void onCreate(Bundle savedInstanceState) {
        et = (EditText) findViewById(R.id.et);
        Intent intent2 = getIntent();
        if (intent2.getStringExtra("texx") != "") {
               String abcd = intent2.getStringExtra("texx");
               et.setText(abcd); 
        } 

   }

答案 1 :(得分:0)

您可以使用 SharedPreferences ,如果您离开布局,这将保存所有内容,如果您关闭应用,则会保存(如果需要)。请查看http://developer.android.com/reference/android/content/SharedPreferences.html

上的Android文档