编辑文本以获取货币格式(###,###,###),但在点击“减少”按钮时崩溃。

huangapple go评论146阅读模式
英文:

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).
and
NumberFormatException** 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

code imag

答案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(",", ""));

huangapple
  • 本文由 发表于 2020年7月29日 16:53:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63149876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定