简体   繁体   English

检查 Edittext 是否为空,如果不是则执行特定任务

[英]Check if Edittext is empty and if it is not then perform a particular piece of task

I am making an app and I'm stuck with one problem.我正在制作一个应用程序,但我遇到了一个问题。 I want to know what can I do to make this happen:我想知道我该怎么做才能做到这一点:

I want to check if the edit text is empty or not and if it is not, then perform a particular piece of the task and if it is empty then make a toast which says please enter the names.我想检查编辑文本是否为空,如果不是,则执行任务的特定部分,如果为空,则举杯说请输入名称。

Here is my code: `这是我的代码:`

final TextView percentage = (TextView) findViewById(R.id.percentage_text);
Button calculateButton = (Button) findViewById(R.id.calculate_button);

    calculateButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Random noGen = new Random();
            int number = noGen.nextInt(101);

            EditText firestName = (EditText) findViewById(R.id.first_name);
            String firstNameString = firestName.getEditableText().toString();

            EditText secName = (EditText) findViewById(R.id.sec_name);
            String secNameString = secName.getEditableText().toString();
            percentage.setText(Integer.toString(number));

            Toast.makeText(getApplicationContext(), "The love score between " + firstNameString  +" & " + secNameString + " is "  + number + "%", Toast.LENGTH_SHORT ).show();
        }
    });

` `

I know I can easily do this with if else statement but I am not able to do it我知道我可以使用 if else 语句轻松做到这一点,但我无法做到

Thanks in advance!!!提前致谢!!!

Try this:尝试这个:

if(firestName.getText()!=null && 
   !firestName.getText().equals("") && secName.getText()!=null && 
   !secName.getText().equals("")){

   // do your task

}else{ 
  // show msg
}

You can check String variable that whether is empty or not.您可以检查字符串变量是否为空。 You can refer below code.你可以参考下面的代码。 You can find more String operation on Internet.您可以在 Internet 上找到更多字符串操作。

Button calculateButton = (Button) findViewById(R.id.calculate_button);
 EditText firestName = (EditText) findViewById(R.id.first_name);
 EditText secName = (EditText) findViewById(R.id.sec_name);

    calculateButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Random noGen = new Random();
            int number = noGen.nextInt(101);

            String firstNameString = firestName.getEditableText().toString();
            String secNameString = secName.getEditableText().toString();

            if(!firstNameString.isEmpty() && !secNameString.isEmpty()){
                //Perform your task
            }

            percentage.setText(Integer.toString(number));

            Toast.makeText(getApplicationContext(), "The love score between " + firstNameString  +" & " + secNameString + " is "  + number + "%", Toast.LENGTH_SHORT ).show();

        }
    });

Try below code;试试下面的代码;

 EditText firestName = (EditText) findViewById(R.id.first_name);
 String firstNameString = firestName.getEditableText().toString();

 EditText secName = (EditText) findViewById(R.id.sec_name);
 String secNameString = secName.getEditableText().toString();

 if(firstNameString.matches("") && secNameString.matches("")){
            //Do your code here
 }
if(firestName.getText().toString().equals("")){
  //do some stuff

} else{ Toast.makeText(getApplicationContext(), "Please enter", Toast.LENGTH_SHORT ).show(); } } else{ Toast.makeText(getApplicationContext(), "Please enter", Toast.LENGTH_SHORT ).show(); } else{ Toast.makeText(getApplicationContext(), "Please enter", Toast.LENGTH_SHORT ).show(); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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