开发者

How to set the hint properly in a text Box means EditText in android

开发者 https://www.devze.com 2023-03-08 09:22 出处:网络
I am using EditText and providing some Hint there. I am putting it like this: android:hint=\"@string/user_name_hint\"

I am using EditText and providing some Hint there. I am putting it like this:

android:hint="@string/user_name_hint"
android:textColorHint="#ffffff"
android:gravity="left"

But here hint is coming in the left side. But i want the hint is in the middle of t开发者_JS百科he box and when i am clicking the text should start from the left.

How it is possible??


Use this EditText

 <EditText
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:hint="user_name_hint"
    android:textColorHint="#fffff"
    android:gravity="center_horizontal"
    android:id="@+id/editText1"/>

Then you need to add addTextChangedListener to solve your problem.

 final EditText hintText = (EditText) findViewById(R.id.editText1);
            hintText.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (s.length() == 0) {
                        hintText.setGravity(Gravity.CENTER);
                    } else {
                        hintText.setGravity(Gravity.LEFT);

                    }

                }
            });


set Gravity to center.. android:gravity="center_horizontal"


Not sure you can do it in a "straight forward way". Probably some workaround - either suggested by nagesh or by placing a button on top of the edit text and switching to edit text when it gets clicked or focused

0

精彩评论

暂无评论...
验证码 换一张
取 消