2015-05-09 22 views
-1

我有一个代码,根据先前的布局的EditText(ET)的值来改变布局的TextView的文本>> somethig这样做什么,如果backpressed

我不能评论,所以我会帮助你编辑。使用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活动中。这意味着当我从此布局返回时,文本应该与之前相同。而在此代码回空:/或

回答

0

你这里有两种选择:

  1. 保存值到内部文件系统,所以它仍然会被应用程序已被卸载
  2. 后装
  3. 保存在其中保存按钮,将活动的活动值,并在那里保存文本的变量和bookingkamal活动开始时,它可以从某处读取(提示:Intent#putExtra("KEY", "VALUE")

我不是描述如何你应该这样做,因为有足够的文件关于它在那里,这不是这个答案的重点。这应该给你一个想法如何。

+0

它不是一个常数值。这是一个用户输入的值 你能告诉我怎么做你说的吗? –

+0

查找意图开始活动。您还应该从启动此活动的活动中设置来自bookingKamal的变量。我想帮助你,但我不打算为你写完整的代码。 – engineercoding

-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

+0

这不应该是答案,因为它可以通过意向和父活动中的简单变量更容易地完成。这样可以确保在关闭应用程序时不会保存该值,而且您不必担心清除数据。 – engineercoding

+0

@engineercoding它是解决问题的方法之一。是的,还有其他的方式!并没有理由downvote :(此外问题谈论SharedPreferences,所以我想我会指出相同的。有一个愉快的一天:) –