android输入框输入价格,小数点后留两位简单实现

[html]  view plain copy
  1. 正好碰到这种情况,就记录下来吧,实现起来很简单  


xml文件里加个靠右限制就把光标移到右边去了,初始值设置成0.00

[html]  view plain copy
  1. <EditText  
  2.         android:id="@+id/id_edit"  
  3.         android:layout_width="fill_parent"  
  4.         android:layout_height="wrap_content"  
  5.         android:gravity="center_vertical|right"   
  6.         android:numeric="integer"  
  7.         android:text="0.00" />  

然后代码里实现

代码中要注意不要在字符未改变时对editText.setText,否则会报 StackOverflowError 

[java]  view plain copy
  1. public class MainActivity extends Activity {  
  2.     EditText edit;  
  3.       
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.activity_main);  
  8.           
  9.         edit = (EditText) findViewById(R.id.id_edit);    
  10.         edit.addTextChangedListener(new TextWatcher() {    
  11.             private boolean isChanged = false;    
  12.     
  13.             @Override    
  14.             public void onTextChanged(CharSequence s, int start, int before,    
  15.                     int count) {    
  16.                 // TODO Auto-generated method stub    
  17.             }    
  18.     
  19.             @Override    
  20.             public void beforeTextChanged(CharSequence s, int start, int count,    
  21.                     int after) {    
  22.                 // TODO Auto-generated method stub    
  23.             }    
  24.     
  25.             @Override    
  26.             public void afterTextChanged(Editable s) {    
  27.                 // TODO Auto-generated method stub    
  28.                 if (isChanged) {// ----->如果字符未改变则返回    
  29.                     return;    
  30.                 }    
  31.                 String str = s.toString();    
  32.     
  33.                 isChanged = true;    
  34.                 String cuttedStr = str;    
  35.                 /* 删除字符串中的dot */    
  36.                 for (int i = str.length() - 1; i >= 0; i--) {    
  37.                     char c = str.charAt(i);    
  38.                     if ('.' == c) {    
  39.                         cuttedStr = str.substring(0, i) + str.substring(i + 1);    
  40.                         break;    
  41.                     }    
  42.                 }    
  43.                 /* 删除前面多余的0 */    
  44.                 int NUM = cuttedStr.length();   
  45.                 int zeroIndex = -1;  
  46.                 for (int i = 0; i < NUM - 2; i++) {    
  47.                     char c = cuttedStr.charAt(i);    
  48.                     if (c != '0') {    
  49.                         zeroIndex = i;  
  50.                         break;  
  51.                     }else if(i == NUM - 3){  
  52.                         zeroIndex = i;  
  53.                         break;  
  54.                     }  
  55.                 }    
  56.                 if(zeroIndex != -1){  
  57.                     cuttedStr = cuttedStr.substring(zeroIndex);  
  58.                 }  
  59.                 /* 不足3位补0 */    
  60.                 if (cuttedStr.length() < 3) {    
  61.                     cuttedStr = "0" + cuttedStr;    
  62.                 }    
  63.                 /* 加上dot,以显示小数点后两位 */    
  64.                 cuttedStr = cuttedStr.substring(0, cuttedStr.length() - 2)    
  65.                         + "." + cuttedStr.substring(cuttedStr.length() - 2);    
  66.                 
  67.                 edit.setText(cuttedStr);    
  68.     
  69.                 edit.setSelection(edit.length());    
  70.                 isChanged = false;    
  71.             }    
  72.         });    
  73.     }  
  74.   
  75.     @Override  
  76.     public boolean onCreateOptionsMenu(Menu menu) {  
  77.         // Inflate the menu; this adds items to the action bar if it is present.  
  78.         getMenuInflater().inflate(R.menu.main, menu);  
  79.         return true;  
  80.     }  
  81.   
  82.       
  83. }  
转载于:http://blog.csdn.net/zoeice/article/details/7900466







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值