【发布时间】:2018-01-05 19:09:22
【问题描述】:
我在 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()
}
}
}
有人知道吗?
【问题讨论】:
-
我在监听器
if(mMessage == null)处加了一个断点,双击后才停止调试 -
只是为了确定,mYesBtn 不起作用?
-
它可以工作,是的,但是在我双击它之后。我希望它在第一次(并且只有一次)点击时工作。
-
@Onik 当然,我已经用 mYesBTn 和 mNoBtn 编辑了我的 XML 文件
-
我应该使用 setOnTouchListener 而不是 setOnClickListener 吗?会有什么不同吗?
标签: android kotlin onclicklistener