0

我想存储输入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;
}
}
4

1 回答 1

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));

其他活动.java

SharedPreferences sh_pref = PreferenceManager.getDefaultSharedPreferences(this);
String value =  sh_pref.getString("Value","nothing");
txtView.setText(value);
于 2012-12-14T08:50:10.893 回答