简体   繁体   English

当我使用 Jetpack Compose 时,Material Components 是否提供编辑文本对话框?

[英]Does Material Components provide edit text dialog when I use Jetpack Compose?

I need a edit text dialog when I use Jetpack Compose in my project.在我的项目中使用 Jetpack Compose 时,我需要一个编辑文本对话框。

The Code A is from the article .代码 A 来自文章

Q1: I don't know if Code A can use in Jetpack Compose, could you tell me? Q1:我不知道代码 A 是否可以在 Jetpack Compose 中使用,您能告诉我吗?

I was told that Dialogs are available in the Material library for Jetpack Compose有人告诉我,Jetpack Compose 的Material 库中提供了对话框

It seems that Material library only provide Alert dialog, Simple dialog and Confirmation dialog.材料库似乎只提供警报对话框、简单对话框和确认对话框。

Q2: Does Material Components provide edit text dialog when I use Jetpack Compose? Q2:当我使用 Jetpack Compose 时,Material Components 是否提供编辑文本对话框?

Code A代码 A

AlertDialog.Builder alert = new AlertDialog.Builder(this); 

final EditText edittext = new EditText(ActivityContext);
alert.setMessage("Enter Your Message");
alert.setTitle("Enter Your Title");

alert.setView(edittext);

alert.setPositiveButton("Yes Option", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        //What ever you want to do with the value
        Editable YouEditTextValue = edittext.getText();
        //OR
        String YouEditTextValue = edittext.getText().toString();
    }
});

alert.setNegativeButton("No Option", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // what ever you want to do with No option.
    }
});

alert.show();

AlertDialog implementation from Jetpack Compose allows you to add title, buttons and text as a content. Jetpack Compose 的 AlertDialog 实现允许您添加标题、按钮和文本作为内容。

If you want to add an EditText (or, more preciesly, TextField) instead of just text, you may create your own layout for dialog like this:如果您想添加 EditText(或者更确切地说是 TextField)而不仅仅是文本,您可以为对话框创建自己的布局,如下所示:

@Composable
fun MyAlertDialog(
    title: @Composable () -> Unit,
    content: @Composable () -> Unit,
    dismissButton: @Composable () -> Unit,
    confirmButton: @Composable () -> Unit,
    onDismiss: () -> Unit,
) {
    Dialog(onDismiss) {
        Surface(shape = MaterialTheme.shapes.medium) {
            Column {
                Column(Modifier.padding(24.dp)) {
                    title.invoke()
                    Spacer(Modifier.size(16.dp))
                    content.invoke()
                }
                Spacer(Modifier.size(4.dp))
                Row(
                    Modifier.padding(8.dp).fillMaxWidth(),
                    Arrangement.spacedBy(8.dp, Alignment.End),
                ) {
                    dismissButton.invoke()
                    confirmButton.invoke()
                }
            }
        }
    }
}

If you call this composable function with OutlinedTextField as a content, result will look like this:如果您使用OutlinedTextField作为内容调用此可组合函数,结果将如下所示:

截屏

You can see full working demo here .你可以在这里看到完整的工作演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我使用 Jetpack Compose 时编辑框对话框不填写初始值? - Why doesn't the edit box dialog fill in initial value when I use Jetpack Compose? 当我以默认设置使用 Jetpack Compose 时,Text("Hello") 如何显示字体颜色? - How does Text("Hello") display font color when I use Jetpack Compose with default settings? 如何在 Jetpack Compose 中使对话框组件可重用 - How to make dialog components reusable in Jetpack Compose Jetpack Compose:如果文本不合适,使用不同的布局? - Jetpack Compose: Use different layout if text does not fit? 何时在 Jetpack Compose 中使用 derivedStateOf? - When to use derivedStateOf in Jetpack Compose? 调整 Jetpack Compose 中 Material Design 3 组件的色调提升? - Adjust Tonal Elevation for Material Design 3 Components in Jetpack Compose? 我可以在 Java 中编写 Jetpack Compose 组件吗? - Can I write Jetpack Compose components in Java? 我什么时候应该使用 Android Jetpack Compose Surface 可组合? - When should I use Android Jetpack Compose Surface composable? 使用 Jetpack Compose Navigation 时导航图是否已过时? - Is the navigation graph obsoleted when I use Jetpack Compose Navigation? 为什么 accompanist-systemuicontroller 在使用 Jetpack Compose 和 Material3 时不起作用? - Why does accompanist-systemuicontroller not work when using Jetpack Compose with Material3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM