[Android OnClickListener仅在第二次点击时触发

问题描述 投票:2回答:3

我的OnClickListener仅在第二次单击时被调用。同一View的OnLongClickListener可以正常工作。我尝试改用OnTouchListener,但是在滑动时显然会触发。

我的侦听器是我在活动中实现的接口的抽象方法:

interface OnVocableFlashcardFragmentInteractionListener {
    fun onEditTextLongClick(view: View): Boolean
    fun onEditTextClick(view: View)
}

我在RecyclerViewAdapter类中这样设置View的侦听器:

init{
    setHasStableIds(true)

    mEditTextOnClickListener = View.OnClickListener {
        mListener.onEditTextClick(it)
    }

    mEditTextOnLongClickListener = View.OnLongClickListener {
        mListener.onEditTextLongClick(it)
    }
}

override fun onBindViewHolder(holder: FlashcardViewHolder, position: Int) {
    ...
    editText.let { it.tag = it.keyListener; it.keyListener = null; }
    editText.setOnClickListener(mEditTextOnClickListener)
    editText.setOnLongClickListener(mEditTextOnLongClickListener)
    ...
}

我的活动中监听器的实现如下:

override fun onEditTextClick(view: View) {
    //-- only show toast if view is not editable (becomes editable on LongClick)
    if ((view as EditText).keyListener == null) {
        if (mToast != null) {
            mToast!!.cancel()
        }
        //-- inform user to long press to edit entry
        mToast = Toast.makeText(this, resources.getString(R.string.long_click_to_edit), Toast.LENGTH_LONG)
        mToast!!.show()
    }
}

override fun onEditTextLongClick(view: View): Boolean {
    //-- I saved the KeyListener in the editTexts tag attribute
    //-- to make it clickable again when needed
    (view as EditText).keyListener = view.getTag() as KeyListener
    showSoftKeyboard(view)
    return true
}

我的视图的XML如下所示:

            <EditText
            android:id="@+id/et_vocable_word"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:background="@null"
            android:textStyle="bold"
            android:hint="@string/enter_word"
            android:imeOptions="actionNext"
            android:inputType="textNoSuggestions"
            android:maxLines="1"
            android:singleLine="true" />

视图的父母及其父母的父母未声明为android:focusable="true"android:clickable="true"

在我的AndroidManifest.xml中,我为活动设置了android:windowSoftInputMode="stateHidden",以防止活动开始时显示SoftInput。

我是不是做错了什么,或者为什么只在第二次点击时才调用OnClickListener?有谁知道我该如何解决问题?

OnClickListener not triggered on first click

java android kotlin android-edittext onclicklistener
3个回答
1
投票

您必须将编辑文本的可聚焦属性设置为false,因为它的默认值是auto,这意味着框架确定它必须为true和false。当第一次触摸它为true时,它已经被键盘聚焦。

https://developer.android.com/reference/android/R.styleable#View_focusable


0
投票

我能够通过使用OnTouchListener而不是OnClickListener来实现所需的行为,如下所示:

override fun onEditTextTouch(editText: EditText, event: MotionEvent): Boolean {
    //-- if the pressed gesture has finished
    if (event.action == MotionEvent.ACTION_UP)
    //-- only show toast if view is not editable (becomes editable on LongClick)
        if (editText.keyListener == null) {
            if (mToast != null) {
                mToast!!.cancel()
            }
            //-- inform user to long press to edit entry
            mToast = Toast.makeText(this, resources.getString(R.string.long_click_to_edit), Toast.LENGTH_LONG)
            mToast!!.show()
        }
    return false
}

在我以前用touchListener替换clickListener的尝试中,我忘记了检查(event.action == MotionEvent.ACTION_UP),因此在刷卡时也执行了我的代码(我不想这样做)


0
投票

我从另一个有相同问题的话题获得了答案。

对我有用的是:

将此行添加到XML中的按钮并尝试:

android:focusable =“ false”

© www.soinside.com 2019 - 2024. All rights reserved.