jetpack compose TextField 组件在手持 PAD 设备无法触发回车事件

44 天前
 ssccyy

安卓小白一个,最近因为公司需求接了一个手持 PDA 的项目(就是仓库扫码的机器,类似快递小哥扫码的机器)

页面输入框组件是使用的 TextField

出现的问题:我理解手持 PDA 设备扫码就是相当于外接一个键盘输入数字+一个回车的操作,我 PC 使用模拟器安装安卓项目,然后使用键盘加回车是可以触发回车事件的, 安装项目到手持 PDA 设备里面的效果是我输入了一段文字,然后触发的是选中文字的操作, 并不是回车的事件(我知道这里我描述的比较模糊,表达不太强, 不知道怎么具体的形容,虚心请教勿喷)

                        TextField(
                            value = productViewModel.zhCode,
                            onValueChange = { productViewModel.zhCode = it },
                            modifier = Modifier
                                .weight(1f)
                                .border(1.dp, Color.Gray, RoundedCornerShape(4.dp))
                                .padding(4.dp),
                            keyboardOptions = KeyboardOptions.Default.copy(
                                imeAction = ImeAction.Done // 设置回车动作为“完成”
                            ),
                            keyboardActions = KeyboardActions(
                                onDone = {

                                    // 执行回车动作时的操作
                                    keyboardController?.hide() // 隐藏键盘
                                    productViewModel.zhEnter() // 触发回调函数
                                }
                            ),

                            )

求大佬指点迷津。。。

1539 次点击
所在节点    Android
5 条回复
ssccyy
44 天前
手持 PDA 设备:iData 95S
手持 PDA 设备系统: 安卓 6.0

项目 build.gradle
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'org.jetbrains.kotlin.plugin.parcelize'
}

android {
compileSdk 34

defaultConfig {
applicationId "icu.bughub.app.app"
minSdk 21
targetSdk 34
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}

pickFirst '**/libc++_shared.so'
doNotStrip "*/arm64-v8a/libYTCommon.so"
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"


def lifecycle_version = "2.4.1"

implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version"
implementation 'androidx.activity:activity-compose:1.4.0'

//accompanist
def accompanist_version = "0.24.3-alpha"
implementation "com.google.accompanist:accompanist-systemuicontroller:$accompanist_version"
implementation "com.google.accompanist:accompanist-insets:$accompanist_version"
implementation "com.google.accompanist:accompanist-pager:$accompanist_version"
implementation "com.google.accompanist:accompanist-navigation-animation:$accompanist_version"
implementation "com.google.accompanist:accompanist-placeholder-material:$accompanist_version"
implementation "com.google.accompanist:accompanist-swiperefresh:$accompanist_version"

//图标扩展
implementation "androidx.compose.material:material-icons-extended:$compose_version"

implementation "androidx.constraintlayout:constraintlayout-compose:1.0.0"


//图片加载
implementation("io.coil-kt:coil-compose:2.0.0-rc01")

//webview
implementation project(path: ':webview')


//播放器
implementation 'com.tencent.liteav:LiteAVSDK_Player:latest.release'

//datastore
implementation "androidx.datastore:datastore-preferences:1.0.0"

def retrofit = "2.9.0"
//网络请求框架
implementation "com.squareup.retrofit2:retrofit:$retrofit"
//GSON
// implementation "com.squareup.retrofit2:converter-gson:$retrofit"
//moshi
implementation "com.squareup.retrofit2:converter-moshi:$retrofit"

def moshi_version = "1.13.0"
implementation "com.squareup.moshi:moshi-kotlin:$moshi_version"


testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
}




会不会是我需要开什么原型的权限或者是什么行为。
nicevar
43 天前
不要什么权限,直接监听按键事件处理,google 的 compose 没几个人愿意用,问题都没几个人回答了。
lxiian
43 天前
pda 设备回车就是往 edittext 里发送了个“\n”的换行符,直接监听这个就行了吧,不需要自带的 ImeAction 去判断
ssccyy
42 天前
@nicevar 哎,领导让用原生安卓, 要是我自己直接 flutter 或者 uniapp ,再简单直接 hbuilder 套 h5 了
ssccyy
42 天前
@lxiian 感谢有空研究下监听 \n, 目前的解决方案是在 modifier 上判断的回车事件
modifier = Modifier
.weight(1f)
.border(1.dp, Color.Gray, RoundedCornerShape(4.dp))
.padding(4.dp)
.onKeyEvent {
// if (it.key.keyCode == Key.Backspace.keyCode) {
// it.key.keyCode
if (it.key.keyCode == Key.Enter.keyCode) {
productViewModel.zhEnter() // 触发回调函数
}
false
}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1037669

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX