如何确保用户在加载下一个屏幕之前输入所有细节

时间:2013-12-23 10:48:03

标签: java android exception android-edittext

我希望用户在加载下一个屏幕之前在edittext中输入所有文本。如果用户按下完成并且他们没有填写所有细节,它应该显示一条消息,告诉他们这样做。

try {
                if (Integer.toString(cigSmoked).length() > 0) {
                    cigSmoked = Integer.parseInt(cigSmokedText.getText()
                            .toString());
                    editor.putInt("smokedCigaretes", cigSmoked);
                    editor.commit();
                }
                if (Integer.toString(cigBox).length() > 0) {
                    cigBox = Integer.parseInt(cigBoxText.getText()
                            .toString());
                    editor.putInt("cigaretteBox", cigBox);
                    editor.commit();
                }
                if (Float.toString(cigCost).length() > 0) {
                    cigCost = Float.parseFloat(cigCostText.getText()
                            .toString());
                    editor.putFloat("cigaretteCost", cigCost);
                    editor.commit();
                }

            } catch (Exception e) {
                Toast.makeText(getApplicationContext(),
                        "Did NOT save settings, please fill in all fields", Toast.LENGTH_LONG)
                        .show();
                ;
            }

            int day = quitYearText.getDayOfMonth();
            int month = quitYearText.getMonth();
            int year = quitYearText.getYear();
            editor.putInt("day", day);
            editor.putInt("month", month);
            editor.putInt("year", year);
            editor.commit();

            Context context = getApplicationContext();
            CharSequence text = "Saved!";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();

            Intent myIntent = new Intent(Settings.this, Home.class);
            Settings.this.startActivity(myIntent);

        }

3 个答案:

答案 0 :(得分:1)

您只需从edittext获取值并检查从editext获得的字符串长度。如下。

String str=editext.getText().tostring.trim(); 
String str2=editext2.getText().tostring.trim(); 
..
..
if(str.length()>0 || str2.length()>0 ......){
   //your navigation code
}else{
  //your alert message
}

为每个editext执行此操作。希望这对你有所帮助。

答案 1 :(得分:0)

你可以这样做,不需要写那么多条件

String str=editText1.getText.toString();
String str1=editText2.getText.toString();
.
.
.
.
String strn=editTextn.getText.toString();

现在检查条件

if(str.length()>0 || str1.length>0 || strn.length>0){
               // whatever code you want to put
}else{
   //alert dialog saying field is empty
}

希望这会有所帮助......

答案 2 :(得分:0)

写一个函数。

 private String validateForm()
{
    try
    {
        if(mEditTextFirstName.getText().toString().trim().equals(""))
        {
            return "Oops..plz Enter First Name";
        }
        if(mEditTextLastName.getText().toString().trim().equals(""))
        {
            return "Oops..plz Enter Last Name";
        }
        if(mEditTextEmail.getText().toString().trim().equals(""))
        {
            return "Oops..plz Enter Email";
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    catch(Error e)
    {
        e.printStackTrace();
    }
    return "";
}

然后,

String errorMessage = validateForm();
            if(errorMessage.equalsIgnoreCase(""))
            {
                             // Do whatever You want.
            }               
            else
            {
                       Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT).show();
            }