微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

EditText 椭圆大小三点

如何解决EditText 椭圆大小三点

我想自定义 android 编辑文本,以便在外部单击时,如果文本太长,它会自动回滚到第一个并在末尾显示三个点。 我已经定制过,试过运行良好,但还没有优化,请参考,如果可能的话,给我一个更优化的方法


class CustomTextInputEditText(context: Context,attrs: AttributeSet) :
    TextInputEditText(context,attrs) {
    private var dotsstring: String? = null
    private var storeString: String? = null
    private var mWidthMeasureSpec: Int = 0
    override fun onMeasure(widthMeasureSpec: Int,heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec,heightMeasureSpec)
        Log.e("CustomTextInputEditText","onMeasure 1: $mWidthMeasureSpec")
        mWidthMeasureSpec = MeasureSpec.getSize(widthMeasureSpec)
    }
    override fun onSizeChanged(w: Int,h: Int,oldw: Int,oldh: Int) {
        super.onSizeChanged(w,h,oldw,oldh)
        Log.e("CustomTextInputEditText","onMeasure 2: $mWidthMeasureSpec")
        mWidthMeasureSpec = width
        setText(text,BufferType.norMAL)
    }
    override fun setText(text: CharSequence?,type: BufferType) {
        if (!isFocused) {
            if (!text.isNullOrEmpty()) {
                val tmpString = text.toString()
                val textWidth = this.paint.measureText(tmpString)
                Log.e("CustomTextInputEditText"," textWidth = " + textWidth)
                Log.e("CustomTextInputEditText"," mWidthMeasureSpec = " + mWidthMeasureSpec)
                storeString = tmpString
                dotsstring = tmpString
                if (mWidthMeasureSpec > 0 && mWidthMeasureSpec <= textWidth + context.convertDpToPx(69f)) {
                    if (dotsstring?.length!! >= (mWidthMeasureSpec / textWidth * tmpString.length - 5).toInt() && (mWidthMeasureSpec / textWidth * tmpString.length) >= 5)
                        dotsstring = tmpString.substring(0,(mWidthMeasureSpec / textWidth * tmpString.length - 5).toInt()) + "..."
                }
                Log.e("CustomTextInputEditText","setText: $dotsstring")
            }
            Log.e("CustomTextInputEditText","not focus: $dotsstring")
            super.setText(dotsstring,type)
        } else {
            Log.e("CustomTextInputEditText","focus: $dotsstring")
            super.setText(storeString,type)
        }
    }

    override fun onFocusChanged(focused: Boolean,direction: Int,prevIoUslyFocusedRect: Rect?) {
        super.onFocusChanged(focused,direction,prevIoUslyFocusedRect)
        if (focused) {
            setText(storeString)
        } else {
            val tmpString = text.toString()
            val textWidth = this.paint.measureText(tmpString)
            storeString = tmpString
            if (mWidthMeasureSpec <= textWidth) {
                dotsstring = tmpString.substring(
                    0,(mWidthMeasureSpec / textWidth * tmpString.length - 10).toInt()
                ) + "...";
                setText(dotsstring)
            }
        }
    }
    fun getRawText(): String {
       if (storeString == null)
           return ""
        else
           return storeString!!
    }
}

我的xml

  <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/til_select_address"
                style="@style/TextInputLayoutStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingStart="@dimen/margin_10"
                android:paddingTop="@dimen/margin_16"
                android:hint="@string/select_address"
                android:paddingEnd="@dimen/margin_10">

                <vn.leaftech.f99fruits.utils.custom_view.CustomTextInputEditText
                    android:id="@+id/edt_select_address"
                    style="@style/TextInputEditTextLayoutStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:singleLine="true"
                    android:textCursorDrawable="@null"
                    android:paddingEnd="@dimen/margin_24"
                    android:ellipsize="end"
                    android:lines="1"
                    android:text=""
                    android:maxLines="1"
                    android:focusable="false"
                    android:fontFamily="@font/opensans_regular"
                    android:scrollHorizontally="true"
                    android:textSize="@dimen/margin_16"/>


            </com.google.android.material.textfield.TextInputLayout>

我尝试了下面的代码,但它不起作用:

 android:lines="1"
    android:scrollHorizontally="true"
    android:ellipsize="end"
    android:singleLine="true"
    android:editable="false"

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。