2015-05-09 26 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,所以我想我會指出相同的。有一個愉快的一天:) –