How to create EditText hint as Text with image in android

前端 未结 2 427
天涯浪人
天涯浪人 2021-01-04 19:53

How to create EditText hint/placeholder with Text & image in android. I write like this



        
相关标签:
2条回答
  • 2021-01-04 20:11

    What you can do is set an drawableLeft as you did in your xml and add an addTextChangedListener to your EditText. When typing occurs this listener will be warned and you can check in the listener if the textsize is bigger than 0. If so, then just set the drawableLeft assigned in the xml programmatically to null.

    @Override
    public void afterTextChanged(Editable s) {
        if(s.toString().length() > 0) {
            editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        } else {
            //Assign your image again to the view, otherwise it will always be gone even if the text is 0 again.
            editText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.image, 0, 0, 0);
        }
    }
    
    0 讨论(0)
  • 2021-01-04 20:14

    I did this by placing my EditText inside a FrameLayout, and then also including a TextView inside the FrameLayout. The TextView serves as the hint text. I have a drawableStart attribute on the TextView. When the user starts typing the TextView is hidden using code similar to that in the accepted answer from Dion.

    0 讨论(0)
提交回复
热议问题