开发者

hint and textview with right gravity and a singleline

I've opened a bug but i was wondering if anyone encountered this issue and knows a workaround. If you define a text view with a hint inside it, give it right gravity (android:gravity="right") then if you define android:singleLine=true or android:maxLines="1" or android:scrollHorizonatally="true" you don't see the hint. removing the right gravity returns the hint to the left side, removing all the tree params i mentioned above puts the hint on the right side. i want my hint on the right, but i need a single horizontal line...

here's the sample layout that doesn't show the hint:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dp">
            <EditText android:layout_width="fill_parent"
                andro开发者_运维技巧id:layout_gravity="center_vertical|right"
                android:layout_height="wrap_content"
                android:layout_margin="6dp"
                android:textSize="16sp"
                android:paddingRight="5dp"
                android:id="@+id/c"
                android:gravity="right"
                android:hint="hello!!!"
                android:scrollHorizontally="true"
                android:maxLines="1"
                android:singleLine="true"/>
</LinearLayout>

i checked on 1.6 and 2.1 emulators and it reproduces 100%, i'm prettysure it's a bug, i don't see the connection between single line and the hint.... what's more the hint got it's own layout in the TextView (mLayout and mHintLayout both exists, in onDraw if the text length is 0 mHintLayout if mHint is not null is used).


Did you try android:ellipsize="start"? This has worked great for me in the past when I've wanted my hint and EditText to be centered.


Looks like you're exactly right with the issue; I tried playing with your example layout and saw the same issue. I assume this is your bug report.

The easiest solution is to just change your layout, but that's probably not what you want to do. My first attempt at a work around would be to try not setting any of those three attributes in XML and then setting them in Java. If that doesn't work...

One option is to mimic the hint by either extending the EditText class and attempting to fix the code that lays out the hint yourself, or by overriding the onDraw method to create the hint, or perhaps by simply overlapping a regular TextView on top of the EditText, which you then show/hide manually. You could even have the view check if it's empty, and if so set the text to your hint text and change the color. When the view gains focus, check if its text is equal to your hint and, if so, remove the text and change the color back.

Another possible workaround that's a bit more "hacky" is to leave off the three attributes that cause problems, but try to manually prevent a newline from being created. You'd need to create an OnKeyListener for your EditText, something like this:

editText.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                // do nothing
                return true;
            }
            return false;
        }
    });

You would also want to call editText.setImeOptions(EditorInfo.IME_ACTION_NEXT) to avoid showing the return key. It still may be possible to create a newline in your text field by pasting into it or perhaps some other method, so you would also want to parse and remove newlines when the form is submitted just to be safe. This is also not likely to do what you want as far as horizontal scrolling.


use these properties with hint and single line...u can chnge gravity!!

            android:gravity="right"
            android:ellipsize="start"
            android:imeOptions="actionNext"             
            android:singleLine="true"


Noting worked good enough for me. When I set Gravity.right, the cursor was always on the right and couldn't be placed in the middle of the word.

I tried a different approach - put the set the gravity the the right when there is no text (or left, if it works for you) and let android decide the best direction once the user entered something

This worked for me:

  1. create TextWatcher class

    private static class FilterTextWatcher implements TextWatcher {
    
        private WeakReference<Activity> mActivity;
    
        public FilterTextWatcher(Activity activity) {
            mActivity = new WeakReference<Activity>(activity);
        }
    
        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) {
            if (mActivity.get() == null)
                return;
    
            EditText searchTxtBx = mActivity.get().mSearchTxtBx;
            if (searchTxtBx.getText().toString().length() == 0)
                searchTxtBx.setGravity(Gravity.RIGHT);
            else
                searchTxtBx.setGravity(0);
        }
    }
    
  2. use it as class member

    private TextWatcher mFilterTextWatcher = new FilterTextWatcher(this);
    
  3. in onCreate():

    mSearchTxtBx.addTextChangedListener(mFilterTextWatcher);
    
  4. in onDestroy():

    mSearchTxtBx.removeTextChangedListener(mFilterTextWatcher);
    mFilterTextWatcher = null;
    


What do you think about my solution to this problem?

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus == false && StringUtils.isNotBlank(editText.getText().toString())) {
                editText.setGravity(Gravity.RIGHT);
            }
        }
    });

And the corresponding XML File:

<EditText android:id="@+id/the_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="number" android:hint="@string/edit_text_prompt"/>

Works fine for me: just one line, no enter-key possible, shows me the hint and when I leave the field after some input was given, the text appears right-aligned.


it worked with me when I added:

android:hint="the hint text ..."
android:singleLine="true"
android:ellipsize="start"

and in my activity i added :

myedittext.setOnKeyListener(new OnKeyListener() {
    @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
          if(keyCode==KeyEvent.KEYCODE_ENTER&&event.getAction()==KeyEvent.ACTION_DOWN){
                    // do nothing
                    return true;
                }
          return false;
    }
});


I noticed this issue when my TextView atrs are:

android:singleLine="true"  
android:gravity="right"

When I try to Linkify the textview or setMovementMethod(LinkMovementMethod.getInstance()) on that textview, the text is just gone.

android:ellipsize="start" 

solved my issue, because I use Arabic text in my app.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜