0

I'm trying to pass OnEditorActionListener to my view. However, the OnEditorActionListener isn't triggered.

class WMScanEmpfaengerFragment : TransactionNavigationFragment<WMScanEmpfaengerViewModel>() {
    private lateinit var binding: FragmentScanEmpfaengerBinding
    override fun getViewModelClass() = WMScanEmpfaengerViewModel::class

    override fun getContentLayoutResId() = R.layout.fragment_scan_empfaenger

    override fun isTransactionRoot() = true

    override var barcodeScannerEnabled = true


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        binding = FragmentScanEmpfaengerBinding.inflate(layoutInflater)
        binding.scaninputView.setOnEditorActionListener( OnEditorActionListener { v, actionId, event ->
                if (event != null && event.keyCode == KeyEvent.KEYCODE_ENTER || actionId == EditorInfo.IME_ACTION_DONE && v.text.isNotBlank()) {
                    setEmpfaenger(v.text.toString().trim())
                    true
                }
                false

        })

Fragment

class ScanInputView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {
    private val binding: ViewScaninputBinding = ViewScaninputBinding.inflate(LayoutInflater.from(context), this)

    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.ScanInputView)
        val hint = typedArray.getString(R.styleable.ScanInputView_hint)
        val imeNext = typedArray.getBoolean(R.styleable.ScanInputView_imeNext, false)
        typedArray.recycle()

        binding.inputId.showSoftInputOnFocus = false
        binding.inputId.addTextChangedListener(AppUtils.onTextChanged { binding.inputLayoutId.error = null })

        setHint(hint)
        if (imeNext)
            binding.inputId.imeOptions = EditorInfo.IME_ACTION_NEXT

        binding.btnKeyboard.setOnClickListener {
            val keyboard = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            if (!binding.inputId.hasFocus()) {
                binding.inputId.requestFocus()
                moveCursorToEnd()
            }
            keyboard.showSoftInput(binding.inputId, 0)
        }

    }

...

    fun setOnEditorActionListener(editorActionListener: TextView.OnEditorActionListener) {
        binding.inputId.setOnEditorActionListener(editorActionListener)

    }
...

View

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:layout_height="wrap_content"
    tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout"
    android:id="@+id/viewScanInput">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/input_layout_id"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintEnd_toStartOf="@id/btn_keyboard"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/img_topbanner">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/input_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:saveEnabled="false"
            android:imeOptions="actionDone"
            android:inputType="text"
            android:maxLines="1"
            android:singleLine="true" />

    </com.google.android.material.textfield.TextInputLayout>

    <ImageButton
        android:id="@+id/btn_keyboard"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginTop="14sp"
        android:background="?attr/selectableItemBackgroundBorderless"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/input_layout_id"
        app:layout_constraintTop_toTopOf="@id/input_layout_id"
        app:srcCompat="@drawable/ic_icon_keyboard"
        app:tint="@color/colorPrimary" />

</merge>

XML View

Can you help me? Thanks

As soon as I press Enter, it must call the method setEmpfaenger.

I have different ways to rewrite the binding.

I change android:imeOptions="actionDone" to android:imeOptions="actionGo"

Nothing helps

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.