一个很神奇,方便的保留小数点后两位的案例xml文件中的内容:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
tools:context="com.shzz.ssss.MainActivity"
tools:ignore="MergeRootFrame" >

<EditText
android:id="@+id/editText1"
android:layout_width="316dp"
android:layout_height="wrap_content"
android:digits="1234567890."
android:inputType="number"
android:gravity="center_vertical|right"
android:numeric="integer"
android:text="0.00"
android:ems="10" >

<requestFocus />
</EditText>
</FrameLayout>
[/color]
activity中的代码:
[size=x-small]package com.shzz.ssss;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.EditText;

public class MainActivity extends Activity {
EditText edit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

edit = (EditText) findViewById(R.id.editText1);
edit.addTextChangedListener(new TextWatcher() {
private boolean isChanged = false;

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// 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 afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if (isChanged) {// ----->如果字符未改变则返回
return;
}
String str = s.toString();

isChanged = true;
String cuttedStr = str;
/* 删除字符串中的dot */
for (int i = str.length() - 1; i >= 0; i--) {
char c = str.charAt(i);
if ('.' == c) {
cuttedStr = str.substring(0, i) + str.substring(i + 1);
break;
}
}
/* 删除前面多余的0 */
int NUM = cuttedStr.length();
int zeroIndex = -1;
for (int i = 0; i < NUM - 2; i++) {
char c = cuttedStr.charAt(i);
if (c != '0') {
zeroIndex = i;
break;
}else if(i == NUM - 3){
zeroIndex = i;
break;
}
}
if(zeroIndex != -1){
cuttedStr = cuttedStr.substring(zeroIndex);
}
/* 不足3位补0 */
if (cuttedStr.length() < 3) {
cuttedStr = "0" + cuttedStr;
}
/* 加上dot,以显示小数点后两位 */
cuttedStr = cuttedStr.substring(0, cuttedStr.length() - 2)
+ "." + cuttedStr.substring(cuttedStr.length() - 2);

edit.setText(cuttedStr);

edit.setSelection(edit.length());
isChanged = false;
}
});
}
}

更多相关文章

  1. 分支和循环(二)(零基础学习C语言)
  2. cocos2dx 在android手机输入特殊字符导致程序崩溃
  3. Android——检测TXT文件中是否含有双字节字符
  4. JNI WARNING: input is not valid Modified UTF-8: illegal star
  5. Android(安卓)Tips1
  6. 混淆打包的Proguard returned with error code 1. See console错
  7. 我的Android进阶之旅------>启动Activity的标准Action和标准Cate
  8. android webview js 交互
  9. Android获取本应用的MD5值

随机推荐

  1. Android实现简易计算器(页面跳转和数据传
  2. android(6)(读数据的一些权限)
  3. Android的计量单位px,in,mm,pt,dp,dip,sp
  4. Android学习整理- 8 -MediaPlayer 放歌
  5. android监控程序状态(安装 卸载)
  6. Android中使用tcpdump、wireshark进行抓
  7. Android隐藏软件盘
  8. android 手电筒
  9. Android 消息通知-Notification
  10. android弹钢琴的一个简单程序