如何保存EditText的输入

时间:2012-12-13 15:26:40

标签: android android-layout

我想存储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 个答案:

答案 0 :(得分: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);