我目前正在开发我的第一个 Android 应用程序,因此我正在使用 fab-speed-dial .我已经实现了 float 操作按钮,以便选项满足我的需要。但现在我需要为菜单项分配操作。
例如如果在下面的屏幕截图中单击“添加房间”,我想显示一个输入字段:
我试过添加一些监听器,但显然我做错了什么。这是我目前在 MainActivity.java 中使用的代码
FabSpeedDial fabSpeedDial = (FabSpeedDial) findViewById(R.id.fab_speed_dial);
fabSpeedDial.setMenuListener(new SimpleMenuListenerAdapter() {
@Override
public boolean onMenuItemSelected(MenuItem menuItem) {
//what do I do here?
return false;
}
});
我的 activity_main.xml 看起来像这样:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<io.github.yavski.fabspeeddial.FabSpeedDial
android:id="@+id/fab_speed_dial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
app:fabGravity="bottom_end"
app:fabMenu="@menu/menu_speeddial"
app:miniFabBackgroundTint="@android:color/white"
app:miniFabDrawableTint="?attr/colorPrimaryDark"
app:miniFabTitleTextColor="?attr/colorPrimaryDark" />
</FrameLayout>
最后但并非最不重要的是 menu_speeddial.xml 中的菜单条目
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/add_room"
android:icon="@drawable/ic_action_home"
android:title="@string/menu_item_room"
android:onClick="gotClicked"/>
<item
android:id="@+id/add_furniture"
android:icon="@drawable/ic_action_armchair"
android:title="@string/menu_item_furniture"/>
</menu>
如能提供有关如何处理此问题的任何帮助,我们将不胜感激。
最佳答案
我没用过这个库。但是看到您的代码,我猜想问题出在 MenuListener 中。
@Override
public boolean onMenuItemSelected(MenuItem menuItem) {
if (menuItem.getItemId()==1){
System.out.print("number 1 clicked");
}
return false;
}
我认为,您必须检查资源 ID 是否相等。像这样,
if (menuItem.getItemId() == R.id.add_room){
System.out.print("number 1 clicked");
}
关于java - 使用快速拨号 float 操作按钮处理菜单项选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37074178/