2015-05-09 55 views
0

我有一个代码,根据的EditText(ET)在以前的布局如何拯救我的文字中的TextView的android

有MorningDrsGeneral值改变布局的TextView的文本:

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

我必须保持日e预订打印布局中的文本。

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

+0

使用saveinstancstate保存数据 –

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

    } 
+0

这不是正确的做法。最好使用SharedPreferences。这将保存数据(即使你关闭了你的应用程序=>如果你想这样做当然)。您使用的方法不方便。当你在这两个之间添加一个新的片段时,你就被迫在任何地方改变你的代码。最好用sharedPreferences保存数据 –

+0

你是对的。如果使用片段,这种方式将无法正常工作。我认为我们必须创建维护SharedPreferences的类。 –

0

您可以使用SharedPreferences,这将节省一切如果你离开布局或前夕n(如果你想)如果你关闭你的应用程序。看看Android的文档http://developer.android.com/reference/android/content/SharedPreferences.html

+0

好的,你可以告诉我如何做到这一点..我读它,我不知道在哪里把我的代码:( –

+0

http://stackoverflow.com/a/3624358/4774263这家伙很容易地解释它 –

+0

没有我的例子不同>>它不是关于时间的问题! –