我在大EditText上有一个FloatingActionButton。当我启动应用程序并尝试点击FloatingActionButton时,键盘将向上翻转,因为它需要点击EditText。
我需要在代码中进行哪些更改,以使FloatingActionButton位于EditText上方?
我发现我需要在最后添加FloatingActionButton,就像我已经做的那样,使它位于最上面,但是没有用。我还实现了一个onClickListener,我想这不是问题。
这是我的布局的屏幕截图:Layout Screenshot
<?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="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/listviewbackground"
android:backgroundTint="#80FFFFFF"
android:backgroundTintMode="src_over"
>
<EditText
android:id="@+id/itemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:hint="What do you need to do?"
android:textAlignment="center"
android:textAppearance="?attr/textAppearanceListItem"
android:textSize="30dp"
android:background="@android:color/transparent"/>
<android.support.constraint.ConstraintLayout
android:id="@+id/constraintLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/itemTitle"
android:paddingTop="50dp"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="2"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
<TextView
android:id="@+id/DueDate"
android:layout_width="0dp"
android:layout_height="39dp"
android:layout_marginEnd="64dp"
android:layout_marginStart="40dp"
android:layout_weight="1"
android:text="Due Date"
android:textAlignment="textStart"
android:textSize="24dp"
app:layout_constraintEnd_toStartOf="@+id/Date"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="0dp" />
<TextView
android:id="@+id/Date"
android:layout_width="110dp"
android:layout_height="33dp"
android:layout_marginEnd="32dp"
android:layout_weight="1.2"
android:clickable="true"
android:focusable="false"
android:onClick="openCalendarView"
android:textAlignment="center"
android:textSize="18dp"
app:layout_constraintEnd_toEndOf="parent"
tools:layout_editor_absoluteY="0dp" />
<TextView
android:id="@+id/Important"
android:layout_width="149dp"
android:layout_height="39dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="40dp"
android:layout_marginTop="8dp"
android:text="Is important?"
android:textAlignment="textStart"
android:textSize="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/DueDate"
app:layout_constraintVertical_bias="0.0" />
<Switch
android:id="@+id/switch1"
android:layout_width="51dp"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_toRightOf="@id/Important"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.59"
app:layout_constraintStart_toEndOf="@+id/Important"
app:layout_constraintTop_toBottomOf="@+id/Date"></Switch>
</android.support.constraint.ConstraintLayout>
<EditText
android:id="@+id/Description"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_below="@id/constraintLayout1"
android:background="@android:color/transparent"
android:hint="Put in your description"
android:inputType="textPersonName"
android:nextFocusLeft="@id/Description"
android:nextFocusUp="@id/Description"
android:paddingTop="20dp"
android:textAlignment="center"
android:textSize="18dp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingButtonDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@drawable/check"
android:backgroundTint="@color/Green"
android:layout_alignBottom="@id/Description"
android:layout_alignParentRight="true"
android:focusable="true"
/>
</RelativeLayout>
Here my onClickListener
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_itemdetail, container, false);
//Getting Data and Creation of ToDoItem Object
FloatingActionButton floatingbutton = view.findViewById(R.id.floatingButtonDone);
floatingbutton.bringToFront();
floatingbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Getting Data
try {
//Do something
} catch (Exception e) {
Snackbar.make(view, "You need to set a Title", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}
});
最佳答案
将FloatingActionButton放在框架布局中,并相应设置其重力和填充位置。
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingButtonDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/Green"
android:contentDescription="@null"
android:gravity="bottom|end"
android:padding="3dp"
android:src="@drawable/check" />
</FrameLayout>
关于java - 无法单击EditText上的FloatingActionButton,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47817695/