我试图创建类似数学编辑器的东西,我想实现方便的LaTeX代码输入. 为此,我创建了一行按钮,扩展键盘.它应该包含特殊的符号,如'\','$'等和 struct ,如\frac {}{},\sqrt {}和其他. 我想将此文本插入到当前正在写入的文本字段中的光标位置(点击这些按钮).

我曾经在Row()中使用以下代码来实现它:

val inputConnection = BaseInputConnection((LocalContext.current as Activity).window.decorView.rootView, true)
...
onClick = {
    inputConnection.sendKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_...)
    inputConnection.sendKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_...)
}
...

但它只能用来写几个符号,如字母和数字.例如,我在KeyEvent对象中没有找到美元符号keycode.使用KeyEvent. ACTION_DOWN—KeyEvent. ACTION_UP模拟输入字符串"\frac {}{}"会很奇怪.除了. sendKeyEvent之外的BaseInputConnection方法对我不起作用(或者我使用它们不当).任何帮助将是感激.

推荐答案

您可以使用Select和Editable API直接操作TextField中的文本,而不是模拟键事件来插入LaTeX符号和构造.下面是一个例子,说明你如何做到这一点:

import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.tooling.preview.Preview

@Composable
fun MathEditor() {
    var textFieldValue by remember { mutableStateOf(TextFieldValue()) }

    Row(modifier = Modifier.padding(16.dp)) {
        Button(onClick = { insertText("\\frac{}{}") }) {
            Text(text = "Fraction")
        }
        Button(onClick = { insertText("\\sqrt{}") }) {
            Text(text = "Square Root")
        }
        // Add more buttons for other symbols and constructions
    }
    // TextField to display and edit LaTeX code
    // Here you will display the content of `textFieldValue`
}

// Function to insert text at the current cursor position in the TextField
fun insertText(textToInsert: String) {
    val selection = Selection(
        start = 0, // Set the start and end to the same value to replace the selected text
        end = 0
    )
    val newText = "$textToInsert"
    // Update the TextFieldValue with the new text inserted at the current cursor position
    // and update the cursor position after the inserted text
    // You can get the current cursor position using TextFieldValue.selection
    // and update the cursor position accordingly
    // Update `textFieldValue` here with the new value
}

@Preview
@Composable
fun MathEditorPreview() {
    MathEditor()
}

在这个例子中:

MathEditor可组合函数为不同的LaTeX符号和构造显示一行按钮. 单击按钮时调用insertText函数.此函数将指定的文本插入到文本字段中的当前光标位置. 您可以根据需要扩展insertText来处理不同的符号和 struct . 这种方法直接操作TextField的文本内容,避免了模拟关键事件的需要.

Android相关问答推荐

如何将文本相对于喷气背包中的图标垂直居中?

如何允许我的应用程序在Android 10上运行,同时目标是API 33

如何在"不同活动"中添加不同按钮?

如何在Reaction Native中显示Google Map iFrame?

在不增加父行宽度的情况下添加延迟行或可滚动行

每次重启Android时预填入Room数据库

如何使可拖动内容停留在可组合框的边界内

如何将我的Android应用程序(Kotlin)中的图像分享给其他应用程序?

在Jetpack Compose中将导航绘图显示在顶部栏下方、底部栏上方

我们可以使用KSP读取类中变量的值吗?

在段的中心绘制饼图(甜甜圈图)的图例

以下代码如何在 Android 上运行

在 Compose 中停止键盘将顶部应用栏推离屏幕

Android:ActivityCompat.requestPermissions 不显示弹窗(Android 13,targetSdkVersion=33)

Android Jetpack Compose - 找不到 R.drawable?

0dp 大小的可组合文件是否可以组合?

Jetpack 组合和片段

如果我在网络请求中指定它们是否与判断网络功能相关

任何 IRCTC 的公共 API 来判断 PNR 状态和座位可用性?

在 Android 10 (API 29) 中隐藏状态栏并在应用程序中使用其空间