当用户点击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();

Android相关问答推荐

Jetpack Compose中的导航找不到NavHost类的名称为:startDestination";的参数

Jetpack Compose中尺寸不断增加的动画

在Jetpack Compose中实现自动换行

我需要在房间数据库中保留旧的自动迁移行吗?

将输出写入已发布的 Android 应用程序中的日志(log)文件?

页面标题未显示在内容页面上

setContentView() 方法的签名

如何以编程方式通过 whatsapp android 共享图像和文本

未找到 com.android.tools.build:gradle:7.4.0 的匹配变体

如何将一个 Composable 作为其参数传递给另一个 Composable 并在 Jetpack Compose 中显示/运行它

ImageBitmap 使用 Glide/Coil 到画布

如何在 Jetpack Compose 中添加多个标签

在compose中,为什么修改List元素的属性,LazyColumn不刷新

无法通过 retrofit 解析对 kotlin 数据类的 xml 响应

我不能在 jetpack Compose 中使用 TextField()(material 3)

我应该使用 Bluetooth Classic 还是 Bluetooth LE 与我的移动应用程序通信?

更新后 Firebase 服务无法在模拟器上运行

Kotlin:如何在另一个变量的名称中插入一个变量的值

Kotlin Compose forEach 中的负间距

关于launchWhenX和repeatOnLifecycle的问题