Android Custom TextView to show Currency

2019-05-10 15:16发布

I need to set a mask for a textview to convert its text to a specific style (Currency, my own though Like this: Rls ###,###,###,### ) but I need to be able to get it's raw text later on. What shall I do?

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

say I will pass this string to the TextView: 2120000

It should be seen by user like : Rls 2,120,000

but the .getText method should return: 2120000

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

the formatting code would be like this:

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

any help would be appreciated

5条回答
趁早两清
2楼-- · 2019-05-10 15:56

Let's suppose you have a class MyParser:

public class MyParser {

    //...
    public static String getRawValue(value) {
        //Converts value to raw value, for instance, converts Rls 2,120,000 to 2120000
    }

    public static String getViewValue(value) {
        //Converts value to view value, for instance, converts 2120000 to Rls 2,120,000
    }
    //...
}

Now, let's have another class, called MyTextView:

public class MyTextView extends TextView {

    public CharSequence getText () {
        return MyParser.getRawValue(super.getText());
    }

    public void setText (CharSequence text, TextView.BufferType type) {
        super.setText(MyParser.getViewValue(text), type)
    }

}
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-05-10 15:56

for convert long to rial format

    public String FormatCost(long cost){
    try {
        DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        symbols.setDecimalSeparator(',');
        DecimalFormat decimalFormat = new DecimalFormat("###,###,###,###", symbols);
        return decimalFormat.format(Integer.parseInt(cost+""));
    }catch (Exception e) {
        return cost + "";
    }
}
查看更多
贪生不怕死
4楼-- · 2019-05-10 16:06

You can use the same DecimalFormat object that you used to format the string to parse it back the other way:

DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
DecimalFormat decimalFormat = new DecimalFormat("Rls ###,###,###,###", symbols);

try {
    // get int value
    int i = decimalFormat.parse(textView.getText().toString()).intValue();
} catch (ParseException e) {
    // handle error here
}
查看更多
看我几分像从前
5楼-- · 2019-05-10 16:12

You have to modify your edittext in each time you enter an input character. Try this code snippet

EditText currencyEditText= (EditText)findViewById(R.id.medittext);
searchTo.addTextChangedListener(new TextWatcher() {

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

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

});
查看更多
家丑人穷心不美
6楼-- · 2019-05-10 16:20

Correct way would be this: To create a custom TextView as a Java Class

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;
    }
}

and in the XML instead of <TextView tag using something like this:

<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"
        />
查看更多
登录 后发表回答