Android 为什么我的按钮在第一次点击时不起作用?

Android 为什么我的按钮在第一次点击时不起作用?,android,events,button,click,Android,Events,Button,Click,我有一个问题,当我第一次使用按钮时,它不会生成点击事件,但是如果我点击屏幕而不是按钮,然后点击它。它直接工作 在我的fragment onCreateView中,我有: viewAnimator = (ViewAnimator) inflater.inflate(R.layout.fragment_login_supplier, container, false); initView(viewAnimator); 在我看来: private void initView(View

我有一个问题,当我第一次使用按钮时,它不会生成点击事件,但是如果我点击屏幕而不是按钮,然后点击它。它直接工作

在我的fragment onCreateView中,我有:

    viewAnimator = (ViewAnimator) inflater.inflate(R.layout.fragment_login_supplier, container, false);
    initView(viewAnimator);
在我看来:

private void initView(ViewAnimator ll) {
......

    errorButton = (Button) errorLayout.findViewById(R.id.buttonError);
    errorButton.setBackgroundResource(btnErrorSelector);
    errorButton.setOnClickListener(FragmentLoginSupplier.this);
.....
}

我的片段实现了OnClickListener,但我的:@Override public void onClick(View vue){} 第一次什么也没收到

按钮id:buttonError

这里是布局的开始部分:



您可能在某个地方有一个第一次运行的验证事件处理程序。查看选项卡序列中最后一个控件中的验证。你可能会在那里找到一些东西。

我不知道这是否能解决你的问题。但就连我在按钮单击方面也遇到了问题。这段代码对我来说是可行的。我认为在第一种情况下,第一次单击只会集中注意力。第二次点击是“真实”点击

并在Xml中使
为true

如果成功了,请告诉我。

只需设置错误按钮:

errorButton.setFocusableInTouchMode(false);
或添加

errorButton.setOnTouchListener(new OnTouchListener(){

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        if (MotionEvent.ACTION_UP == event.getAction())
        {
            view.performClick();
            return true;
        }
    return false;
    }
});

我通过简单设置解决了这个问题:

<ImageButton
    android:id="@+id/imageButtonFiltroNome"
    ...
    android:clickable="true"
    android:focusable="false"
    android:focusableInTouchMode="false" />

我也面临同样的问题,让我告诉你我是如何解决的。 我在表单中有xml

<RelativeLayout
    android:id="@+id/rlContactUs"
    style="@style/SettingsItemContainer" >
    <com.CustomTextView
        android:id="@+id/tvContactUs"
        style="@style/SettingsItem"
        android:text="@string/contactUs"/>
</RelativeLayout>

SettingsItemContainer具有以下样式

<style name="SettingsItemContainer">
other things
    <item name="android:clickable">true</item>
    <item name="android:focusable">true</item>
    <item name="android:focusableInTouchMode">true</item>
</style>

其他事情
真的
真的
真的
现在,即使在触摸屏上,相对布局也没有收到第一次点击。所以我把它改成:

<style name="SettingsItemContainer">
other things
    <item name="android:clickable">true</item>
    <item name="android:focusable">true</item>
    <item name="android:focusableInTouchMode">false</item>
</style>

其他事情
真的
真的
假的

这解决了问题

如果控件位于可滚动容器内,请在容器上设置:

android:descendantFocusability="afterDescendants"

那么我也受了这个问题的困扰。 在我的例子中,我在
NestedScrollView
中有一个表单和一个按钮。 我可以根据这个答案解决这个点击问题


基本上,您需要将此costum布局行为添加到AppBar。

让我们看看您的实际代码,而不仅仅是您的布局。我确信您已将onClickListener设置为onCreate以外的其他位置的按钮。为什么要使用此
android:layout\u height=“match\u parent”
我们能看到这个xml的快照吗?你的活动代码在哪里?@Ascorbin我的setOnClickListener在oncreated中调用了一个方法。另外,看看这个方法是否有效:
setFocusabeInTouchMode(false)
在这个场景中没有效果。不,没有效果。令人惊讶的是它有效!!单击可调真但可调焦假和android:focusableInTouchMode假,特别是在使用相机界面或其后面的surfaceview时!!。你能解释一下为什么“点击”按钮首先会聚焦,然后从第二次开始就被点击了吗?按钮通常不会以这种方式工作,但为什么会发生这种情况?对于自定义键盘中的某些视图,我面临着同样的问题。对于普通视图组视图,DegenantFocusability=“AfterDegenders”不适用于我。我在视图组布局中使用了android:genderantfocusability=“blocksDescendants”,这对我很有用。更改DegenantFocusability属性是解决我的问题的关键。
<style name="SettingsItemContainer">
other things
    <item name="android:clickable">true</item>
    <item name="android:focusable">true</item>
    <item name="android:focusableInTouchMode">true</item>
</style>
<style name="SettingsItemContainer">
other things
    <item name="android:clickable">true</item>
    <item name="android:focusable">true</item>
    <item name="android:focusableInTouchMode">false</item>
</style>
android:descendantFocusability="afterDescendants"