当用户点击My App中的Button
(打印在SurfaceView
中)时,我希望出现文本Dialog
,并希望将结果存储在String
中.我希望文本Dialog
覆盖在当前屏幕上.我怎么才能做到这一点呢?
当用户点击My App中的Button
(打印在SurfaceView
中)时,我希望出现文本Dialog
,并希望将结果存储在String
中.我希望文本Dialog
覆盖在当前屏幕上.我怎么才能做到这一点呢?
听起来是个用AlertDialog的好机会.
尽管看起来很简单,Android没有内置的对话框来做这件事(据我所知).幸运的是,这只是创建标准AlertDialog之上的一点额外工作.您只需要为用户创建一个EditText来输入数据,并将其设置为AlertDialog的视图.如果需要,您可以使用setInputType自定义允许的输入类型.
如果可以使用成员变量,只需将该变量设置为EditText的值,该变量将在对话框关闭后保持不变.如果不能使用成员变量,则可能需要使用侦听器将字符串值发送到正确的位置.(如果这是你需要的,我可以编辑和详细说明).
在你的课堂上:
private String m_Text = "";
在按钮的OnClickListener中(或在从那里调用的函数中):
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
// Set up the input
final EditText input = new EditText(this);
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
builder.setView(input);
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
m_Text = input.getText().toString();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();