Java 为edittext设置特定的区域设置

Java 为edittext设置特定的区域设置,java,android,eclipse,Java,Android,Eclipse,我正在使用TextWatcher在输入EditText时编辑值 这是我的TextWatcher public class NumberTextWatcher implements TextWatcher { private DecimalFormat df; private DecimalFormat dfnd; private boolean hasFractionalPart; private EditText et; public NumberTextWatcher(EditText

我正在使用TextWatcher在输入EditText时编辑值 这是我的TextWatcher

public class NumberTextWatcher implements TextWatcher {

private DecimalFormat df;
private DecimalFormat dfnd;
private boolean hasFractionalPart;

private EditText et;

public NumberTextWatcher(EditText et)
{
    df = new DecimalFormat("#,###");

    df.setDecimalSeparatorAlwaysShown(true);
    dfnd = new DecimalFormat("#,###");
    this.et = et;
    hasFractionalPart = false;
}

@SuppressWarnings("unused")
private static final String TAG = "NumberTextWatcher";

@Override
public void afterTextChanged(Editable s)
{
    et.removeTextChangedListener(this);

    try {
        int inilen, endlen;
        inilen = et.getText().length();

        String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "");
        Number n = df.parse(v);
        int cp = et.getSelectionStart();
        if (hasFractionalPart) {
            et.setText(df.format(n));
        } else {
            et.setText(dfnd.format(n));
        }
        endlen = et.getText().length();
        int sel = (cp + (endlen - inilen));
        if (sel > 0 && sel <= et.getText().length()) {
            et.setSelection(sel);
        } else {
            // place cursor at the end?
            et.setSelection(et.getText().length() - 1);
        }
    } catch (NumberFormatException nfe) {
        // do nothing?
    } catch (ParseException e) {
        // do nothing?
    }

    et.addTextChangedListener(this);
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
    if (s.toString().contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator())))
    {
        hasFractionalPart = true;
    } else {
        hasFractionalPart = false;
    }
}
但我的问题是,当设备默认语言不是英语时,它可以将字符串解析为双倍,所以我想我为EditText设置了Locale!这可能吗?如果没有,我应该怎么做才能将值解析为双精度

DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);

当您可以控制后端时,我发现最容易处理您期望的语言,并且已经对其进行了广泛的测试。因此,在处理我内部拥有的文件上的文件IO时,在执行任何文件创建代码之前,我总是调用您在注释末尾的调用。在我的情况下是这样的

final DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getInstance(Locale.ENGLISH); //Format our data to two decimal places for brightness change.
String stringFormat = "#0.00";
decimalFormat.applyPattern(stringFormat);
decimalFormat.format(dataString);
然后,无论设备实际设置为哪种语言,您总是在处理您习惯的语言环境。这将有助于处理其他可能使用不同数字格式的语言,例如在本例中,因为我在处理数字。由于您处理的是双精度运算,因此您可能正在处理这个数字转换问题。但是,如果您正在处理来自EditText的输入,那么仅在我的后端使用的这种特殊方法可能不适用。但我认为沟通我的方法可能还是有帮助的;希望无论如何。分享没有坏处

final DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getInstance(Locale.ENGLISH); //Format our data to two decimal places for brightness change.
String stringFormat = "#0.00";
decimalFormat.applyPattern(stringFormat);
decimalFormat.format(dataString);