我想做一个 isnumeric 方法, 检查如果用户输入空白/字母,它会 toast “请输入数字”
我不知道,为什么当我以字母格式键入第一个编辑文本框时, 它不会有错误, 但是,当我以字母格式或数字格式键入第二个编辑文本框时, 将显示强制关闭错误。
我已经为我所有的编辑文本设置了方法,但它似乎不起作用。
01-26 15:23:43.078: E/AndroidRuntime(30816): FATAL EXCEPTION: main
01-26 15:23:43.078: E/AndroidRuntime(30816): java.lang.NumberFormatException: Invalid double: "4dg"
01-26 15:23:43.078: E/AndroidRuntime(30816): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
01-26 15:23:43.078: E/AndroidRuntime(30816): at java.lang.StringToReal.parseDouble(StringToReal.java:269)
01-26 15:23:43.078: E/AndroidRuntime(30816): at java.lang.Double.parseDouble(Double.java:295)
01-26 15:23:43.078: E/AndroidRuntime(30816): at com.example.fuellogproject.MainActivity$4.afterTextChanged(MainActivity.java:197)
01-26 15:23:43.078: E/AndroidRuntime(30816): at android.widget.TextView.sendAfterTextChanged(TextView.java:7562)
01-26 15:23:43.078: E/AndroidRuntime(30816): at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:9383)
01-26 15:23:43.078: E/AndroidRuntime(30816): at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
01-26 15:23:43.078: E/AndroidRuntime(30816): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
01-26 15:23:43.078: E/AndroidRuntime(30816): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
01-26 15:23:43.078: E/AndroidRuntime(30816): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
01-26 15:23:43.078: E/AndroidRuntime(30816): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:676)
01-26 15:23:43.078: E/AndroidRuntime(30816): at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:196)
01-26 15:23:43.078: E/AndroidRuntime(30816): at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:183)
01-26 15:23:43.078: E/AndroidRuntime(30816): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:279)
01-26 15:23:43.078: E/AndroidRuntime(30816): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
01-26 15:23:43.078: E/AndroidRuntime(30816): at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 15:23:43.078: E/AndroidRuntime(30816): at android.os.Looper.loop(Looper.java:137)
01-26 15:23:43.078: E/AndroidRuntime(30816): at android.app.ActivityThread.main(ActivityThread.java:4921)
01-26 15:23:43.078: E/AndroidRuntime(30816): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 15:23:43.078: E/AndroidRuntime(30816): at java.lang.reflect.Method.invoke(Method.java:511)
01-26 15:23:43.078: E/AndroidRuntime(30816): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
01-26 15:23:43.078: E/AndroidRuntime(30816): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
01-26 15:23:43.078: E/AndroidRuntime(30816): at dalvik.system.NativeStart.main(Native Method)
public void onClick(View view) {
if(view.getId() == R.id.cancelBTN){
Intent viewAll=new Intent(getApplicationContext(),ViewAll.class);
startActivity(viewAll);
}else if(view.getId() == R.id.saveBTN){
// Get the values provided by the user via the UI
String date = dateEdit.getText().toString();
String price = priceEdit.getText().toString();
String pump = pumpEdit.getText().toString();
String tcost = costView.getText().toString();
String odometer = odometerEdit.getText().toString();
String fcon = fconView.getText().toString();
if(isNumeric(priceEdit.getText().toString())){
Intent viewAll=new Intent(getApplicationContext(),ViewAll.class);
startActivity(viewAll);
if(!odometer.trim().equalsIgnoreCase(""))
{
SharedPreferences.Editor editor=sp.edit();
editor.putString("ODO", odometer.trim());
editor.commit();
}
else
{
SharedPreferences.Editor editor=sp.edit();
editor.putString("ODO", "NA");
editor.commit();
}
// Pass above values to the setter methods in POJO class
fuelLogPojo fuelLogPojoObj = new fuelLogPojo();
fuelLogPojoObj.setdate(date);
fuelLogPojoObj.setprice(price);
fuelLogPojoObj.setpump(pump);
fuelLogPojoObj.setcost(tcost);
fuelLogPojoObj.setodometer(odometer);
fuelLogPojoObj.setfcon(fcon);
// Add an undergraduate with his all details to a ArrayList
fuelLogArrayList.add(fuelLogPojoObj);
// Inserting undergraduate details to the database is doing in a separate method
insertLog(fuelLogPojoObj);
}
else if (priceEdit.getText().toString().trim().length() > 0)
{
Toast.makeText(getApplicationContext(), "Please enter a number", Toast.LENGTH_SHORT).show();
}
else if(isNumeric(pumpEdit.getText().toString())){
Toast.makeText(getApplicationContext(), "Please enter a number", Toast.LENGTH_SHORT).show();
}
else if (pumpEdit.getText().toString().trim().length() > 0) {
Toast.makeText(getApplicationContext(), "Please enter a number", Toast.LENGTH_SHORT).show();
}
else if(isNumeric(odometerEdit.getText().toString())){
Toast.makeText(getApplicationContext(), "Please enter a number", Toast.LENGTH_SHORT).show();
}
else if (odometerEdit.getText().toString().trim().length() > 0) {
Toast.makeText(getApplicationContext(), "Please enter a number", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "Please enter a valid number", Toast.LENGTH_SHORT).show();
}
完整代码:
public class MainActivity extends Activity implements OnClickListener {
// TableLayout tablelayout_Log = null;
private Button saveButton = null;
private Button cancelButton = null;
// Button searchButton = null;
private static EditText dateEdit;
private EditText priceEdit;
private EditText pumpEdit;
private TextView costView;
private EditText odometerEdit;
private TextView fconView;
private TextWatcher textWatcher;
private String priceEditStr ="",pumpEditStr="";
private String odmEditStr = "";
private String lastOdm = "";
private ArrayList<fuelLogPojo> fuelLogArrayList;
double result;
double resultCon;
private int mYear;
private int mMonth;
private int mDay;
SharedPreferences sp;
static final int DATE_DIALOG_ID = 0;
public boolean isNumeric(String str)
{
return str.matches("-?\\d+(\\.\\d+)?");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
costView = (TextView)findViewById(R.id.tcost);
dateEdit = (EditText)findViewById(R.id.date);
priceEdit = (EditText)findViewById(R.id.fuelprice);
pumpEdit = (EditText)findViewById(R.id.fuelpump);
odometerEdit = (EditText)findViewById(R.id.odometer);
fconView = (TextView)findViewById(R.id.fcon);
fuelLogArrayList = new ArrayList<fuelLogPojo>();
cancelButton = (Button) findViewById(R.id.cancelBTN);
cancelButton.setOnClickListener(this);
saveButton = (Button) findViewById(R.id.saveBTN);
saveButton.setOnClickListener(this);
//Shared preference
sp=this.getSharedPreferences("result_store", MODE_WORLD_READABLE);
lastOdm=sp.getString("ODO", lastOdm);
if(lastOdm.trim().equalsIgnoreCase("NA"))
Toast.makeText(getApplicationContext(), " "+lastOdm.trim(), Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), " "+lastOdm.trim(), Toast.LENGTH_LONG).show();
// DBAdapter dbAdaptor = new DBAdapter(getApplicationContext());
// lastOdm = dbAdaptor.getLastOdometer();
//Check that your database is enable to fetch the value or not?
//Toast.makeText(getApplicationContext()," "+lastOdm,Toast.LENGTH_LONG).show();
dateEdit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// showDialog(DATE_DIALOG_ID);
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
});
priceEdit.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !priceEdit.getText().toString().trim().equalsIgnoreCase(null))
priceEditStr = priceEdit.getText().toString().trim();
if(!pumpEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(null))
pumpEditStr = pumpEdit.getText().toString().trim();
if(isNumeric(priceEdit.getText().toString())){
if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
{
result = Double.parseDouble(priceEditStr) * Double.parseDouble(pumpEditStr);
costView.setText(" "+result);
}
}
else
{
Toast.makeText(getApplicationContext(), "Please enter a valid number", Toast.LENGTH_SHORT).show();
}
}
});
pumpEdit.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
if(!priceEdit.getText().toString().trim().equalsIgnoreCase(""))
priceEditStr = priceEdit.getText().toString().trim();
if(!pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
pumpEditStr = pumpEdit.getText().toString().trim();
if(isNumeric(pumpEdit.getText().toString())){
if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
{
result = Double.parseDouble(priceEditStr) * Double.parseDouble(pumpEditStr);
costView.setText(" "+result);
}
}
else
{
Toast.makeText(getApplicationContext(), "Please enter a valid number", Toast.LENGTH_SHORT).show();
}
}
});
odometerEdit.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
if(!odometerEdit.getText().toString().trim().equalsIgnoreCase(""))
odmEditStr = odometerEdit.getText().toString().trim();
if(isNumeric(odometerEdit.getText().toString())){
if(!odometerEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase("") && !lastOdm.trim().equalsIgnoreCase("") && !lastOdm.trim().equalsIgnoreCase("NA"))
{
resultCon = (Double.parseDouble(odmEditStr) - Double.parseDouble(lastOdm) )/ Double.parseDouble(pumpEditStr);
fconView.setText(" "+resultCon);
}
}
else
{
Toast.makeText(getApplicationContext(), "Please enter a valid number", Toast.LENGTH_SHORT).show();
}
}
});
}
public void onClick(View view) {
if(view.getId() == R.id.cancelBTN){
Intent viewAll=new Intent(getApplicationContext(),ViewAll.class);
startActivity(viewAll);
}else if(view.getId() == R.id.saveBTN){
// Get the values provided by the user via the UI
String date = dateEdit.getText().toString();
String price = priceEdit.getText().toString();
String pump = pumpEdit.getText().toString();
String tcost = costView.getText().toString();
String odometer = odometerEdit.getText().toString();
String fcon = fconView.getText().toString();
if(isNumeric(priceEdit.getText().toString())){
Intent viewAll=new Intent(getApplicationContext(),ViewAll.class);
startActivity(viewAll);
if(!odometer.trim().equalsIgnoreCase(""))
{
SharedPreferences.Editor editor=sp.edit();
editor.putString("ODO", odometer.trim());
editor.commit();
}
else
{
SharedPreferences.Editor editor=sp.edit();
editor.putString("ODO", "NA");
editor.commit();
}
// Pass above values to the setter methods in POJO class
fuelLogPojo fuelLogPojoObj = new fuelLogPojo();
fuelLogPojoObj.setdate(date);
fuelLogPojoObj.setprice(price);
fuelLogPojoObj.setpump(pump);
fuelLogPojoObj.setcost(tcost);
fuelLogPojoObj.setodometer(odometer);
fuelLogPojoObj.setfcon(fcon);
// Add an undergraduate with his all details to a ArrayList
fuelLogArrayList.add(fuelLogPojoObj);
// Inserting undergraduate details to the database is doing in a separate method
insertLog(fuelLogPojoObj);
}
else if (priceEdit.getText().toString().trim().length() > 0)
{
Toast.makeText(getApplicationContext(), "Please enter a number", Toast.LENGTH_SHORT).show();
}
else if(isNumeric(pumpEdit.getText().toString())){
Toast.makeText(getApplicationContext(), "Please enter a number", Toast.LENGTH_SHORT).show();
}
else if (pumpEdit.getText().toString().trim().length() > 0) {
Toast.makeText(getApplicationContext(), "Please enter a number", Toast.LENGTH_SHORT).show();
}
else if(isNumeric(odometerEdit.getText().toString())){
Toast.makeText(getApplicationContext(), "Please enter a number", Toast.LENGTH_SHORT).show();
}
else if (odometerEdit.getText().toString().trim().length() > 0) {
Toast.makeText(getApplicationContext(), "Please enter a number", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "Please enter a valid number", Toast.LENGTH_SHORT).show();
}
// Release from the existing UI and go back to the previous UI
//Intent viewAll=new Intent(getApplicationContext(),ViewAll.class);
// startActivity(viewAll);
}
}
private void insertLog(fuelLogPojo fuelLogPojoObj) {
// TODO Auto-generated method stub
// First we have to open our DbHelper class by creating a new object of that
AndroidOpenDbHelper androidOpenDbHelperObj = new AndroidOpenDbHelper(this);
// Then we need to get a writable SQLite database, because we are going to insert some values
// SQLiteDatabase has methods to create, delete, execute SQL commands, and perform other common database management tasks.
SQLiteDatabase sqliteDatabase = androidOpenDbHelperObj.getWritableDatabase();
// ContentValues class is used to store a set of values that the ContentResolver can process.
ContentValues contentValues = new ContentValues();
// Get values from the POJO class and passing them to the ContentValues class
contentValues.put(AndroidOpenDbHelper.KEY_DATE, fuelLogPojoObj.getdate());
contentValues.put(AndroidOpenDbHelper.KEY_PRICE, fuelLogPojoObj.getprice());
contentValues.put(AndroidOpenDbHelper.KEY_FUEL, fuelLogPojoObj.getpump());
contentValues.put(AndroidOpenDbHelper.KEY_COST, fuelLogPojoObj.getcost());
contentValues.put(AndroidOpenDbHelper.KEY_ODM, fuelLogPojoObj.getodometer());
contentValues.put(AndroidOpenDbHelper.KEY_CON, fuelLogPojoObj.getfcon());
// Now we can insert the data in to relevant table
// I am going pass the id value, which is going to change because of our insert method, to a long variable to show in Toast
long affectedColumnId = sqliteDatabase.insert(AndroidOpenDbHelper.TABLE_NAME_LOG, null, contentValues);
// It is a good practice to close the database connections after you have done with it
sqliteDatabase.close();
// I am not going to do the retrieve part in this post. So this is just a notification for satisfaction ;-)
Toast.makeText(this, "Values inserted column ID is :" + affectedColumnId, Toast.LENGTH_SHORT).show();
}
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
public EditText editText;
DatePicker dpResult;
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
//return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
dateEdit.setText(String.valueOf(day) + "/"
+ String.valueOf(month + 1) + "/" + String.valueOf(year));
// set selected date into datepicker also
}}}
最佳答案
您的字符串 priceEditStr
包含 2 个字母“dg”,因此在尝试转换为 double 时会崩溃。您应该在转换值时处理预期的 Exceptions
。
请像这样编辑您的代码:
@Override
public void afterTextChanged(Editable editable) {
//here, after we introduced something in the EditText we get the string from it
if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !priceEdit.getText().toString().trim().equalsIgnoreCase(null))
priceEditStr = priceEdit.getText().toString().trim();
if(!pumpEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(null))
pumpEditStr = pumpEdit.getText().toString().trim();
if((!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))&&((priceEdit.getText().toString().trim().matches("[0-9]+"))&&(pumpEdit.getText().toString().trim().matches("[0-9]+"))))
{
result = "";
try
{
result = Double.parseDouble(priceEditStr) * Double.parseDouble(pumpEditStr.replace("dg", "").trim());
}
catch(NumberFormatException nfe)
{
//Stack trace will be printed in your Logcat, if the app crashes in the try block with NumberFormatException exception
nfe.printStackTrace();
}
catch(Exception e)
{
//Stack trace will be printed in your Logcat, if the app crashes in the try block with some unhandled exception
e.printStackTrace();
}
costView.setText(" "+result);
}
else
{
Toast.makeText(getApplicationContext(), "Please enter a valid number", Toast.LENGTH_SHORT).show();
}
同时编辑你的 isNumeric(..)
方法,如下所示:
public boolean isNumeric(String str)
{
return str.matches("[0-9]+");
}
希望对您有所帮助。
关于android - 当我尝试输入数字或字母时强制关闭,即使我已经设置了一个 isnumeric 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21360890/