Android EditText remain hint visible and right-sided

被刻印的时光 ゝ 提交于 2019-12-12 15:09:31

问题


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:


回答1:


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>



回答2:


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.




回答3:


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'.




回答4:


@ 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...



来源:https://stackoverflow.com/questions/20310415/android-edittext-remain-hint-visible-and-right-sided

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