Android:阻止警报中的EditText为空

时间:2014-04-03 23:51:32

标签: java android sqlite android-edittext android-alertdialog

我目前有一个带有editText的警告对话框。这个想法是一个人将输入他们的pin并且如果引脚是正确的,数据库中的那个做了一些事情,但是如果它是空白的话它会崩溃程序。所以我需要一些帮助来阻止用户将EditText留空。

我有我当前的警报代码:

AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
        alert.setTitle("Welcome!");
        alert.setMessage("Please select on of the following options:");
        // Set an EditText view to get user input
        final EditText input = new EditText(this);
        alert.setView(input);
        pindb = pin;
        statuses = status;
        lockerNumbers = lockerNumber;
        if (statuses != 0) {
            input.setInputType(InputType.TYPE_CLASS_NUMBER);
            alert.setMessage("Enter your pin");
            InputFilter[] FilterArray = new InputFilter[1];
            FilterArray[0] = new InputFilter.LengthFilter(4);
            input.setFilters(FilterArray);
            alert.setButton("Leave", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    String value = input.getText().toString();
                    // Do something with value!
                    pins = Integer.parseInt(value);
                    if (pins != pindb) {
                        Toast.makeText(MainActivity.this, "wrong",
                                Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(MainActivity.this, "correct",
                                Toast.LENGTH_LONG).show();
                        ourDb.open();
                        ourDb.removePin(lockerNumbers, pins, 0);
                        ourDb.close();
                    }

                    loadListView();
                }
            });

            alert.setButton2("Access", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    String value = input.getText().toString();
                    // Do something with value!
                    pins = Integer.parseInt(value);
                    if (pins != pindb) {
                        Toast.makeText(MainActivity.this, "wrong",
                                Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(MainActivity.this, "correct",
                                Toast.LENGTH_LONG).show();
                    }
                }
            });
        } else {
            input.setInputType(InputType.TYPE_CLASS_NUMBER);
            alert.setMessage("Enter a new 4 digit pin");
            alert.setButton3("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String value = input.getText().toString();
                    // Do something with value!
                    pins = Integer.parseInt(value);
                    if (statuses != 1) {
                        ourDb.open();
                        ourDb.createPin(lockerNumbers, pins, 1);
                        ourDb.close();
                        loadListView();

                    }
                }
            });
        }
        alert.show();

    }

有什么想法吗?

崩溃的Logcat:

04-03 17:22:30.109: E/AndroidRuntime(32612): java.lang.NumberFormatException: Invalid int: ""
04-03 17:22:30.109: E/AndroidRuntime(32612):    at java.lang.Integer.invalidInt(Integer.java:137)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at java.lang.Integer.parseInt(Integer.java:358)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at java.lang.Integer.parseInt(Integer.java:331)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at com.samsunglockercenter.MainActivity$2.onClick(MainActivity.java:171)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:174)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at android.os.Looper.loop(Looper.java:157)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at android.app.ActivityThread.main(ActivityThread.java:5872)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at java.lang.reflect.Method.invokeNative(Native Method)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at java.lang.reflect.Method.invoke(Method.java:515)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
04-03 17:22:30.109: E/AndroidRuntime(32612):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

获取pins中的值后,检查是否为0.如果不是那么有一个引脚,但如果是,则不执行任何操作/要求他们输入引脚。

pins = Integer.parseInt(value);
if(value.length() != 0 || !value.equals("") || value != null)
{
    if (pins != pindb) 
    {
        Toast.makeText(MainActivity.this, "wrong", Toast.LENGTH_LONG).show();
    } 
    else 
    {
        Toast.makeText(MainActivity.this, "correct", Toast.LENGTH_LONG).show();
        ourDb.open();
        ourDb.removePin(lockerNumbers, pins, 0);
        ourDb.close();
    }
}
else                   
{
    //ask to enter pin 
}

答案 1 :(得分:0)

试试这个..

获取字符串值检查if(value.length() <= 0|| value.equals(""){后,如果存在该条件,则显示toast 请输入Pin ,否则执行操作

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                String value = input.getText().toString().trim();

                if(value.length() <= 0|| value.equals(""){
                    Toast.makeText(MainActivity.this, "Please Enter Pin",
                            Toast.LENGTH_LONG).show();
                }else{
                // Do something with value!
                pins = Integer.parseInt(value);
                if (pins != pindb) {
                    Toast.makeText(MainActivity.this, "wrong",
                            Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this, "correct",
                            Toast.LENGTH_LONG).show();
                    ourDb.open();
                    ourDb.removePin(lockerNumbers, pins, 0);
                    ourDb.close();
                }

                loadListView();
               }
            }
        });