2014-09-11 128 views
0

我的應用程序中有一些按鈕,當點擊它們時會打開一個對話框。 在這些對話框中,用戶可以用edittext填寫他的作業。 無論如何,我想保存該用戶輸入,以便如果我關閉對話框或應用程序,我能夠回憶這些數據,並且因爲我是新的eclipse和編程,我不知道該怎麼做。保存對話框輸入

這是我的MainActivity

public class MainActivity extends ActionBarActivity { 

protected static final String TAG = null; 
private Button bthomeWork; 
private Button bthomeWork1; 
private Button bthomeWork2; 
private Button bthomeWork3; 
private Button bthomeWork4; 
private AlertDialog.Builder dialogbuilder; 
private String strName=""; 




private void homeworkdialog(){ 
    dialogbuilder = new AlertDialog.Builder(this); 
    final EditText txtinput = new EditText(this); 
    dialogbuilder.setTitle("Homework"); 
    dialogbuilder.setMessage("Was sind deine Hausaufgaben?"); 
    dialogbuilder.setView(txtinput); 
    dialogbuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 

     @SuppressLint("ShowToast") @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
      strName+= txtinput; 
      Toast.makeText(getApplicationContext(), "Eingetragen", Toast.LENGTH_SHORT); 
     } 
    }); 

    dialogbuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

     @SuppressLint("ShowToast") @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
      Toast.makeText(getApplicationContext(), "Kein Eintrag", Toast.LENGTH_SHORT); 

     } 
    }); 

    dialogbuilder.show(); 

} 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fragment_main); 


    bthomeWork = (Button)findViewById(R.id.bthomeWork); 
    bthomeWork1= (Button)findViewById(R.id.bthomeWork1); 
    bthomeWork2= (Button)findViewById(R.id.bthomeWork2); 
    bthomeWork3= (Button)findViewById(R.id.bthomeWork3); 
    bthomeWork4= (Button)findViewById(R.id.bthomeWork4); 
    bthomeWork.setOnClickListener(new View.OnClickListener() { 


     public void onClick(View v) { 
      // TODO Auto-generated method stub 
       homeworkdialog(); 
     } 
     }); 
    bthomeWork1.setOnClickListener(new View.OnClickListener() { 


     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      homeworkdialog(); 
     } 
     }); 

希望你能幫助我:)

+0

代替'strName + = txtinput;'可以通過變量'strName'訪問你的文本... – 2014-09-11 11:28:57

+0

如果你想將編輯框的文本設置爲上次輸入的內容,只需執行'txtinput.setText(strName);' – 2014-09-11 11:30:20

回答

1

你可以得到文字和EDITTEXT的值保存到一個字符串

String MyText = txtinput.getText().toString(); 

然後你可以使用sharedPreferences或將數據保存到數據庫中,具體取決於您想要執行的操作。

您可以在共享偏好保存價值這樣

SharedPreferences sp = getSharedPreferences("key", 0); 
SharedPreferences.Editor sedt = sp.edit(); 
sedt.putString("textvalue", txtInput.getText().toString()); 
sedt.commit(); 

然後你就可以在另一個活動獲取已保存的文本或任何在您的項目像這樣

SharedPreferences sp = getSharedPreferences("key", 0); 
String textValue = sp.getString("textvalue",""); 

更多有關使用共享偏好檢查跳出此鏈接http://www.tutorialspoint.com/android/android_shared_preferences.htm

還要注意,sharedpreferences存儲爲簡單的xml值...所以任何擁有紮根android手機的人都可以輕鬆訪問保存的值...所以如果你w如果你把`strName + = txtinput.getText()。toString();`那麼你可以檢查出保持數據在android編程中的安全的方法http://www.androidsnippets.com/encryptdecrypt-strings

+0

如果我的代碼看起來像使用sharedPreferences?正如我所說我也是新的:) – Alstai 2014-09-11 11:40:07