EditText ellipsize (three dots…)

2019-02-17 04:37发布

Unfortunatelly I am not able to make ellipsize for EditText works. Is even possible to put three dots at the end of the text when the text is too long? It is working perfectly for TextiView but not for EditText. Some idea?

 android:id="@+id/ed_email_personalInfo"
 android:layout_width="match_parent"
 android:layout_height="55dp"
 android:background="@color/transparent"
 android:ellipsize="end"
 android:ems="10"
 android:hint="@string/email"
 android:inputType="textEmailAddress"
 android:maxLength="64"
 android:paddingLeft="10dp"
 android:paddingRight="10dp"
 android:singleLine="true"
 android:textColorHint="@color/col9a9a9a"
 android:textSize="15.7sp"

4条回答
兄弟一词,经得起流年.
2楼-- · 2019-02-17 05:03

You have to remove android:inputType attribute.

Ellipsize doesn't work if inputType is defined.

查看更多
趁早两清
3楼-- · 2019-02-17 05:06

Might not be possible in EditText (unless you create your own View). I think the default behavior (for singleLine EditText) is that you can scroll the text sideways when it can't fit in the view.

查看更多
疯言疯语
4楼-- · 2019-02-17 05:09

You need write a new class that extends EditText. for example:

MyEditTextEllipsize extends EditText{

private String dotsString;

private String storeString;

public MyEditTextEllipsize(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

@Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);

         if(focused)

     {
       setText(storeString);
      }else {
             String NOW = getText().toString();
                storeString = NOW;
            if (NOW != null && getWidth() <= getTextSize() * NOW.length()) {

                    dotsString = NOW.substring(0, (int) (getWidth() / getTextSize())) + "...";

                    setText(dotsString);

                }
}

    }
}
查看更多
霸刀☆藐视天下
5楼-- · 2019-02-17 05:26

Set this property to edit text. Elipsize is working with disable edit text

    android:lines="1"
    android:scrollHorizontally="true"
    android:ellipsize="end"
    android:singleLine="true"
    android:editable="false"
查看更多
登录 后发表回答