在进程的编辑文本中只获取数字而不是逗号

Get only number not comma in edittext on process

我有这段代码可以从我的 EditText 输入中检测数字格式。

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;
        }
    }

}

然后在activity中调用。

JumlahTrx = (EditText)findViewById(R.id.editJumlah);
JumlahTrx.addTextChangedListener(new NumberTextWatcher(JumlahTrx));

如果我输入该字段,它将产生 5,000。我想要的是当我单击按钮时,值 5,000 变为 5000,然后插入到我的数据库中。因为我在我的数据库中设置了 INT。

任何帮助都会很好。谢谢

====================== 更新

添加此代码

String data = JumlahTrx.getText().toString();
data=data.replaceAll(",","");
int value=Integer.parseInt(data);
JumlahTrx.setText(value);

单击按钮将其保存到数据库时出现此错误

11-26 20:27:54.441 2609-2609/com.androidjson.serverupdate_androidjsoncom E/AndroidRuntime: FATAL EXCEPTION: main Process: com.androidjson.serverupdate_androidjsoncom, PID: 2609 android.content.res.Resources$NotFoundException: String resource ID #0x61a8 at android.content.res.Resources.getText(Resources.java:335) at android.widget.TextView.setText(TextView.java:4555) at com.androidjson.serverupdate_androidjsoncom.MainActivity.StudentRegistration(MainActivity.java:120) at com.androidjson.serverupdate_androidjsoncom.MainActivity.onClick(MainActivity.java:53) at android.view.View.performClick(View.java:5657) at android.view.View$PerformClick.run(View.java:22453) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6236) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)

==============更新 2=================== =

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    JenisTrx = (EditText)findViewById(R.id.editJenis);
    KeteranganTrx = (EditText)findViewById(R.id.editKeterangan);
    JumlahTrx = (EditText)findViewById(R.id.editJumlah);
    JumlahTrx.addTextChangedListener(new NumberTextWatcher(JumlahTrx));


    RegisterStudent = (Button)findViewById(R.id.buttonSubmit);
    ShowStudents = (Button)findViewById(R.id.buttonShow);

    RegisterStudent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            // Checking whether EditText is Empty or Not
            CheckEditTextIsEmptyOrNot();

            if(CheckEditText){

                // If EditText is not empty and CheckEditText = True then this block will execute.

                StudentRegistration(JenisTrxHolder,KeteranganTrxHolder, JumlahTrxHolder);

            }
            else {

                // If EditText is empty then this block will execute .
                Toast.makeText(MainActivity.this, "Please fill all form fields.", Toast.LENGTH_LONG).show();

            }

        }
    });

    ShowStudents.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            Intent intent = new Intent(getApplicationContext(),ShowAllStudentsActivity.class);

            startActivity(intent);

        }
    });
 }

public void StudentRegistration(final String S_Jenis, final String S_Keterangan, final String S_Jumlah){

    class StudentRegistrationClass extends AsyncTask<String,Void,String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            progressDialog = ProgressDialog.show(MainActivity.this,"Loading Data",null,true,true);
        }

        @Override
        protected void onPostExecute(String httpResponseMsg) {

            super.onPostExecute(httpResponseMsg);
            String a = JumlahTrx.getText().toString();
            a=a.replaceAll(",","");
            JumlahTrx.setText(a);
            System.out.println( a );
            progressDialog.dismiss();

            Toast.makeText(MainActivity.this,httpResponseMsg.toString(), Toast.LENGTH_LONG).show();

        }

        @Override
        protected String doInBackground(String... params) {

            hashMap.put("JenisTrx",params[0]);

            hashMap.put("KeteranganTrx",params[1]);

            hashMap.put("JumlahTrx",params[2]);

            finalResult = httpParse.postRequest(hashMap, HttpURL);

            return finalResult;
        }
    }

    StudentRegistrationClass studentRegistrationClass = new StudentRegistrationClass();

    studentRegistrationClass.execute(S_Jenis,S_Keterangan,S_Jumlah);
}

public void CheckEditTextIsEmptyOrNot(){

    JenisTrxHolder = JenisTrx.getText().toString();
    KeteranganTrxHolder = KeteranganTrx.getText().toString();
    JumlahTrxHolder = JumlahTrx.getText().toString();

    if(TextUtils.isEmpty(JenisTrxHolder) || TextUtils.isEmpty(KeteranganTrxHolder) || TextUtils.isEmpty(JumlahTrxHolder))
    {

        CheckEditText = false;

    }
    else {

        CheckEditText = true ;
    }

}

在将数据保存到数据库之前,您可以使用 replacereplaceAll 函数 例如

String data = "5,000";
data= data.replaceAll(",","");

Int value = Integer.parseInt(data);

将该值存储到您的数据库

已更新

在你的 doInBackGround

改变

hashMap.put("JumlahTrx",params[2]);

 String a = params[2];
 a=a.replaceAll(",","");
 hashMap.put("JumlahTrx",a);

正如你所说我想要的是当我点击按钮时值 5,000 变成 5000。如果这只是要求,那么我有一个替代解决方案。 试试这个

String a="5,00,000";

a=a.replaceAll(",","");

int num=Integer.parseInt(a);