在我的项目中,有一个带有victory()方法的MainActivity类:
public void victory() {
name = "";
AlertDialog dialog = DialogScreen.getDialog(this, DialogScreen.IDD_MAPHIGHSCORE);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
name = DialogScreen.nickname;
Log.i("NICKNAME :", " " + name);
}
我在DialogScreen类中调用getDialog(Activity activity,int ID)方法:
public class DialogScreen {
static String nickname = "DEF";
public static final int IDD_MAPHIGHSCORE = 1;
public static final int IDD_WORLDHIGHSCORE = 2;
public static AlertDialog getDialog(Activity activity, int ID) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
final EditText editText = new EditText(activity);
int maxLength = 15;
editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});
switch(ID) {
case IDD_MAPHIGHSCORE: {
builder.setTitle(R.string.newHighScore);
builder.setMessage(R.string.enterNickName);
builder.setCancelable(true);
builder.setView(editText).create();
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String text = editText.getText().toString();
nickname = text;
dialog.dismiss();
}
});
}
}
return builder.create();
}
}
但是在Log.i中,我只能看到“NICKNAME:”。我还尝试使用DialogScreen类中的getter和MainActivity类中的setter。但这没有用。
所以第一个问题是如何在MainActivity类中使用EditText创建文本?
第二个是我如何知道(来自MainActivity类)对话框中的“OK”按钮是否已被按下?
答案 0 :(得分:0)
我想这是一个时间问题 在单击对话框的正面按钮之前完成Log.i. 在MainActivity中的对话框中添加DismissListener,并将Log.i移动到该侦听器。