输入文本对话框 Android [英] Input text dialog Android

查看:27
本文介绍了输入文本对话框 Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击我的应用程序中的 Button(打印在 SurfaceView 中)时,我想要一个文本 Dialog出现,我想将结果存储在 String 中.我希望文本 Dialog 覆盖当前屏幕.我该怎么做?

When a user clicks a Button in my App (which is printed in a SurfaceView), I'd like a text Dialog to appear and I would like to store the result in a String. I'd like the text Dialog to overlay the current screen. How can I do this?

推荐答案

听起来是使用 警报对话框.

尽管看起来很基本,但 Android 没有内置对话框来执行此操作(据我所知).幸运的是,这只是在创建标准 AlertDialog 之上的一些额外工作.您只需创建一个 EditText 供用户输入数据,并将其设置为 AlertDialog 的视图.您可以使用 setInputType,如果你需要.

As basic as it seems, Android does not have a built-in dialog to do this (as far as I know). Fortunately, it's just a little extra work on top of creating a standard AlertDialog. You simply need to create an EditText for the user to input data, and set it as the view of the AlertDialog. You can customize the type of input allowed using setInputType, if you need.

如果您能够使用成员变量,您可以简单地将该变量设置为 EditText 的值,它会在对话框关闭后持续存在.如果不能使用成员变量,则可能需要使用侦听器将字符串值发送到正确的位置.(如果这是您的需要,我可以编辑和详细说明).

If you're able to use a member variable, you can simply set the variable to the value of the EditText, and it will persist after the dialog has dismissed. If you can't use a member variable, you may need to use a listener to send the string value to the right place. (I can edit and elaborate more if this is what you need).

在您的班级内:

private String m_Text = "";

在按钮的 OnClickListener 中(或从那里调用的函数中):

Within the OnClickListener of your button (or in a function called from there):

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

这篇关于输入文本对话框 Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆