【发布时间】: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