当用户单击Button
在我的应用程序中(打印在SurfaceView
),我想要一条短信Dialog
出现,我想将结果存储在String
。我想要文字Dialog
覆盖当前屏幕。我怎样才能做到这一点?
听起来是一个使用的好机会警报对话框.
尽管看起来很简单,Android 并没有内置的对话框来执行此操作(据我所知)。幸运的是,除了创建标准 AlertDialog 之外,这只是一些额外的工作。您只需要创建一个EditText供用户输入数据,并将其设置为AlertDialog的视图。您可以使用自定义允许的输入类型设置输入类型, 如果你需要。
如果您能够使用成员变量,则只需将该变量设置为 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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)