简体   繁体   中英

checking textbox as null or not java android

I am creating a login page with username and password as mandatory fields. Error message "Username & Password is empty" should be thrown if these mandatory fields are empty. But nothing happens as per the below code.

Following is My Code

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;
   }
}
 Toast.makeText(Login_testingActivity.this, "You need to enter a high AND low.", Toast.LENGTH_SHORT).show();

First this line if (edit1.getText().trim().length() != 0 && edit2.getText().trim().length() != 0) {

add in onClick(View view) and check there if(){startActivty(...);}else{Toast here..}

try like this,

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();

    }

I hope this work for you.

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();
}

Use This Lines As

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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM