gpt4 book ai didi

java - 当 OnFocusChangeListener 应用于包装的 EditText 时,TextInputLayout 不设置动画

转载 作者:行者123 更新时间:2023-11-29 09:52:54 28 4
gpt4 key购买 nike

没有比标题更清楚的了。我有一个由文本输入布局包裹的 EditText。我试图在 EditText 失去焦点时触发一个事件。但是,一旦应用了事件监听器,TextInputLayout 就不再为文本设置动画,它只是停留在 editText 行上,直到输入信息,此时它才会设置动画。谁能告诉我为什么会这样?

编辑:布局文件。

    <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/card_drawable"
android:gravity="center"
android:padding="11dp"
android:text="The fields below will be used to calculate your training maxes. Please enter your best rep record, or 1RM, for each lift. Training maxes will be determined using the following formula provided in Beyond 5/3/1: \n(Weight Lifted x Reps X .0333 + Weight Lifted)" />

<LinearLayout
android:id="@+id/squat_container"
style="@style/linear_layout_style">

<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">

<EditText
android:id="@+id/squatWeight_editText"
style="@style/editText_floating_label"
android:hint="Squat Weight" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
android:id="@+id/txtInputLayoutSquat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">

<EditText
android:id="@+id/squatReps_editText"
style="@style/editText_floating_label"
android:hint="Repetitons" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>

<LinearLayout
android:id="@+id/bench_container"
style="@style/linear_layout_style">

<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">

<EditText
android:id="@+id/bechpressWeight_editText"
style="@style/editText_floating_label"
android:hint="Bench Press Weight" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">

<EditText
android:id="@+id/benchpressReps_editText"
style="@style/editText_floating_label"
android:hint="Repetitons" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>

<LinearLayout
android:id="@+id/deadlift_container"
style="@style/linear_layout_style">

<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">

<EditText
android:id="@+id/deadliftWeight_editText"
style="@style/editText_floating_label"
android:hint="Deadlift Weight" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">

<EditText
android:id="@+id/deadliftReps_editText"
style="@style/editText_floating_label"
android:hint="Repetitons" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>

<LinearLayout
android:id="@+id/ohp_container"
style="@style/linear_layout_style">

<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">

<EditText
android:id="@+id/ohpWeight_editText"
style="@style/editText_floating_label"
android:hint="Overhead Press Weight" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1">

<EditText
android:id="@+id/ohpReps_editText"
style="@style/editText_floating_label"
android:hint="Repetitons" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>


</LinearLayout>

最佳答案

我不得不处理同样的问题。

我使用的解决方法是使用自定义 EditText 并向其添加自定义监听器,以便在触发 onFocusChanged 方法时调用。

自定义EditText(未显示样板代码)

public class EditTextPlus extends AppCompatEditText {
//This is the custom listener you will use
public OnFocusChangeListener customListener;

@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (customListener != null) {
customListener.onFocusChange(this, focused);
}
}
}

在 Activity/fragment 中

yourEditText.customListener = new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
//Do your stuff
}
}

不要忘记在 xml 中也使用自定义的 EditText 而不是默认的 EditText。

关于java - 当 OnFocusChangeListener 应用于包装的 EditText 时,TextInputLayout 不设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31197312/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com