【发布时间】:2016-04-18 23:00:58
【问题描述】:
我不知道是什么突然导致了这种情况,因为我认为我没有更改任何代码(也许这是 Android Studio 2.0 的错误?)。
但是现在,当我按下 FloatingActionButton 时,它会一直保持按下状态,并且在我第一次单击它时不会执行任何操作。如果我再次开始单击它,它似乎可以工作(但看起来仍然被按下)。
在我的 OnCreate 方法中:
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
assert fab != null;
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//does stuff here, behavior is the same even if I remove this code
}
});
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_input_add"
android:baselineAlignBottom="false"
android:focusableInTouchMode="true"
/>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
【问题讨论】:
-
为什么你有
assert fab != null;? -
只是为了摆脱 Android Studio 给我的警告;可能不需要
标签: android android-studio android-activity android-xml floating-action-button