0

我正在使用 IF 语句来检查值 MobileNumber 是否以 07 开头。这是我的代码:

public void MobileNumberCheck(EditText MobileNumber_Text, TextView ErrorDisplay, Boolean Output) {
        Editable MobileNumber = MobileNumber_Text.getText();
        Output = false;
        ///This method only has to check that the mobile number starts with 07, as the XML specifies the number can only be number, and 11 digits long.
        if( (MobileNumber.charAt(0) != "0".charAt(0) ) || (MobileNumber.charAt(1) != "7".charAt(0)) ){
                ErrorDisplay.setText("Invalid UK mobile number");
        }
            else if( (MobileNumber.charAt(0) == "0".charAt(0)) && (MobileNumber.charAt(1) == "7".charAt(0)) ){

                    ///Do nothing
                    ErrorDisplay.setText("");
                    Output = true;


            }
            else
                ErrorDisplay.setText(MobileNumber.charAt(0) + MobileNumber.charAt(1));
    }





public void AddPeople_Save(View view){
    TextView ErrorDisplay = (TextView) findViewById(R.id.AddPeople_ErrorDisplay);
    EditText MobileNumber_ET = (EditText) findViewById(R.id.AddPeople_MobileNumber);
    Boolean WriteReady = false;
    MobileNumberCheck(MobileNumber_ET, ErrorDisplay, WriteReady); ///Runs the method specified above. Doesn't output, except into the TEXTVIEW ErrorDisplay.
    EditText Name_ET = (EditText) findViewById(R.id.AddPeople_Name);

    String AddPeople_PeopleFilename = "PartyApp_PeopleFile";
    String Person_Details = Name_ET.toString() + MobileNumber_ET.toString();
    FileOutputStream outputStream;
    if(WriteReady == true)
            try {
                outputStream = openFileOutput(AddPeople_PeopleFilename, Context.MODE_PRIVATE);
                outputStream.write(Person_Details.getBytes());
                outputStream.close();
            } catch (Exception e) {
      e.printStackTrace();
    }
    else{
        ///Do nothing. The error message is passed through METHOD MobileNumberCheck.
        ErrorDisplay.setText("AAA" + WriteReady);

    }

你能告诉我我做错了什么吗?我假设我的逻辑运算符 && 和 || 是错误的,或者string.CharAt()正在执行错误。感谢!!

4

2 回答 2

2

此方法只需要检查手机号码是否以 07 开头,因为 XML 指定号码只能是数字,并且长度为 11 位。

我只会使用:

if(MobileNumber.toString().startsWith("07") && MobileNumber.length() <= 11){

}
于 2013-10-28T10:42:48.187 回答
0

if(num.startsWith("0") && num.startsWith("7", 1))if(num.startsWith("07"))将工作。

于 2013-10-28T10:55:44.210 回答