英文:
2 Edit text get currency format(###,###,###) and mines but (crash when clicked on btn mines)
问题
这是Java代码:
package com.aliabbasi.mytamrin;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DecimalFormat;
public class full extends AppCompatActivity {
EditText full_payment, cash_payment, remain_payment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full);
Toast.makeText(this, "مقادیر را با دقت وارد کنید", Toast.LENGTH_LONG).show();
Button btn_cancel = findViewById(R.id.btn_cancel);
btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent btn_cancel = new Intent(full.this, MainActivity.class);
startActivities(new Intent[]{btn_cancel});
}
});
try {
Button remain_check = findViewById(R.id.remain_btn);
remain_check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
full_payment = findViewById(R.id.edt_full_payment);
cash_payment = findViewById(R.id.edt_cash_payment);
remain_payment = (EditText) findViewById(R.id.edt_remain);
Float a = Float.parseFloat(full_payment.getText().toString());
Float b = Float.parseFloat(cash_payment.getText().toString());
Float s = a - b;
String r = Double.toString(s);
remain_payment.setText(r);
}
});
} catch (Exception e) {
Toast.makeText(this, "خطایی رح داده" + e, Toast.LENGTH_LONG).show();
}
try {
EditText editText = (EditText) findViewById(R.id.edt_full_payment);
editText.addTextChangedListener(new MyNumberWatcher(editText));
} catch (Exception e) {
Toast.makeText(this, "ERR" + e, Toast.LENGTH_LONG).show();
}
try {
EditText editText = (EditText) findViewById(R.id.edt_cash_payment);
editText.addTextChangedListener(new MyNumberWatcher(editText));
} catch (Exception e) {
Toast.makeText(this, "ERR" + e, Toast.LENGTH_LONG).show();
}
/*try {
EditText editText= (EditText) findViewById(R.id.edt_remain);
editText.addTextChangedListener(new MyNumberWatcher(editText));
}
catch (Exception e){
Toast.makeText(this, "ERR"+e, Toast.LENGTH_LONG).show();
}*/
}
}
我尝试使用double变量,没有错误,但当我按下remain_btn时,程序崩溃。我检查了调试信息并发现了问题:
at com.aliabbasi.mytamrin.full$2.onClick(full.java:45).
并且出现了NumberFormatException,但我不知道如何解决它。我真的想知道如何解决这个问题,因为我已经陷在这里两天了。谢谢阅读,希望一切顺利。
英文:
here is the java code :
package com.aliabbasi.mytamrin;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DecimalFormat;
public class full extends AppCompatActivity {
EditText full_payment, cash_payment,remain_payment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full);
Toast.makeText(this, "مقادیر را با دقت وارد کنید", Toast.LENGTH_LONG).show();
Button btn_cancel = findViewById(R.id.btn_cancel);
btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent btn_cancel = new Intent(full.this, MainActivity.class);
startActivities(new Intent[]{btn_cancel});
}
});
try {
Button remain_check = findViewById(R.id.remain_btn);
remain_check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
full_payment = findViewById(R.id.edt_full_payment);
cash_payment = findViewById(R.id.edt_cash_payment);
remain_payment = (EditText) findViewById(R.id.edt_remain);
Float a = Float.parseFloat(full_payment.getText().toString());
Float b = Float.parseFloat(cash_payment.getText().toString());
Float s = a - b;
String r = Double.toString(s);
remain_payment.setText(r);
}
});
} catch (Exception e) {
Toast.makeText(this, "خطایی رح داده" + e, Toast.LENGTH_LONG).show();
}
try {
EditText editText= (EditText) findViewById(R.id.edt_full_payment);
editText.addTextChangedListener(new MyNumberWatcher(editText));
}
catch (Exception e){
Toast.makeText(this, "ERR"+e, Toast.LENGTH_LONG).show();
}
try {
EditText editText= (EditText) findViewById(R.id.edt_cash_payment);
editText.addTextChangedListener(new MyNumberWatcher(editText));
}
catch (Exception e){
Toast.makeText(this, "ERR"+e, Toast.LENGTH_LONG).show();
}
/*try {
EditText editText= (EditText) findViewById(R.id.edt_remain);
editText.addTextChangedListener(new MyNumberWatcher(editText));
}
catch (Exception e){
Toast.makeText(this, "ERR"+e, Toast.LENGTH_LONG).show();
}*/
}
}
i try the double variable
there is no Error but program crashed when i press the remain_btn.
i check the Debug and find out there is a problem with this:
at com.aliabbasi.mytamrin.full$2.onClick(full.java:45).
andNumberFormatException** but IDK how to solve it.**
and i really like to know how fix this becase i stuck in this about 2 days...
thanks for reading ...
wish the best
答案1
得分: 0
你可以这样做。去掉那个逗号。
Float a = Float.parseFloat(full_payment.getText().toString().replace(",", ""));
英文:
You can do something like this. Remove that comma.
Float a = Float.parseFloat(full_payment.getText().toString().replace(",", ""));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论