简体   繁体   中英

Making required EditText field in Android Studio

I am starting to make an app. I currently have one EditText. How do I make it required? When nothing is entered into the EditText! the message Please Enter a username should flash on the screen but it still goes to the next scene/activity. How do I stop the submit if the length is 0. I put return false into the public void but I get the following message cannot return a value from a method with void result type

    public void sendMessage(View view){
    Intent intent = new Intent(this,DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.editText);
    String message = editText.getText().toString();

    //Trim whitespace
    message = message.trim();

    //Checks if the message has anything.
    if (message.length() == 0)
    {
        editText.setError("Please Enter a username!");
        //return false;
    }
    intent.putExtra(EXTRA_MESSAGE,message);
    startActivity(intent);
}

try this

string text=editText.getText().toString().trim();
if (TextUtils.isEmpty(text)){
            editText.setError("Please Enter a username!");
        }else {
            //do something
        }

Instead of return false; , just write return; with no return type (because the method's return type is void ) and it should let you leave the method.

Modify the function like the following.

public void sendMessage(View view){
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.editText);
    String message = editText.getText().toString();

    //Trim whitespace
    message = message.trim();

    //Checks if the message has anything.
    if (message.length() == 0) {
        editText.setError("Please Enter a username!");
        return;
    }

    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

Use if and else clause will achieve what you want.

//Checks if the message has anything.
if (message.length() == 0)
{
    editText.setError("Please Enter a username!");
    //return false;
} else {
    intent.putExtra(EXTRA_MESSAGE,message);
    startActivity(intent);
}

Try this

public void sendMessage(View view){
Intent intent = new Intent(this,DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
if (!emptyEdittext(editText, "Please Enter a username!")) 
{
  intent.putExtra(EXTRA_MESSAGE,message);
  startActivity(intent);
}}

2.Call below method for validation

public boolean emptyEdittext(EditText editText, String msg)
{
    /*check edittext length*/
    if(editText.getText().toString().length()==0)
    {
        Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
        return true;
    }
    return false;
}

The issue here is that a TextEdit.getText().toString() doesn't give you a null value... It gives you an empty string, which is still a string. Try this. It is the way I do it.

public void sendMessage(View view){
    Intent intent = new Intent(this,DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.editText);
    String message = editText.getText().toString();

    //Trim whitespace
    message = message.trim();

    //Checks if the message has anything. This checks to see if 
      //it has an empty string rather than a null
    if (message.equals(""))
    {
        Toast.makeText(getApplicationContext(),"Please provide a 
               message!, Toast.LENGTH_SHORT").show();
    }else
    intent.putExtra(EXTRA_MESSAGE,message);
    startActivity(intent);
}

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