我想自定义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"