注册isEmpty

时间:2017-10-22 19:36:09

标签: java android

//GET TEXTS //
    final String firstName= editText_firstName.getText().toString();
    final String lastName = editText_lastName.getText().toString();
    final String email = editText_email.getText().toString();
    //final String password = editText_password.getText().toString();
    //final String dateOfBirth = editText_dateOfBirth.getText().toString();
    //final String idNumber = editText_idNumber.getText().toString();



    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           if(firstName.isEmpty() || firstName.length() == 0 || firstName.equals("") || firstName == null){


               Toast.makeText(MainActivity.this, "Fill the name", Toast.LENGTH_SHORT).show();
           }
           else{

               Toast.makeText(MainActivity.this, "Successful", Toast.LENGTH_SHORT).show();
           }
        }
    });

为什么吐司结果总是“填写名称”即使我填补了空白? 输出永远不会给出“成功”的结果

2 个答案:

答案 0 :(得分:1)

您必须在editText_firstName.getText().toString();听众

中获得onClick()的值

答案 1 :(得分:0)

因为您在活动加载时设置firstName,并且文本字段在此时为空。您应该在点击按钮的开头调用String firstName= editText_firstName.getText().toString();