处理android kotlin中的点击事件:setOnClickListener仅在双击时才有效

时间:2018-01-05 19:09:23

标签: android kotlin onclicklistener

我在DialogFragment中有一个按钮可以返回另一个活动。但它只在双击时才有效。我的想法已经不多了。在我的XML文件中,我已经尝试了以下内容(以不同的方式组合,但它们都没有工作)

我的整个片段XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="600dp"
    android:layout_height="500dp"
    android:layout_gravity="center"
    android:background="@drawable/bg_rounded_white"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="@dimen/vertical_margin">

    <TextView
        android:id="@+id/tv_payment_done_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/vertical_margin"
        android:gravity="center_horizontal"
        android:text="@string/payment_approved_title"
        android:textColor="@color/bg_color"
        android:textSize="@dimen/text_large"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv_payment_message"
        android:layout_width="550dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/vertical_margin"
        android:gravity="center_horizontal"
        android:minHeight="100dp"
        android:text="Payment done!"
        android:textColor="@color/black_overlay"
        android:textSize="@dimen/text_medium"
        android:textStyle="normal" />

    <LinearLayout
        android:layout_width="600dp"
        android:layout_height="80dp"
        android:layout_marginTop="46dp"
        android:clickable="false"
        android:focusable="false"
        android:gravity="center"
        android:orientation="horizontal">

        <Button
            android:id="@+id/rpv_btn_no_payment_done"
            android:layout_width="250dp"
            android:layout_height="?android:attr/actionBarSize"
            android:background="@drawable/bg_rounded_light_blue"
            android:duplicateParentState="true"
            android:text="@string/txt_btn_no"
            android:textAllCaps="false"
            android:textColor="@color/white_text_color"
            android:textSize="@dimen/text_medium"
            android:textStyle="bold"
            android:visibility="gone" />

        <Button
            android:id="@+id/rpv_btn_yes_payment_done"
            android:layout_width="250dp"
            android:layout_height="?android:attr/actionBarSize"
            android:layout_marginLeft="@dimen/vertical_margin"
            android:background="@drawable/bg_rounded_light_blue"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:focusedByDefault="true"
            android:text="@string/txt_btn_exit"
            android:textAllCaps="false"
            android:textColor="@color/white_text_color"
            android:textSize="@dimen/text_medium"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>

至于我的DialogFragment,我试图将我的setOnClickListener放在onViewCreated()方法中,然后放在onStart()内部和onResume()方法之后。它只在双击时才能继续工作。我还有一个计时器,让对话在10秒后消失,然后它引导我到我想要的活动,所以这不是一个问题,但我真的需要并想要修复它。如果我将计时器设置为5秒或更低,当然,它给人的印象是按钮在点击一次时起作用,但实际上没有。

class PaymentDoneDialogFragment : DialogFragment() {

    private lateinit var mYesBtn: Button
    private lateinit var mActionYes: () -> Unit

override fun onViewCreated(view: View, @Nullable savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    dialog.setCanceledOnTouchOutside(false)
    dialog.window.requestFeature(Window.FEATURE_NO_TITLE)
    dialog.window.setDimAmount(.85f)
    dialog.window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
    dialog.setCancelable(false)
    dialog.setCanceledOnTouchOutside(false)
    dialog.window.decorView.systemUiVisibility = activity?.window!!.decorView.systemUiVisibility
    dialog.window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
    mDialogTitle = view.find(R.id.tv_payment_done_title)
    if (mTitle != null) {
        mDialogTitle.text = mTitle
    }
    mYesBtn = view.find(R.id.rpv_btn_yes_payment_done)
    if (mYesTitle != null) {
        mYesBtn.text = mYesTitle
    }
    mYesBtn.setOnClickListener {
        if (mMessage == null) {
            (activity as PaymentActivity).nextScreensaverActivity()
        } else {
            mActionYes()
        } 
        activity?.supportFragmentManager?.beginTransaction()?.remove(this)?.commit()
    }

    if (mMessage != null) {
        mMessageTextView = view.find(R.id.tv_payment_message)
        mMessageTextView?.text = mMessage
        mNoBtn = view.find(R.id.rpv_btn_no_payment_done)
        if (mNoTitle != null)
            mNoBtn.text = mNoTitle
        mNoBtn.visibility = View.VISIBLE
        mNoBtn.setOnClickListener {
            mActionNo()
            activity?.supportFragmentManager?.beginTransaction()?.remove(this)?.commit()
        }
    }
}

有没有人有一些线索?

1 个答案:

答案 0 :(得分:0)

您应该从android:clickable="false"孩子

中删除android:focusable="false"LinearLayout