java - 使按钮功能像键盘一样

标签 java android android-recyclerview kotlin

我有一个应该在平板电脑上运行的应用程序,并且有一个页面,其中存在 recyclerview,并且在其行项目中有 2 个编辑文本(以 numberDecimal 作为输入),默认键盘不应出现,因为它覆盖了屏幕的很大一部分。

我在 Activity 中创建了按钮,其作用类似于键盘按钮,但问题是如何使 Activity 中的按钮与适配器中的编辑文本进行通信。

我如何知道,如果我按下(按钮“1”),例如,它应该在适配器中的聚焦编辑文本中显示 1,以及如果我按下“<”或“>”按钮,它应该如何知道上一个和下一个编辑文本

请帮忙

最佳答案

我将提供 Kotlin 中的示例,我假设这就是您正在使用的。 因此,您可以简单地重写父类中的键处理,例如:

创建一个从 onCreate 调用一次的方法,如下所示:

 fun keydown_onClick(){
        try{
            myActivity.setOnKeyListener { _, keyCode, event ->
                if (keyCode == android.view.KeyEvent.KEYS_YOU_CARE_ABOUT) {
                    //handle it
                    if(activeViewHolder != null){
                        var displayText = activeViewHolder.yourEditText.text
                        keyPressedChar = (char)event.getUnicodeChar()+""
                        //if it's a special key you care about, then handle it in a when statement or if and pass to your adapter if needed to go to next row for example.
                        displayText += keyPressedChar
                        activeViewHolder.yourEditText.text = displayText
                    }
                    return@setOnKeyListener true//we've processed it
                } else
                    return@setOnKeyListener false// pass on to be processed as normal
            }
        }catch (ex: Exception){
            A35Log.e(mClassTag, "Error handling onkeydown listener: ${ex.message}")
        }
    }

接下来是你如何处理它。那么你应该跟踪 Activity 行。您可以通过在 Activity 中创建回调来实现此目的,该回调会在选择不同行或获得焦点时收到通知。

interface IFocusChangeListener {
    fun onNewItemFocused(holder: MyApdaterViewHolder, item: DataItem, index: Int)
}

这将被传递到您的适配器并用于触发您的 Activity 类。

//Activity 中

var activeViewHolder: MyAdapterViewHolder? = null
var activeIndex: Int? = null
var activeItem: DataItem? = null
fun onNewItemFocused(holder: MyAdapterViewHolder, item: DataItem, index: Int){
     activeViewHolder = holder
     activeIndex = index
     activeItem = item
}

//现在在按键事件中,您只需将值传递给 activeViewHolder 中的 editText,

最后一 block 位于适配器中。

//绑定(bind) View 创建 View 持有者,通常的膨胀。 然后,当您拥有 editText 时,您只需添加即可。

var item = currentDataItem
var index = currentDataItemIndex
var viewHolder = currentViewHolder //from onBind
viewHolder.setOnFocusChangeListener(object: View.OnFocusChangeListener{
            override fun onFocusChange(v: View?, hasFocus: Boolean) {
                if(hasFocus){
                    mFocusListener?.onNewItemFocused(viewHolder, item, index)
                }
            }
        })

//在适配器中您可能还需要

fun onNextEditTextClicked(item: DataItem, index: Int){
    //get next data Item by index + 1 if exist and get it's viewholder.editText and set focus to it, this will automatically trigger the focus event back to the activity
}
fun onPreviousEditTextClicked(item: DataItem, index: Int){
    //get next data Item by index - 1 if exist and get it's viewholder.editText and set focus to it, this will automatically trigger the focus event back to the activity
}

现在,您在调用 Activity 中拥有了焦点 View 持有者,您可以捕获您想要捕获的键,可能是所有键。您可以将它们传递进去,应该可以开始了。

注意*

郑重声明,如果您使用现代实践,即数据绑定(bind),那么您应该能够只更新模型中的可绑定(bind)字符串,它就会显示在屏幕上,而无需传递它。您还可以将焦点绑定(bind)到被选择并仅更新模型的选定 boolean 值。如果您使用绑定(bind),则可以通过更简洁的方法来执行此操作。但现在,只是帮助你,而不是让事情变得复杂。

这个解决方案可能需要调整,我只是在这里输入它,所以可能会有点偏差,但应该基本上可以帮助您实现目标。

祝你编码愉快。

关于java - 使按钮功能像键盘一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53003553/

相关文章:

java - 带直线的 CodenamOne 条形图

android - 按返回键后黑屏

适用于 Android/Iphone 的 PHP 后端 - 特殊字符问题

android - ItemDecoration - 如何创建垂直分隔线?

android - 在 Android MVP 中通过构造函数将 Fragment 的(View)Presenter 传递给 Adapter

java - Android Froyo : Connection to https using trusting all socket factory results in target server failed to respond

java - 从 ZonedDateTime 对象获取该月的最后一天

java - node.getTextContent 在通过 Tomcat 调用时抛出异常

c# - Xamarin 缺少引用

java - 无法从 Kotlin 中的自定义 ResyclerView 扩展 Adapter