显示“下一步”按钮,而不是键盘上的“完成”按钮

问题描述 投票:0回答:2

我尝试通过 imeAction 更改键盘的某些部分, 但仍然可以看到这仅适用于第一个文本字段 enter image description here

如何解决这个问题?

@Composable
fun TipTimeLayout() {
        EditNumberField(
            label = R.string.bill_amount,
            value = amountInput,
            onValueChange = {amountInput = it},
            keyboardOptions = KeyboardOptions.Default.copy(
                keyboardType = KeyboardType.Number,
                imeAction = ImeAction.Next
            ),
            modifier = Modifier
                .padding(bottom = 32.dp)
                .fillMaxWidth()
        )
        )
        EditNumberField(
            value = tipInput,
            keyboardOptions = KeyboardOptions.Default.copy(
                keyboardType = KeyboardType.Number,
                imeAction = ImeAction.Done
            ),
            onValueChange = {tipInput = it},
            label = R.string.how_was_the_service,
            modifier = Modifier
                .padding(top = 32.dp)
                .fillMaxWidth()
        )
    }
}

/**
 * Calculates the tip based on the user input and format the tip amount
 * according to the local currency.
 * Example would be "$10.00".
 */
private fun calculateTip(amount: Double, tipPercent: Double = 15.0): String {
    val tip = tipPercent / 100 * amount
    return NumberFormat.getCurrencyInstance().format(tip)
}

@Composable
fun EditNumberField(
    value: String,
    onValueChange: (String) -> Unit,
    @StringRes label: Int,
    keyboardOptions: KeyboardOptions,
    modifier: Modifier = Modifier
) {

    TextField(
        value = value,
        onValueChange = onValueChange,
        modifier = Modifier,
        label = { Text(stringResource(label))},
        singleLine = true,
        keyboardOptions = KeyboardOptions.Default.copy(
            keyboardType = KeyboardType.Number,
            imeAction = ImeAction.Next
        )
    )
}

当用户点击第二个文本字段时,键盘右下角应出现“完成”按钮

android kotlin android-jetpack-compose
2个回答
0
投票

代码中的问题似乎出在 EditNumberField 可组合项中,而不是使用 ImeAction 创建新的 KeyboardOptions。Next 它应该使用参数提供的:

@Composable
fun EditNumberField(
    value: String,
    onValueChange: (String) -> Unit,
    @StringRes label: Int,
    keyboardOptions: KeyboardOptions,
    modifier: Modifier = Modifier
) {

    TextField(
        value = value,
        onValueChange = onValueChange,
        modifier = Modifier,
        label = { Text(stringResource(label))},
        singleLine = true,
        keyboardOptions = keyboardOptions,
    )
}

0
投票

在您的

EditNumberField
可组合项中,您明确设置了
imeAction = ImeAction.Next
,因此 both TextFields 具有 Next 操作。

您为该可组合项提供一个

keyboardOptions
参数,其中第一个 TextField 的操作设置为 Next ,第二个 TextField 的操作设置为 Done ,但该参数从未使用过,因此您设置什么并不重要到.

© www.soinside.com 2019 - 2024. All rights reserved.