在自定义对话框和EditText之间传递字符串

时间:2013-10-23 14:57:52

标签: android string dialog

我正在使用活动。当我使用EditText时,会出现如下对话框: http://techblogon.com/wp-content/uploads/2013/03/alert-dialog-with-edittext-in-android.png

如何在我的第一个活动中将String(我的名字)现在从Dialog Box传递给EditText?这是我的代码:

public class Main extends Activity implements OnItemSelectedListener {

EditText inputName;
Button btnName;

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

    inputName = (EditText) findViewById(R.id.add_name);

    inputName.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            Dialog();
        }
    });

    // Add Healthcare Button Click event
    btnName.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            final String nameD = inputDoctorName.getText().toString();
            Log.d("TEST","SUCCESS");
        }
    });



}

protected void Dialog()
{
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.layout_dialogbox);
dialog.setTitle("Description de la note");
Button ok = (Button)dialog.findViewById(R.id.btnOK);
ok.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
dialog.dismiss();

}
});
dialog.show();

}
}

你能帮我吗?

1 个答案:

答案 0 :(得分:0)

您必须使用SharedPreference存储字符串并在第一个Activity

中调用它

使用Activity

Dialog中调用此邮件
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
// Set up our Prefs and Editor
prefs = this.getSharedPreferences("YOURCHOICESTRING", Context.MODE_PRIVATE);
editor = prefs.edit();

//onClick (after the dialog closes and you goto your first Activity)
editor.putString("STRING VARIABLE", "THE STRING YOU WANT TO SAVE");

在第一个Activity中使用以下代码,以显示您从Dialog保存的字符串:

private SharedPreferences prefs;
private SharedPreferences.Editor editor;
String strImageNum = prefs.getString("STRING VARIABLE", "");
// use strImageNum as you please.