输入文本对话框 Android

编程入门 行业动态 更新时间:2024-10-11 05:22:33
本文介绍了输入文本对话框 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

本文发布于:2023-11-28 03:52:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640844.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对话框   文本   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!