我正在创建一个包含用户名和密码作为必填字段的登录页面。如果这些必填字段为空,则应抛出错误消息“用户名和密码为空”。但是根据以下代码没有任何反应。
以下是我的代码
public class Login_testingActivity extends Activity{
EditText edit1,edit2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText edit1 = (EditText) findViewById(R.id.editText_username);
EditText edit2 = (EditText) findViewById(R.id.editText_password);
if (edit1.getText().length() != 0 && edit2.getText().length() != 0) {
fun();
} else {
Toast.makeText(Login_testingActivity.this, "You need to enter a high AND low.", Toast.LENGTH_SHORT);
}
}
public boolean fun(){
Button next = (Button) findViewById(R.id.button_login);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Login_2.class);
startActivityForResult(myIntent, 0);
}
});
return true;
}
}
答案 0 :(得分:7)
Toast.makeText(Login_testingActivity.this, "You need to enter a high AND low.", Toast.LENGTH_SHORT).show();
答案 1 :(得分:1)
首先这一行if (edit1.getText().trim().length() != 0 && edit2.getText().trim().length() != 0) {
添加onClick(View view)
并检查if(){startActivty(...);}else{Toast here..}
答案 2 :(得分:1)
尝试这样,
if (edit1.getText().equalsIgnoreCase("") && edit2.getText().equalsIgnoreCase(""))
{
Toast.makeText(Login_testingActivity.this, "You need to enter a high AND low.", Toast.LENGTH_SHORT).show();
}
else
{
fun();
}
答案 3 :(得分:0)
我希望这对你有用。
if (!edit1.getText().equals("") && !edit2.getText().equals("")) {
fun();
} else {
Toast.makeText(Login_testingActivity.this, "You need to enter a high AND low.", Toast.LENGTH_SHORT).show();
}
答案 4 :(得分:-1)
将此行用作
if (edit1.getText().toString().length() > 0 && edit2.getText().toString().length() > 0) {
fun();
} else {
Toast.makeText(Login_testingActivity.this, "You need to enter a high AND low.", Toast.LENGTH_SHORT).show();
}