문제

How to make hint in EditText visible during user input and after it? And is it posible to place hint on the right side of EditText control, but user input text - on the left side?

Here is an example what I'm looking for:

Both hint and user input are visible

도움이 되었습니까?

해결책

So I've found pretty simple solution, a bit tricky, but it works - just to to make relative layout with EditText for input and TextView for hint inside:

<RelativeLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Some text"
            android:layout_alignParentLeft="true"
            android:id="@+id/editText2"
            android:layout_gravity="center"
            />
    <TextView
            android:id="@+id/text_hint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="HintText"
            android:textSize="18sp"
            android:layout_marginRight="10dp"
            android:textColor="#808080"
            />
</RelativeLayout>

다른 팁

Im not sure it is possible by the widget mechanics. What I would try is to manage its content with a function to be called after every callback (you have to set a listener to the edittext). Try adding this listener:

textMessage = (EditText)findViewById(R.id.textMessage);
textMessage.addTextChangedListener(new TextWatcher(){
    public void afterTextChanged(Editable s) {}
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){
         /*here goes your function*/     }
}); 

Then for the function use parsed HTML to give the grey'ish 'hint' color several spaces after the user's input. You can use this formula:

String tmpHtml = "<html>a whole bunch of html stuff</html>";
String htmlTextStr = Html.fromHtml(tmpHtml).toString();

Hope this gives you an idea on how to implement it.

You can use onFocusChangeListener to display the editText when the user is entering text into the field.

myEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
           myEditText.setHint(R.string.hint);
        }
      }
    });

And regarding the alignment of the hint, It is not possible to right align the hint. Atleast it is not straightforward. In normal cases, Hint is designed to be visible only when there is no text in the EditText box and it has the same gravity as the editText content. You may try to have an accompanying TextView on the right besides the editText, if you want to provide a persistent 'hint'.

@ Sergii,

I agree with you.

Here My Answer Code with Same XML

private void setHintRight(final EditText et, final TextView tv) {
        et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus == false && TextUtils.isEmpty(et.getText().toString())) {
                    tv.setVisibility(View.VISIBLE);
                }
            }
        });
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                if (et.getText().length() == 0) {
                    tv.setVisibility(View.VISIBLE);
                } else {
                    tv.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

Here, you have to pass the edittext with left gravity and and Textview with right gravity...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top