`

android中editText保留小数点后两位

 
阅读更多
一个很神奇,方便的保留小数点后两位的案例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; 
            }
        }); 
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics