【问题标题】:Android buttons in xml with onClickListeners, have to click a second time for the button to Clickxml中带有onClickListeners的Android按钮,必须再次单击按钮才能单击
【发布时间】:2016-11-15 12:21:50
【问题描述】:

尝试了各种解决方案,包括这里的解决方案:

Have to click a button twice for it to work in Android Studio 和这里:

I have to click the button twice for it to work

并且已经在 xml 文件中的按钮上尝试了“android:focusableInTouchMode="false"”,但也没有用。

我目前的代码如下:

SplashActivity.java

//THIS BIT IS IN THE ONCREATE METHOD
    Button enterButton = (Button) findViewById(R.id.enter_button);
    enterButton.setOnClickListener(OnClick);

    Button bookButton = (Button) findViewById(R.id.book_button);
    bookButton.setOnClickListener(OnClick);

}

//THIS BIT IS OUTSIDE OF THE ONCREATE METHOD
private OnClickListener OnClick = new OnClickListener() {

    public void onClick(View v) {
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        switch (v.getId()) {
            case R.id.enter_button:

                handler.removeCallbacksAndMessages(null);
                startActivity(intent);

                break;
            case R.id.book_button:

                handler.removeCallbacksAndMessages(null);
                intent.putExtra("source", "onClick");
                startActivity(intent);

                break;
            default:
                break;
        }
    }
};
}

activity_splash.xml

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
             >


            <Button
                android:id="@+id/enter_button"
                android:theme="@style/AppTheme.DevButton"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:text="Enter"
                android:stateListAnimator="@null"
                />


            <Button
                android:id="@+id/book_button"
                android:theme="@style/AppTheme.DevButton"
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:text="Book"
                android:stateListAnimator="@null"
                />

    </LinearLayout>

真的不知道如何解决这个问题!

更新到 -activity_splash.xml

所以我已将其范围缩小到 Java 文件,这似乎不是导致问题的 xml 代码,因为我已将 xml 文件缩减为以下内容并且症状相同:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     >

            <Button
                android:id="@+id/enter_button"
                android:layout_width="100dp"
                android:layout_height="50dp"

                />


            <Button
                android:id="@+id/book_button"
                android:layout_width="100dp"
                android:layout_height="50dp"
                />

</LinearLayout>

【问题讨论】:

    标签: java android android-layout


    【解决方案1】:

    我通常会扩展 SplashActivity 以包含 View.OnClickListener 并实现

    Button button = (Button)findViewById(R.id.button);
    button.setOnClickListener(this);
    
    @Override 
    public void onClick(View view_) {
        ...
    }
    

    不确定这是否会有所作为?

    【讨论】:

    • 这实际上是我最初的样子,我把它改成了现在的样子,看看它是否会起作用,我害怕无论如何都不高兴。不过感谢您的建议。
    【解决方案2】:

    我认为焦点是父视图,所以将android:descendantFocusability= 属性添加到您的父视图

    【讨论】:

    • 通过将 'android:descendantFocusability="blocksDescendants"' 放在 LinearLayout、按钮本身、RelativeLayout 上进行了尝试,但恐怕它并没有做到这一点。我应该在代码中的其他地方放置它吗?
    • 我还尝试了android:descendantFocusability="afterDescendants" 的父母,但恐怕也没有用。
    【解决方案3】:

    所以经过所有测试后,我设法找出了问题所在。

    正是这里的这行代码有助于将图像显示为全屏:

    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    

    SYSTEM_UI_FLAG_HIDE_NAVIGATION 标志不注册触摸事件,因此您需要将其更改为SYSTEM_UI_FLAG_IMMERSIVE,因此其内容如下:

    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE);
    

    但请注意,此标志仅在 Android 4.4 API 级别 19 之后发布,因此在此之前的 Android 版本中不起作用。

    感谢大家也为我调查此事,非常感谢。

    【讨论】:

      猜你喜欢
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 2013-10-31
      • 2012-10-01
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多