"的EditText和TextView的"格式化千位分隔符(,)的机器人 [英] "EditText and TextView" formatted with thousands separators (,) in android
本文介绍了"的EditText和TextView的"格式化千位分隔符(,)的机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要千位分隔符(,)中的EditText当我键入EDITTEXT。 在那之后,我会做一些操作上的number.then显示结果在TextView中由千位分隔符(,)。
I want a thousands separator (,) in EditText when i am typing in editText. after that,i will do some operation on the number.then show the result in the TextView by thousands separator (,).
这是我的code:
public class Mainactivity extends Activity {
/** Called when the activity is first created. */
EditText edt;
TextView txt;
Button btn;
OnClickListener myclick = new OnClickListener() {
@Override
public void onClick(View arg0) {
// my calculation
double num1 = Double.parseDouble(edt.getText().toString());
double num2 = num1 + 7;
txt.setText("" + num2);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edt = (EditText) findViewById(R.id.edt);
txt = (TextView) findViewById(R.id.txt);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(myclick);
}
}
推荐答案
您可以使用
String value=String.format("%,.2f", num );
解析与千位分隔符的数量为字符串,然后将结果设置为你想要的的TextView或EditText上
to parse the number to String with a thousands separator and then set the result to the TextView or EditText that you want
例如:
txt.setText(String.format("%,.2f", num ));
edt.setText(String.format("%,.2f", num ));
这篇关于"的EditText和TextView的"格式化千位分隔符(,)的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文