package com.android.app_base.base.adapter.databinding;
|
|
import android.text.TextUtils;
|
import android.view.View;
|
import android.widget.EditText;
|
import android.widget.TextView;
|
|
import androidx.databinding.BindingAdapter;
|
import androidx.databinding.InverseBindingAdapter;
|
|
import java.math.BigDecimal;
|
import java.util.Locale;
|
|
/**
|
* @author Ljj
|
* @date 2023.05.22. 21:10
|
* @desc
|
*/
|
public class TextViewAdapter {
|
/**
|
* 设置TextView的Ellipsize
|
*
|
* @param textView TextView
|
* @param ellipsize 1:开头 2:中间 3:结尾 4:跑马灯 0或其他:不设置
|
*/
|
@BindingAdapter({"textOverflowMode"})
|
public static void setTextViewEllipsize(TextView textView, int ellipsize) {
|
TextUtils.TruncateAt where = null;
|
switch (ellipsize){
|
case 1:
|
where = TextUtils.TruncateAt.START;
|
break;
|
case 2:
|
where = TextUtils.TruncateAt.MIDDLE;
|
break;
|
case 3:
|
where = TextUtils.TruncateAt.END;
|
break;
|
case 4:
|
where = TextUtils.TruncateAt.MARQUEE;
|
break;
|
default:
|
break;
|
}
|
if (textView.getEllipsize() == where) {
|
return;
|
}
|
textView.setEllipsize(where);
|
if (where != TextUtils.TruncateAt.MARQUEE) {
|
return;
|
}
|
if (!textView.isSelected()) {
|
// 设置跑马灯之后需要设置选中才能有效果
|
textView.setSelected(true);
|
}
|
if (!textView.isFocusable()) {
|
// 设置跑马灯需要先获取焦点
|
textView.setFocusable(true);
|
}
|
textView.setSingleLine(true);
|
// 设置跑马灯的循环次数
|
textView.setMarqueeRepeatLimit(-1);
|
}
|
|
@BindingAdapter("android:text")
|
public static void setText(TextView view, double value) {
|
// String formattedValue = String.format(Locale.CHINA, "%.2f", value);
|
// if (!TextUtils.equals(view.getText().toString(), formattedValue)) {
|
// view.setText(formattedValue);
|
// }
|
view.setText(BigDecimal.valueOf(value).stripTrailingZeros().toPlainString());
|
}
|
|
@InverseBindingAdapter(attribute = "android:text")
|
public static double getText(TextView view) {
|
String text = view.getText().toString();
|
if (TextUtils.isEmpty(text)) {
|
return 0.0;
|
} else {
|
return Double.parseDouble(text);
|
}
|
}
|
|
}
|