V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ssccyy
V2EX  ›  Android

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

  •  
  •   ssccyy · 13 天前 · 840 次点击

    安卓小白一个,最近因为公司需求接了一个手持 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() // 触发回调函数
                                    }
                                ),
    
                                )
    

    求大佬指点迷津。。。

    5 条回复    2024-05-06 11:21:52 +08:00
    ssccyy
        1
    ssccyy  
    OP
       13 天前
    手持 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
        2
    nicevar  
       13 天前
    不要什么权限,直接监听按键事件处理,google 的 compose 没几个人愿意用,问题都没几个人回答了。
    lxiian
        3
    lxiian  
       12 天前
    pda 设备回车就是往 edittext 里发送了个“\n”的换行符,直接监听这个就行了吧,不需要自带的 ImeAction 去判断
    ssccyy
        4
    ssccyy  
    OP
       12 天前
    @nicevar 哎,领导让用原生安卓, 要是我自己直接 flutter 或者 uniapp ,再简单直接 hbuilder 套 h5 了
    ssccyy
        5
    ssccyy  
    OP
       12 天前
    @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
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2458 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 04:07 · PVG 12:07 · LAX 21:07 · JFK 00:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.