我有这段代码来检测我的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;
}
}
}
然后在活动中调用它。
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 过程:com.androidjson.serverupdate_androidjsoncom,PID:2609 android.content.res.Resources $ NotFoundException:字符串资源ID#0x61a8 在android.content.res.Resources.getText(Resources.java:335) 在android.widget.TextView.setText(TextView.java:4555) 在com.androidjson.serverupdate_androidjsoncom.MainActivity.StudentRegistration(MainActivity.java:120) 在com.androidjson.serverupdate_androidjsoncom.MainActivity $ 1.onClick(MainActivity.java:53) 在android.view.View.performClick(View.java:5657) 在android.view.View $ PerformClick.run(View.java:22453) 在android.os.Handler.handleCallback(Handler.java:751) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6236) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:891) 在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 ;
}
}
答案 0 :(得分:0)
正如你所说我想要的是当我点击按钮时,值5000变为5000 。如果这只是要求,那么我有一个替代解决方案。 试试这个
String a="5,00,000";
a=a.replaceAll(",","");
int num=Integer.parseInt(a);