Android 自定义 TextView 显示货币

2024-04-04

我需要为文本视图设置一个掩码,以将其文本转换为特定样式(货币,我自己的样式,如下所示:Rls ###,###,###,###),但我需要能够稍后获取它的原始文本。我该怎么办?

=================================================== ====

假设我将把这个字符串传递给 TextView:2120000

用户应该看到这样的内容:Rls 2,120,000

但 .getText 方法应该返回:2120000

=================================================== ====

格式化代码将是这样的:

DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
DecimalFormat decimalFormat = new DecimalFormat("Rls ###,###,###,###", symbols);
String prezzo = decimalFormat.format(Integer.parseInt(textTemp));

任何帮助,将不胜感激


正确的方法是这样的: 创建自定义 TextView 作为 Java 类

public class RialTextView extends TextView {

    String rawText;

    public RialTextView(Context context) {
        super(context);

    }

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

    public RialTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        rawText = text.toString();
        String prezzo = text.toString();
        try {

            DecimalFormatSymbols symbols = new DecimalFormatSymbols();
            symbols.setDecimalSeparator(',');
            DecimalFormat decimalFormat = new DecimalFormat("###,###,###,###", symbols);
            prezzo = decimalFormat.format(Integer.parseInt(text.toString()));
        }catch (Exception e){}

        super.setText(prezzo, type);
    }

    @Override
    public CharSequence getText() {

        return rawText;
    }
}

并在 XML 中而不是<TextView标签 使用这样的东西:

<com...RialTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Price"
        android:id="@+id/tv_itemgriditems_Price"
        android:layout_below="@+id/tv_itemgriditems_Remain"
        android:layout_centerHorizontal="true"
        android:textSize="15sp"
        />
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 自定义 TextView 显示货币 的相关文章

随机推荐