我正在申请计算PMT功能,但我不得不计算第一笔付款。简短说明: 我的应用程序将有3个edittexts,1个用于价格,1个用于百分比,最后一个用于它们的结果,例如我们输入价格100.000然后第一笔付款30 然后第3个将自动显示结果,在这种情况下为30.000
Edittext1:
100000
Edittext2:
30
Edittext3:
30000(this one automatically calculate when we done give an input in the Edittext2)
这是代码:
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
editText2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String text1 = editText1.getText().toString();
if (text1.length() != 0) {
int input1 = Integer.valueOf(text1);
int input2 = Integer.valueOf(s.toString());
int output = (input1 * input2) / 100;
editText3.setText(output + "");
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
数字到十进制转换器
public void makeDecimal(View v)
{
ethint1.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
DigitsKeyListener dkl = new DigitsKeyListener(true,true);
ethint1.setKeyListener(dkl);
ethint1.addTextChangedListener(new TextWatcher(){
private String current = "";
@Override
public void afterTextChanged(Editable s) {
String userInput=s.toString();
if(!userInput.toString().equals(current)){
ethint1.removeTextChangedListener(this);
String cleanString = userInput.replaceAll("[,]", "");
if(cleanString.length()>0){
double parsed = Double.parseDouble(cleanString);
String formated = DecimalFormat.getNumberInstance().format(parsed);
current = formated;
ethint1.setText(formated);
ethint1.setSelection(formated.length());
}else{
ethint1.setText(cleanString);
ethint1.setSelection(cleanString.length());
}
ethint1.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) {
}
});
}
ethint1 = edittext1我改变了一些变量
logcat :::::::::::::::::::::::::::::02-16 14:43:12.331: E/AndroidRuntime(609): FATAL EXCEPTION: main
02-16 14:43:12.331: E/AndroidRuntime(609): java.lang.NumberFormatException: unable to parse '1,000,000' as integer
02-16 14:43:12.331: E/AndroidRuntime(609): at java.lang.Integer.parse(Integer.java:433)
02-16 14:43:12.331: E/AndroidRuntime(609): at java.lang.Integer.parseInt(Integer.java:422)
02-16 14:43:12.331: E/AndroidRuntime(609): at java.lang.Integer.parseInt(Integer.java:382)
02-16 14:43:12.331: E/AndroidRuntime(609): at java.lang.Integer.valueOf(Integer.java:682)
02-16 14:43:12.331: E/AndroidRuntime(609): at com.m4nd1r1tun45f1n4nc3.abcdefg.Mastercode$1.onTextChanged(Mastercode.java:43)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.widget.TextView.handleTextChanged(TextView.java:6172)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6316)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:583)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:174)
02-16 14:43:12.331: E/AndroidRuntime(609): at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:120)
02-16 14:43:12.331: E/AndroidRuntime(609): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:247)
02-16 14:43:12.331: E/AndroidRuntime(609): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:73)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.os.Looper.loop(Looper.java:123)
02-16 14:43:12.331: E/AndroidRuntime(609): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-16 14:43:12.331: E/AndroidRuntime(609): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 14:43:12.331: E/AndroidRuntime(609): at java.lang.reflect.Method.invoke(Method.java:521)
02-16 14:43:12.331: E/AndroidRuntime(609): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-16 14:43:12.331: E/AndroidRuntime(609): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-16 14:43:12.331: E/AndroidRuntime(609): at dalvik.system.NativeStart.main(Native Method)
问题更新:: 是否有办法使两个工作(数字转换器和第三个edittext输出) 。注意:对于数字转换器,我实际上想让它显示为货币 当我们将数字输入到edittext1(ethint1)时,它会自动添加一个分隔符。例如:我们输入100只会是100(没有改变)然后当我们再添加一个数字时它将是1,000(自动),依此类推,直到10,000,000,000
/ 错误日志/问题没有错误,但是当我们输入百分比时会崩溃 /
提前感谢,
答案 0 :(得分:2)
更改行
editText3.setText(output + "");
到
editText3.setText("" + output);
<强>说明:强>
output
是整数值,而.setText()
是String参数。在任何整数之前添加""
(空字符串)会将其转换为(让Java相信它是)String。
答案 1 :(得分:0)
更改
editText3.setText(output + "");
到
editText3.setText(String.valueOf(value));