android:hint not disappearing onFocus using EditText [duplicate]

最后都变了- 提交于 2019-12-08 08:17:06

问题


I am on Android 3+ and I am trying to add hints to my edit text widgets. I tried adding the hint to the layout as follows...

<EditText
        android:id="@+id/bar_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:hint="@string/bar_name_hint"
        android:imeOptions="actionNext"
        />

But when I focus on the Text box it writes over the hint instead of the hint disappearing.

Hint Issue http://desmond.imageshack.us/Himg580/scaled.php?server=580&filename=barhintprob.png&res=crop

I found documentation on adding a onFocus listener to the EditText, but I would like to avoid doing this programatically. The post below also mentioned using selectors but I can't find documentation on how to do that.

Android EditText Hint

So what is the best way to handle this?


回答1:


For some reason, I had the activity and fragment layout set twice. On Activity onCreate:

protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.edit_consumption);

and on the fragment onCreateView:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.edit_consumption, null);

So I had 2 layers of layout. I only had to remove that layout from the Activity onCreate to fix the problem. Maybe you have the same problem.




回答2:


You can do it by pragmatically

EditText name = (EditText)findViewById(R.id.bar_name); 
name.setHint = "user name"

And delete text hint in your XML



来源:https://stackoverflow.com/questions/9656492/androidhint-not-disappearing-onfocus-using-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!