In my application in android are many EditText fields. And I ran into a problem with hint. It is not disappearing when EditText is on tuch, but in disappears when i'm starting to write somathing. How can this problem be fixed? Because I need hint to disappear when the EditText field is touched.
在我的android应用程序中有许多EditText字段。我遇到了提示问题。当EditText出现时它并没有消失,但是当我开始写somathing时消失了。如何解决这个问题?因为触摸EditText字段时我需要提示消失。
It is for example:
例如:
<EditText
android:layout_width="260dp"
android:layout_height="30dp"
android:ems="10"
android:inputType="text"
android:id="@+id/driv_lic_num_reg"
android:hint="@string/driver_license_numb"
android:textColor="@color/black"
android:textColorHint="@color/grey_hint"
android:background="@drawable/input_field_background"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="2dp"
android:textCursorDrawable="@color/black"
android:textSize="15dp"
android:paddingLeft="10dp"
android:nextFocusDown="@+id/pass_reg"
android:imeOptions="actionNext"
android:maxLines="1"
android:maxLength="50"
android:focusableInTouchMode="true"/>
10 个解决方案
#1
35
Hint only disappears when you type in any text, not on focus. I don't think that there is any automatic way to do it, may be I am wrong. However, as a workaround I use the following code to remove hint on focus in EditText
键入任何文本时,提示只会消失,而不是焦点。我不认为有任何自动方式可以做到,可能是我错了。但是,作为一种解决方法,我使用以下代码删除EditText中的焦点提示
myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
myEditText.setHint("");
else
myEditText.setHint("Your hint");
}
});
#2
56
You can also custom your XML code by using Selector. Here is an example code.
您还可以使用Selector自定义XML代码。这是一个示例代码。
Create a selector. In file selector.xml
创建一个选择器。在文件selector.xml中
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="@android:color/transparent" />
<item android:color="@color/gray" />
</selector>
In your view
在你看来
android:textColorHint="@drawable/selector"
#3
8
Here's my solution, where the hint disappear when user focus editText and appears again when focus changes to other place if the edittext is still empty:
这是我的解决方案,当用户聚焦editText时提示消失,如果edittext仍然为空,焦点更改为其他位置时再次出现:
editText.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
editText.setHint("");
return false;
}
});
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
editText.setHint("Hint");
}
}
});
hope this helps someone
希望这有助于某人
#4
4
This is my solution:
这是我的解决方案:
final EditText editText = (EditText) findViewById(R.id.activity_edittext);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
editText .setHint("my little hint");
} else {
editText .setHint("");
}
}
});
#5
2
The proposed selector solution by windyzboy doesn't work for EditTexts with centered text, because the cursor stays behind the transparent hint text instead of moving to the center.
windyzboy提出的选择器解决方案不适用于具有居中文本的EditTexts,因为光标位于透明提示文本后面而不是移动到中心。
A good way without polluting your code is to make a custom EditText, since you probably need the same style in the whole application. That way you can avoid adding checks for each EditText separately.
不污染代码的好方法是制作自定义EditText,因为在整个应用程序中可能需要相同的样式。这样您就可以避免单独为每个EditText添加检查。
public class HideHintEditText extends EditText {
CharSequence hint;
public HideHintEditText(Context context) {
super(context);
hint = this.getHint();
}
public HideHintEditText(Context context, AttributeSet attrs) {
super(context, attrs);
hint = this.getHint();
}
public HideHintEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
hint = this.getHint();
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
this.setHint(focused ? "" : hint);
}
}
#6
1
Try
EditText editText = (EditText) findViewById(...);
editText.setHint("");
in your onTouch
logic.
However your hint must be dissapearing when you clicked on edit text. So re-check your app logic.
在你的onTouch逻辑中。但是,当您单击编辑文本时,您的提示必须消失。因此,请重新检查您的应用逻辑。
#7
0
Here i have used the little bit of code to hide and show hint may be it'll be helpful.
在这里,我使用了一点点代码来隐藏和显示提示可能会有所帮助。
txt_username.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(txt_username.getText().toString().isEmpty()){
if(hasFocus){
txt_username.setHint("");
}else{
txt_username.setHint(R.string.username);
}
}
}
});
#8
0
findViewById(R.id.mainLayout).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hideKeyboard();
editText.clearFocus();
if (TextUtils.isEmpty(editText.getText().toString())) {
editText.setHint("your hint");
}
}
});
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
editText.setHint("");
return false;
}
});
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
editText.setHint("");
else
editText.setHint("your hint");
}
});
#9
0
android:hint="User Name"
This code line can be done that easily way!
这条代码行可以轻松完成!
<EditText
android:id="@+id/edtUserName"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:hint="User Name" />
#10
-1
Every Given Solution may work or not I don't know but the Following steps would definitely help.
每个给定的解决方案可能工作与否我不知道,但以下步骤肯定会有所帮助。
First Create a dummy focus to restrain the EditText from gaining focus by
首先创建一个虚拟焦点,以限制EditText获得焦点
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
Then Normally set hint in your EditText as example:
然后通常在EditText中设置提示作为示例:
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="text"
android:ems="10"
android:id="@+id/editText"
android:hint="Your Hint"
android:focusableInTouchMode="true"
/>
Finally add the following code in your activity or fragment where you intend
最后在您想要的活动或片段中添加以下代码
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
editText.setHint("");
return false;
}
});
#1
35
Hint only disappears when you type in any text, not on focus. I don't think that there is any automatic way to do it, may be I am wrong. However, as a workaround I use the following code to remove hint on focus in EditText
键入任何文本时,提示只会消失,而不是焦点。我不认为有任何自动方式可以做到,可能是我错了。但是,作为一种解决方法,我使用以下代码删除EditText中的焦点提示
myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
myEditText.setHint("");
else
myEditText.setHint("Your hint");
}
});
#2
56
You can also custom your XML code by using Selector. Here is an example code.
您还可以使用Selector自定义XML代码。这是一个示例代码。
Create a selector. In file selector.xml
创建一个选择器。在文件selector.xml中
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="@android:color/transparent" />
<item android:color="@color/gray" />
</selector>
In your view
在你看来
android:textColorHint="@drawable/selector"
#3
8
Here's my solution, where the hint disappear when user focus editText and appears again when focus changes to other place if the edittext is still empty:
这是我的解决方案,当用户聚焦editText时提示消失,如果edittext仍然为空,焦点更改为其他位置时再次出现:
editText.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
editText.setHint("");
return false;
}
});
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
editText.setHint("Hint");
}
}
});
hope this helps someone
希望这有助于某人
#4
4
This is my solution:
这是我的解决方案:
final EditText editText = (EditText) findViewById(R.id.activity_edittext);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
editText .setHint("my little hint");
} else {
editText .setHint("");
}
}
});
#5
2
The proposed selector solution by windyzboy doesn't work for EditTexts with centered text, because the cursor stays behind the transparent hint text instead of moving to the center.
windyzboy提出的选择器解决方案不适用于具有居中文本的EditTexts,因为光标位于透明提示文本后面而不是移动到中心。
A good way without polluting your code is to make a custom EditText, since you probably need the same style in the whole application. That way you can avoid adding checks for each EditText separately.
不污染代码的好方法是制作自定义EditText,因为在整个应用程序中可能需要相同的样式。这样您就可以避免单独为每个EditText添加检查。
public class HideHintEditText extends EditText {
CharSequence hint;
public HideHintEditText(Context context) {
super(context);
hint = this.getHint();
}
public HideHintEditText(Context context, AttributeSet attrs) {
super(context, attrs);
hint = this.getHint();
}
public HideHintEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
hint = this.getHint();
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
this.setHint(focused ? "" : hint);
}
}
#6
1
Try
EditText editText = (EditText) findViewById(...);
editText.setHint("");
in your onTouch
logic.
However your hint must be dissapearing when you clicked on edit text. So re-check your app logic.
在你的onTouch逻辑中。但是,当您单击编辑文本时,您的提示必须消失。因此,请重新检查您的应用逻辑。
#7
0
Here i have used the little bit of code to hide and show hint may be it'll be helpful.
在这里,我使用了一点点代码来隐藏和显示提示可能会有所帮助。
txt_username.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(txt_username.getText().toString().isEmpty()){
if(hasFocus){
txt_username.setHint("");
}else{
txt_username.setHint(R.string.username);
}
}
}
});
#8
0
findViewById(R.id.mainLayout).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hideKeyboard();
editText.clearFocus();
if (TextUtils.isEmpty(editText.getText().toString())) {
editText.setHint("your hint");
}
}
});
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
editText.setHint("");
return false;
}
});
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
editText.setHint("");
else
editText.setHint("your hint");
}
});
#9
0
android:hint="User Name"
This code line can be done that easily way!
这条代码行可以轻松完成!
<EditText
android:id="@+id/edtUserName"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:hint="User Name" />
#10
-1
Every Given Solution may work or not I don't know but the Following steps would definitely help.
每个给定的解决方案可能工作与否我不知道,但以下步骤肯定会有所帮助。
First Create a dummy focus to restrain the EditText from gaining focus by
首先创建一个虚拟焦点,以限制EditText获得焦点
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
Then Normally set hint in your EditText as example:
然后通常在EditText中设置提示作为示例:
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="text"
android:ems="10"
android:id="@+id/editText"
android:hint="Your Hint"
android:focusableInTouchMode="true"
/>
Finally add the following code in your activity or fragment where you intend
最后在您想要的活动或片段中添加以下代码
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
editText.setHint("");
return false;
}
});