为什么我的按钮永不停止?

时间:2013-11-26 08:13:31

标签: java android if-statement switch-statement

在我的代码中,我使用了switch语句。当变量(returnString)(和不,它只是命名为String,它实际上是一个整数)是0,1,2或3时,我只是将TextView设置为某种东西,以防万一它是4,它使用intent转到我的其他活动。另一方面,如果EditText为空,则忽略它们并启动另一个活动。我希望它停止并将TextView设置为"Please enter username or password"

switch (returnString) {
            case 0: tv.setText("Invalid Entry. Please re-enter Username or Password - 0.");

                break;

            case 1: tv.setText("Invalid Entry. Please re-enter Username or Password - 1.");
                break;

            case 2: tv.setText("Invalid Entry. Please re-enter Username or Password - 2.");
                break;

            case 3: tv.setText("Invalid Entry. Please re-enter Username or Password - 3.");
                break;

            case 4: 
                    startActivity(intent);

                break;

            default: 

                String sUsername = editUserName.getText().toString();
                String sPassword = editPassword.getText().toString();
                if (sUsername.matches("") & sPassword.matches("")){
                tv.setText("Please enter username or password");
                break;
            }
                break;
            }

2 个答案:

答案 0 :(得分:2)

Switch / Case在您的示例中对整数进行操作,因此您应该将字符串转换为整数:

int returnInt = Integer.parseInt(returnString);
switch (returnInt) {

答案 1 :(得分:1)

将此代码添加到您的按钮: -

if(edittext.getText().length() = 0)
   toast.maketest(getApplicationContext(),"Please enter username and password",Toast.LENGTH_SHORT).show()
else
  //do your thing