聚焦后更改 TextInputLayout 提示文本
Change TextInputLayout hint text after focused
在片段中,我想在用户触摸 TextInputEditText 后更改 TextInputLayout 提示文本。在它动画到浮动提示之前。
我可以更改提示没问题,但我希望用户先触摸它,然后再更改它。
我该怎么做?
谢谢
您可以使用 OnFocusChangedListener 并检索用户获得 EditText 焦点的事件。
myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
// do what you have to do
}
}
});
试试这个方法..
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
editText.setHint("Please Enter Name"); // here set new hint
}
});
更改提示文本颜色使用View.OnFocusChangeListener
设置hintTextAppearance
。试试下面的配置。
<style name="inactive" parent="Theme.AppCompat.Light">
<item name="android:textColorPrimary">@android:color/darker_gray</item>
<item name="android:textColor">@android:color/darker_gray</item>
</style>
<style name="active" parent="Theme.AppCompat.Light">
<item name="android:textColorPrimary">@android:color/black</item>
<item name="android:textColor">@android:color/black</item>
</style>
XMl
<android.support.design.widget.TextInputLayout
android:id="@+id/tet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/et"
app:hintTextAppearance="@style/inactive"
android:layout_margin="@dimen/activity_horizontal_margin"
app:hintEnabled="true">
<android.support.design.widget.TextInputEditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:textColor="@color/colorPrimary"
android:layout_height="wrap_content"
android:hint="Floating Hint" />
</android.support.design.widget.TextInputLayout>
更改焦点时更改样式。
final TextInputLayout inputLayout=findViewById(R.id.tet);
final TextInputEditText edit=findViewById(R.id.edit);
edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
inputLayout.setHintTextAppearance(R.style.active);
else
inputLayout.setHintTextAppearance(R.style.inactive);
}
});
编辑:- 要仅更改提示文本,您可以在焦点更改时更改它。
edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
edit.setHint("Enter name");
else
edit.setHint("Name");
}
});
在片段中,我想在用户触摸 TextInputEditText 后更改 TextInputLayout 提示文本。在它动画到浮动提示之前。
我可以更改提示没问题,但我希望用户先触摸它,然后再更改它。
我该怎么做?
谢谢
您可以使用 OnFocusChangedListener 并检索用户获得 EditText 焦点的事件。
myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
// do what you have to do
}
}
});
试试这个方法..
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
editText.setHint("Please Enter Name"); // here set new hint
}
});
更改提示文本颜色使用View.OnFocusChangeListener
设置hintTextAppearance
。试试下面的配置。
<style name="inactive" parent="Theme.AppCompat.Light">
<item name="android:textColorPrimary">@android:color/darker_gray</item>
<item name="android:textColor">@android:color/darker_gray</item>
</style>
<style name="active" parent="Theme.AppCompat.Light">
<item name="android:textColorPrimary">@android:color/black</item>
<item name="android:textColor">@android:color/black</item>
</style>
XMl
<android.support.design.widget.TextInputLayout
android:id="@+id/tet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/et"
app:hintTextAppearance="@style/inactive"
android:layout_margin="@dimen/activity_horizontal_margin"
app:hintEnabled="true">
<android.support.design.widget.TextInputEditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:textColor="@color/colorPrimary"
android:layout_height="wrap_content"
android:hint="Floating Hint" />
</android.support.design.widget.TextInputLayout>
更改焦点时更改样式。
final TextInputLayout inputLayout=findViewById(R.id.tet);
final TextInputEditText edit=findViewById(R.id.edit);
edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
inputLayout.setHintTextAppearance(R.style.active);
else
inputLayout.setHintTextAppearance(R.style.inactive);
}
});
编辑:- 要仅更改提示文本,您可以在焦点更改时更改它。
edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus)
edit.setHint("Enter name");
else
edit.setHint("Name");
}
});