2012-12-13 61 views
0

我想存儲EditText的輸入並在另一個對話框中顯示它。我怎樣才能做到這一點?如何保存EditText的輸入

所以我有2個活動在其中一個我寫和保存,在另一個我加載它。

public class SaveDialog extends Dialog implements View.OnClickListener{ 

private MapEditorView mapEditorView; 
private Context context; 
private Button ok; 
private EditText editTextMap; 
private boolean exist; 

public SaveDialog(Context context, MapEditorView mapEditorView) { 
    super(context); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 

    this.context = context; 
    this.mapEditorView = mapEditorView; 

    setContentView(R.layout.dialog_mapeditor_save); 

    ok = (Button) findViewById(R.id.Button_save_map); 
    ok.setOnClickListener(this); 

    exist = true; 
} 

public void onClick(View v) { 
    if (v.getId() == ok.getId()) { 

     if(exist){ 
      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
        context); 
      // set title 
      alertDialogBuilder.setTitle("Are You sure?"); 
      // set dialog message 
      alertDialogBuilder 
      .setMessage("Current map Name will be overwritten!") 
      .setCancelable(false) 
      .setPositiveButton("Yes", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, 
           int id) { 

          mapEditorView.getEditMapRenderer().reNameMap(
            String.valueOf(editTextMap.getText().toString())); 

          dismiss(); 
         } 
        }) 
      .setNegativeButton("No", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, 
           int id) { 
          dialog.cancel(); 
         } 
        }); 
      AlertDialog alertDialog = alertDialogBuilder.create(); 
      // show it 
      alertDialog.show(); 
     } 
     else{ 
      mapEditorView.getEditMapRenderer().reNameMap(String.valueOf(editTextMap.getText().toString())); 
      this.dismiss(); 
     } 
    } 
} 

public void setExist(boolean exist) { 
    this.exist = exist; 
} 
} 

回答

1

使用共享偏好:

第一個活動

sh_pref = this.getSharedPreferences("sh_pref", 
       MODE_WORLD_READABLE); 
SharedPreferences.Editor sh_pref_editor = sh_pref.edit(); 
sh_pref_editor.putString("Value", valueEdt.getText().toString()); 
sh_pref_editor.commit(); 
startActivity(new Intent(this, OtherActivity.class)); 

OtherActivity.java

SharedPreferences sh_pref = PreferenceManager.getDefaultSharedPreferences(this); 
String value = sh_pref.getString("Value","nothing"); 
txtView.setText(value);