其他视图上的浮动按钮 - Android

Floating button over other views - Android

我想制作一个能够显示在 EditTexts 上方的按钮或视图 当单击 时,将对特定的焦点执行某些操作 EditText。我不知道它是怎么称呼的,也不知道是否有任何 3d 方库可以做到这一点。

这就是 RelativeLayouts 在这里发挥重要作用的时候,因为您可以将视图放置在另一个视图之上,而顶部的视图具有焦点,反之亦然。所以首先你要编译 Android 的最新支持设计库到你的本地 build.gradle 文件中以使用 FAB,然后对布局文件做这样的事情:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    tools:context="com.davenotdavid.Whosebug.MainActivity">

    <EditText
        android:id="@+id/et1"
        android:hint="HINT..."
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/et2"
        android:hint="HINT..."
        android:layout_below="@id/et1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/et3"
        android:hint="HINT..."
        android:layout_below="@id/et2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" />

</RelativeLayout>

... 然后也许通过在您的 onCreate() 中显示 Toast 消息(而不是弹出 EditText 的键盘)来测试 FAB 是否已将焦点放在聚焦的 EditText 上,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "FAB clicked", Toast.LENGTH_SHORT).show();
        }
    });
}