0

我希望有人能帮帮忙!我对 android 还很陌生,并且遇到了文本字段的问题。我有 3 个文本字段要求用户输入信息,当单击注册按钮时,一条消息将让用户知道他们已注册并将他们重定向到登录活动。我的问题是我想检查用户是否已完成所有 3 个文本字段,如果没有显示错误消息并允许他们重试。我可以显示错误消息,但即使如果单击 reg 按钮,所有文本字段都不完整,它仍然会告诉用户他们已注册并将用户重定向到登录活动,如果有人能告诉我我出错了,我已经发布了我的代码, 提前致谢!

@Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   String data1 = inputName.getText().toString();
        if(data1.trim().equals("")){
            Toast.makeText(RegisterActivity.this, "Please enter your name",
                    Toast.LENGTH_SHORT).show();
}
   String data2 = inputEmail.getText().toString();
        if(data2.trim().equals("")){
            Toast.makeText(RegisterActivity.this, "please enter email address",
                    Toast.LENGTH_SHORT).show();
        }
   String data3 = inputPassword.getText().toString();
        if(data3.trim().equals("")){
            Toast.makeText(RegisterActivity.this, "Please enter a password",
          Toast.LENGTH_SHORT).show();
        }

   if(data1 != null && data2 != null && data3!= null){
   mySQLiteAdapter.insert(data1, data2, data3);
   updateList();
   Toast.makeText(RegisterActivity.this, "You are now registered",
           Toast.LENGTH_SHORT).show();

   startActivity(new Intent(getApplicationContext(), LoginActivity.class));
   inputName.setText("");
   inputEmail.setText("");
   inputPassword.setText("");
  }
        else Toast.makeText(RegisterActivity.this, "Error please try again!",
               Toast.LENGTH_SHORT).show();
  }

   };
4

3 回答 3

3

这里:

if(data1 != null && data2 != null && data3!= null)

那些不是null,只是空字符串("")。这总是正确的。

于 2013-02-08T18:59:16.177 回答
0

感谢您的有用回答,我想我已经解决了我的问题!希望我的解决方案对其他人有所帮助!

@Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   String data1 = inputName.getText().toString();
   String data2 = inputEmail.getText().toString();
   String data3 = inputPassword.getText().toString();

   boolean invalid = false;

    if(data1.equals("")){
        invalid = true;
        Toast.makeText(getApplicationContext(), "Username Missing", Toast.LENGTH_SHORT).show();
    }else if(data2.equals("")){
        invalid = true;
        Toast.makeText(getApplicationContext(), "Email ID Missing", Toast.LENGTH_SHORT).show();
    }else if(data3.equals("")){
        invalid = true;
        Toast.makeText(getApplicationContext(), "Password Missing", Toast.LENGTH_SHORT).show();
    }

    if(invalid == false){
        mySQLiteAdapter.insert(data1, data2, data3);
           updateList();
           Toast.makeText(RegisterActivity.this, "You are now registered",
                   Toast.LENGTH_SHORT).show();

        Intent i_register = new Intent(RegisterActivity.this, LoginActivity.class);
        startActivity(i_register);

        inputName.setText("");
        inputEmail.setText("");
        inputPassword.setText("");
        finish();
    }   

   /*mySQLiteAdapter.insert(data1, data2, data3);
   updateList();
   Toast.makeText(RegisterActivity.this, "You are now registered",
           Toast.LENGTH_SHORT).show();

   startActivity(new Intent(getApplicationContext(), LoginActivity.class));*/


  }

   };
于 2013-02-08T19:30:20.937 回答
0

你也可以使用TextUtils.isEmpty(string)

   if (!TextUtils.isEmpty(data1) && !TextUtils.isEmpty(data2) && !TextUtils.isEmpty(data2)) {
        Log.d(TAG, "All three Strings are not empty or null!");
    }
于 2013-02-08T19:07:06.623 回答