Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android按钮在设置为View.VISIBLE后需要点击几下才能工作_Java_Android_Xml_Events_Button - Fatal编程技术网

Java Android按钮在设置为View.VISIBLE后需要点击几下才能工作

Java Android按钮在设置为View.VISIBLE后需要点击几下才能工作,java,android,xml,events,button,Java,Android,Xml,Events,Button,我有一个按钮: <Button android:id="@+id/my_btn" android:focusable="true" android:focusableInTouchMode="true" android:layout_width="match_parent" android:layout_height="match_parent" android:text="My Button" android:textColor="

我有一个按钮:

<Button
    android:id="@+id/my_btn"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="My Button"
    android:textColor="@android:color/black"
    android:textSize="20sp"
    android:textStyle="bold" />
在我的代码中,我将按钮的可见性设置为View.INVSISBLE,然后再次设置为View.VISIBLE。当它从不可见变为可见时,我尝试单击它,有时在onclicklistener实际接收事件之前需要单击几下。同时,每当我点击它时,什么也没发生,logcat每次都会记录:

D/ViewRootImpl@1b16f49[MainActivity]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@1b16f49[MainActivity]: mHardwareRenderer.destroy()#4
D/ViewRootImpl@1b16f49[MainActivity]: dispatchDetachedFromWindow
W/InputEventReceiver: Attempted to finish an input event but the input event 
receiver has already been disposed.
D/InputTransport: Input channel destroyed: fd=71
一旦实际接收到按钮单击事件,每隔一次对按钮的单击都会起作用,直到其变为不可见并再次变为可见


我应该如何确保按钮单击事件始终被接收和处理?

根本原因是两个xml属性:
android:focusable=“true”
android:focusableInTouchMode=“true”
,这会导致按钮在其
OnClickListener
被触发之前刚刚聚焦,每次你设置按钮
View.INVISIBLE
,然后再回到
View.VISIBLE
,它就会失去焦点,所以奇怪的现象再次发生


有关更多详细信息,请参阅。

解决方案实际上是,我在本应解除警报时隐藏了警报

我通过调用alert.hide()使警报在几秒钟后自动消失,但这不会删除它们。因此,当焦点从我的主要活动中丢失时,焦点会返回到隐藏的警报,因此按钮在我的主要活动中不起作用,直到我单击屏幕足够多的时间来消除所有隐藏的警报


我将代码更改为调用alert.dismise(),我不再有此问题。

您在哪里绑定此onclick侦听器,以及
handleButtonClick()
方法在哪里?您也可以发布handleButtonClick()代码吗
D/ViewRootImpl@1b16f49[MainActivity]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@1b16f49[MainActivity]: mHardwareRenderer.destroy()#4
D/ViewRootImpl@1b16f49[MainActivity]: dispatchDetachedFromWindow
W/InputEventReceiver: Attempted to finish an input event but the input event 
receiver has already been disposed.
D/InputTransport: Input channel destroyed: fd=71