我有带有android:numeric =“decimal”的EditText字段和用于输入十进制数字的android:inputType =“phone”。我使用输入型手机,因为用户输入数字更容易。
对于任何带有软键盘的设备,我没有任何问题,但是当我开始使用硬键盘时会打印错误的数字(例如当我使用HTC Desire Z时)。
如何解决这个问题?
P.S。我为Android开发了“Simple Loan Calculator” - 它是开源和免费软件
答案 0 :(得分:0)
解决!
<强>的AndroidManifest.xml 强>
<activity android:name=".MainActivity" android:label="@string/app_name" android:configChanges="keyboardHidden|orientation|keyboard" >
<强> MainActivity.java 强>
public void onCreate(Bundle savedInstanceState) {
....
setPriceInputFilter(amountEdit, interestEdit, fixedPaymentEdit, periodYearEdit, periodMonthEdit, downPaymentEdit, disposableCommissionEdit, monthlyCommissionEdit);
....
}
private void setPriceInputFilter(EditText ... fields){
PriceInputFilter filter = new PriceInputFilter();
for (EditText field: fields){
field.setFilters(new InputFilter[]{filter});
}
}
public void onConfigurationChanged(Configuration newConfig) {
if(newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO){
setInputType(InputType.TYPE_NULL, amountEdit, interestEdit, fixedPaymentEdit, periodYearEdit,
periodMonthEdit, downPaymentEdit, disposableCommissionEdit, monthlyCommissionEdit);
Toast.makeText(this, "HARD-keyboard", Toast.LENGTH_SHORT).show();
}else{
setInputType(InputType.TYPE_CLASS_PHONE, amountEdit, interestEdit, fixedPaymentEdit, periodYearEdit,
periodMonthEdit, downPaymentEdit, disposableCommissionEdit, monthlyCommissionEdit);
Toast.makeText(this, "SOFT-keyboard", Toast.LENGTH_SHORT).show();
}
super.onConfigurationChanged(newConfig);
}
private void setInputType(int type , EditText ... fields){
for (EditText field: fields){
field.setInputType(type);
}
}
<强> PriceInputFilter.java 强>
public class PriceInputFilter implements InputFilter {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
String checkedText = dest.toString() + source.toString();
String pattern = getPattern();
if (!Pattern.matches(pattern, checkedText)) {
return "";
}
return null;
}
private String getPattern() {
return "[0-9]+([.]{1}||[.]{1}[0-9]{1,2})?";
}
}