【问题标题】:How to call back the cursor to Line1 and position 0 in EditText?如何将光标回调到 Line1 并在 EditText 中定位 0?
【发布时间】:2020-04-04 08:10:38
【问题描述】:

我正在为 PDA 设备编写条码扫描器应用程序。在该设备中,只有 LF 终结器。所以当我扫描条形码时,光标跳到了另一行。即使我设置了edittext.requestFocus(),光标也不会回到编辑文本的开头。我想将光标回调到第一行并定位“0”。我怎样才能做到这一点?另一件事是,我可以获得按下的触发键码吗?这是代码-

ID.setOnKeyListener(View.OnKeyListener { _, keyCode, event ->

        if (event.keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_DOWN   ) {

            if (edt_location.text.toString() == "" || edt_qty.text.toString() == "") {
                Toast.makeText(this, "Make Sure To Enter Location And Qty", Toast.LENGTH_SHORT)
                    .show()
                edt_id.setText("")

            } else{
                try {
                    val item=Item(
                        edt_id.text.toString(),
                        Integer.parseInt(edt_qty.text.toString()),
                        edt_location.text.toString()
                    )
                    qt=edt_qty.text.toString().toInt()
                    record_code=edt_id.text.toString()
                    record_location=edt_location.text.toString()
                    db.addItem(item)
                    db.viewData()


                    txt_article.setText(code.toString())
                    txt_price.setText(price.toString())
                    txt_description.setText(desc.toString())
                    txt_isbn.setText(record_code.toString())
                    txt_tran_qty.setText(qty.toString())

                    record_id=edt_id.text.toString()
                    edt_id.setText("")
                    edt_id.requestFocus()

                } catch (nfe: NumberFormatException) {
                    nfe.printStackTrace()
                }

            }
        }

        false

    })

【问题讨论】:

    标签: android kotlin keypress barcode-scanner keycode


    【解决方案1】:

    所以关于question 的解决方案,我认为您的问题的解决方案应该很简单:

    edt_id.setSelection(position)
    

    whereposition 是您想要的光标位置。

    你的键码到底是什么?

    【讨论】:

      猜你喜欢
      • 2011-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多