简体   繁体   English

带有onClickListeners的xml中的Android按钮,必须再次单击该按钮才能单击

[英]Android buttons in xml with onClickListeners, have to click a second time for the button to Click

Have tried various solutions including those in here: 尝试了各种解决方案,包括此处的解决方案:

Have to click a button twice for it to work in Android Studio and here: 必须单击两次按钮才能使其在Android Studio中工作,如下所示:

I have to click the button twice for it to work 我必须单击两次按钮才能正常工作

And have tried " android:focusableInTouchMode="false" " on the buttons in the xml file which did not work either. 并尝试了在xml文件中的按钮上都无法正常运行“ android:focusableInTouchMode =“ false”“。

My current code is as follows: 我当前的代码如下:

SplashActivity.java 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 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>

Really can't work out how to work around this! 真的无法解决该问题!

UPDATE to - activity_splash.xml 更新至-activity_splash.xml

So I've narrowed this down to the Java file, it doesn't seem to be the xml code causing the issue as I've trimmed down the xml file to the following and the symptoms are the same: 因此,我将其范围缩小到了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>

I would normally extend SplashActivity to include View.OnClickListener and implement 我通常将SplashActivity扩展为包括View.OnClickListener并实现

Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);

@Override 
public void onClick(View view_) {
    ...
}

Not sure if this would make a difference or not? 不确定是否会有所作为?

我认为重点是父视图,因此将android:descendantFocusability=属性添加到父视图

So after all of the testing I've managed to find out what the problem was. 因此,在进行所有测试之后,我设法找出了问题所在。

It was this line of code here which helps with display the image as full screen: 这行代码有助于将图像全屏显示:

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

The SYSTEM_UI_FLAG_HIDE_NAVIGATION flag does not register touch events so you need to change it to SYSTEM_UI_FLAG_IMMERSIVE so it reads as follows: SYSTEM_UI_FLAG_HIDE_NAVIGATION标志不会注册触摸事件,因此您需要将其更改为SYSTEM_UI_FLAG_IMMERSIVE因此其内容如下:

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

Note however this flag was only released after Android 4.4 API Level 19 so won't work in Android versions before this. 但是请注意,此标志仅在Android 4.4 API级别19之后发布,因此在此之前的Android版本中将不起作用。

Thanks guys for looking into this as well for me, most appreciated. 非常感谢你们也为我研究了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM