以下代码用于在用户输入值编辑文本时将.(dot)
替换为,(coma)
InputFilter [] FilterArray = new InputFilter [1]; FilterArray [0] = new InputFilter(){
@Override
public CharSequence filter(CharSequence arg0, int arg1, int arg2,
Spanned arg3, int arg4, int arg5) {
String temp =arg0.toString();
temp=temp.replace('.',',');
return temp;
}
};
etxt_kg.setFilters(FilterArray);
上面的代码成功地解释了用户的每个输入。但它没有取代任何东西。(点)
现在:输入:12.45输出:1245
我想要的是:输入12.45输出:12,45
任何帮助(TIA)
编辑:
问题出现在xml .. EditText inputType =“number”。
答案 0 :(得分:3)
我遇到了同样的情况。这是一个解决方案
使用以下代码段
InputFilter filter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (source.charAt(i) == '.') {
return ",";
}
}
return null;
}
};
EditText editText = (EditText) findViewById(R.id.rg);
editText.setFilters(new InputFilter[] {filter});
以下是我的工作代码
package org.vipul;
import android.app.Activity;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.widget.EditText;
public class AbcActivity extends Activity {
EditText text;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText etxt_kg = (EditText) findViewById(R.id.rg);
etxt_kg.setMaxHeight(etxt_kg.getHeight());
InputFilter filter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (source.charAt(i) == '.') {
return ",";
}
}
return null;
}
};
// EditText editText = (EditText) findViewById(R.id.rg);
etxt_kg.setFilters(new InputFilter[] {
filter
});
}
}