android TextView输入到多个TextViews的货币

时间:2015-02-25 07:46:21

标签: android

我有一个函数可以将输入转换为货币格式example: 1000 to 1,000我使用此函数来处理我的edittext价格输入,当我们在edittext中输入数字时它会自动转换。现在我正在制作另一个有几十个textview的活动,它的表格会显示一些价格。现在我使用相同的函数来处理每个textview(一个函数一个textview),而我有超过1个textview。我处理这些文本视图的代码太长了。我想知道我是否可以有一个更好的功能,可以转换多个textviews的输入

数字转换器功能:

public void makeDecimal()
                {
                                txtBox.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

                                DigitsKeyListener dkl = new DigitsKeyListener(true,true);
                                txtBox.setKeyListener(dkl);

                                txtBox.addTextChangedListener(new TextWatcher(){
                        private String current = "";

                        @Override
                        public void afterTextChanged(Editable s) {
                                String userInput=s.toString();

                                if(!userInput.toString().equals(current)){
                                                txtBox.removeTextChangedListener(this);

                                                String cleanString = userInput.replaceAll("[,]", "");

                                                if(cleanString.length()>0){
                                                                double parsed = Double.parseDouble(cleanString);
                                                                String formated = DecimalFormat.getNumberInstance(Locale.ENGLISH).format(parsed);

                                                                current = formated;
                                                                txtBox.setText(formated);
                                                                txtBox.setSelection(formated.length());
                                                }else{
                                                                txtBox.setText(cleanString);
                                                                txtBox.setSelection(cleanString.length());
                                                }

                                                txtBox.addTextChangedListener(this);
                    }
                        }
                        @Override
                        public void beforeTextChanged(CharSequence s, int start,
                                int count, int after) {
                        }
                        @Override
                        public void onTextChanged(CharSequence s, int start,
                                int before, int count) {
                        }
                    });


                }

我的活动有很多短信:

public class TableOutput extends Activity implements TextWatcher{
    TextView try10,try1,try2,try3,try4,try5,try6,try7,try8,try9;
    String a,a1,a2,a3,a4,a5,a6,a7,a8,a9;
    TextView trying,trying1,trying2,trying3,trying4,trying5,trying6,trying7,trying8,trying9;
    String b,b1,b2,b3,b4,b5,b6,b7,b8,b9;
    @Override
    protected void onCreate(Bundle bundle) {
        // TODO Auto-generated method stub
        super.onCreate(bundle);
        setContentView(R.layout.tableoutputsimulasikredit);
        try10 = (TextView) findViewById(R.id.tumbal1);
        try1 = (TextView) findViewById(R.id.tumbal2);
        try2= (TextView) findViewById(R.id.tumbal3);
        try3 = (TextView) findViewById(R.id.tumbal4);
        try4 = (TextView) findViewById(R.id.tumbal5);
        try5 = (TextView) findViewById(R.id.tumbal6);
        try6 = (TextView) findViewById(R.id.tumbal7);
        try7 = (TextView) findViewById(R.id.tumbal8);
        try8 = (TextView) findViewById(R.id.tumbal9);
        try9 = (TextView) findViewById(R.id.tumbal10);

        trying = (TextView) findViewById(R.id.tumbalaa1);
        trying1 = (TextView) findViewById(R.id.tumbalaa2);
        trying2 = (TextView) findViewById(R.id.tumbalaa3);
        trying3 = (TextView) findViewById(R.id.tumbalaa4);
        trying4 = (TextView) findViewById(R.id.tumbalaa5);
        trying5 = (TextView) findViewById(R.id.tumbalaa6);
        trying6 = (TextView) findViewById(R.id.tumbalaa7);
        trying7 = (TextView) findViewById(R.id.tumbalaa8);
        trying8 = (TextView) findViewById(R.id.tumbalaa9);
        trying9 = (TextView) findViewById(R.id.tumbalaa10);

         bundle = getIntent().getExtras();
        long tumbalnya1 = bundle.getLong("tumbal1");
        long tumbalnya2 = bundle.getLong("tumbal2");
        long tumbalnya3 = bundle.getLong("tumbal3");
        long tumbalnya4 = bundle.getLong("tumbal4");
        long tumbalnya5 = bundle.getLong("tumbal5");
        long tumbalnya6 = bundle.getLong("tumbal6");
        long tumbalnya7 = bundle.getLong("tumbal7");
        long tumbalnya8 = bundle.getLong("tumbal8");
        long tumbalnya9 = bundle.getLong("tumbal9");
        long tumbalnya10 = bundle.getLong("tumbal10");

        long tumbalaanya1 = bundle.getLong("tumbalaa1");
        long tumbalaanya2 = bundle.getLong("tumbalaa2");
        long tumbalaanya3 = bundle.getLong("tumbalaa3");
        long tumbalaanya4 = bundle.getLong("tumbalaa4");
        long tumbalaanya5 = bundle.getLong("tumbalaa5");
        long tumbalaanya6 = bundle.getLong("tumbalaa6");
        long tumbalaanya7 = bundle.getLong("tumbalaa7");
        long tumbalaanya8 = bundle.getLong("tumbalaa8");
        long tumbalaanya9 = bundle.getLong("tumbalaa9");
        long tumbalaanya10 = bundle.getLong("tumbalaa10");

        a = Long.toString(tumbalnya1);
        a1 = Long.toString(tumbalnya2);
        a2 = Long.toString(tumbalnya3);
        a3 = Long.toString(tumbalnya4);
        a4 = Long.toString(tumbalnya5);
        a5 = Long.toString(tumbalnya6);
        a6 = Long.toString(tumbalnya7);
        a7 = Long.toString(tumbalnya8);
        a8 = Long.toString(tumbalnya9);
        a9 = Long.toString(tumbalnya10);

        b = Long.toString(tumbalaanya1);
        b1 = Long.toString(tumbalaanya2);
        b2 = Long.toString(tumbalaanya3);
        b3 = Long.toString(tumbalaanya4);
        b4 = Long.toString(tumbalaanya5);
        b5 = Long.toString(tumbalaanya6);
        b6 = Long.toString(tumbalaanya7);
        b7 = Long.toString(tumbalaanya8);
        b8 = Long.toString(tumbalaanya9);
        b9 = Long.toString(tumbalaanya10);

        try10.setText(a +" IDR");
        try1.setText(a1 +" IDR");
        try2.setText(a2 +" IDR");
        try3.setText(a3 +" IDR");
        try4.setText(a4 +" IDR");
        try5.setText(a5 +" IDR");
        try6.setText(a6 +" IDR");
        try7.setText(a7 +" IDR");
        try8.setText(a8 +" IDR");
        try9.setText(a9 +" IDR");

        trying.setText(b + " IDR");
        trying1.setText(b1 + " IDR");
        trying2.setText(b2 + " IDR");
        trying3.setText(b3 + " IDR");
        trying4.setText(b4 + " IDR");
        trying5.setText(b5 + " IDR");
        trying6.setText(b6 + " IDR");
        trying7.setText(b7 + " IDR");
        trying8.setText(b8 + " IDR");
        trying9.setText(b9 + " IDR");
    }                                    
@Override
public void afterTextChanged(Editable arg0) {
    // 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) {
    // TODO Auto-generated method stub

}

}

1 个答案:

答案 0 :(得分:1)

您可以使用InputFilter界面。只需传递regex货币格式

即可
public class CurrencyFormaterInputFilter implements InputFilter {

Pattern mPattern = Pattern.compile("(0|[1-9]+[0-9]*)?(\\.[0-9]{0,2})?");

@Override
public CharSequence filter(
        CharSequence source,
        int start,
        int end,
        Spanned dest,
        int dstart,
        int dend) {

    String result = 
            dest.subSequence(0, dstart)
            + source.toString() 
            + dest.subSequence(dend, dest.length());

    Matcher matcher = mPattern.matcher(result);

    if (!matcher.matches()) {
       return dest.subSequence(dstart, dend);
       }

    return null;
}
}

使用方法:

editText.setFilters(new InputFilter[] {new CurrencyFormaterInputFilter()}); 

您可以根据需要更改正则表达式