繁体   English   中英

检查EditText是否为空

[英]Check if EditText is empty

我正在制作一个数学应用程序。 我有两个数组有两组数字。 我让应用程序从每个数组中选择一个随机数。 该应用程序在TextView显示两个数字。 它要求用户在EditText键入添加数字的内容。 然后当用户按下“提交”时,应用程序将告诉用户他们是对还是错。

该应用程序工作,但我有一个主要问题。 如果用户未在EditText键入任何内容并按“提交”,则app force将关闭。 我已经查看了其他问题,询问如何检查EditText是否为空。 我尝试了不同的方法,但它仍然无效。

public class MainActivity extends Activity {
Button submitb, startb, idkb;
EditText et;
TextView tv, rig, wron;

int arrone[] = { 24, 54, 78, 96, 48, 65, 32, 45, 95, 13, 41, 55, 33, 22,
        71, 5, 22, 17, 13, 29, 63, 72, 14, 81, 7 }; // One Set of Numbers//
int arrtwo[] = { 121, 132, 147, 79, 82, 723, 324, 751, 423, 454, 448,
        524, 512, 852, 845, 485, 775, 186, 99 }; // Another set of Numbers//
int random1, random2, add;
int wrong = 0;
int right = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list();
    startb.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            int num = (int) (Math.random() * 25);
            int mun = (int) (Math.random() * 19);
            random1 = arrone[num];
            random2 = arrtwo[mun];
            String yu = (random1 + " + " + random2 + " =");
            tv.setText(yu);
            et.setHint("");
            idkb.setVisibility(0);
            startb.setText("Next Question");

        }
    });

    startb.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            int bread2 = 0;
            if (bread2 == 0) {
                Toast.makeText(getApplicationContext(),
                        "You didn't answer the question!",
                        Toast.LENGTH_LONG).show();

            }

            int swag = random1 + random2;
            String bread = et.getText().toString();
            bread2 = Integer.parseInt(bread);

            if (bread2 == swag) {
                startb.setText("Next Question");
                tv.setText("Correct");
                et.setText("");
                et.setHint("Click Next Question");
                right++;
                rig.setText("Right:" + right);
                rig.setTextColor(Color.parseColor("#14df11"));
                bread2 = 0;
            }

            else if (bread2 == 0) {
                tv.setText("Wrong, the correct answer is " + swag + ".");

                startb.setText("Next Question");
                et.setText("");
                tv.setHint("Click Next Question");
                wrong++;
                wron.setText("Wrong:" + wrong);
                wron.setTextColor(Color.parseColor("#ff0000"));
                bread2 = 0;
            }

            else {
                tv.setText("Wrong, the correct answer is " + swag + ".");

                startb.setText("Next Question");
                et.setText("");
                et.setHint("Click Next Question");
                wrong++;
                wron.setText("Wrong:" + wrong);
                wron.setTextColor(Color.parseColor("#ff0000"));
                bread2 = 0;

            }

        }

    });

    idkb.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            int swag = random1 + random2;

            tv.setText("The Correct Answer is  " + swag + ".");

            et.setText("");
            tv.setHint("Click Next Question");
            wrong++;
            wron.setText("Wrong:" + wrong);
            wron.setTextColor(Color.parseColor("#ff0000"));

        }
    });

}

private void list() {
    submitb = (Button) findViewById(R.id.b1);
    startb = (Button) findViewById(R.id.b2);
    et = (EditText) findViewById(R.id.et1);
    tv = (TextView) findViewById(R.id.tv1);
    rig = (TextView) findViewById(R.id.rtv);
    wron = (TextView) findViewById(R.id.wtv);
    idkb = (Button) findViewById(R.id.idk);

}

几点要考虑:

  • 你两次设置OnClickListener上STARTB,它只是一个错字?
  • 在第二个onClick()中,你有一个条件:

     int bread2 = 0; if (bread2 == 0) { //rest of code } 

这种情况总是会让人满意,这就是你想要的吗?

  • 您可以使用以下内容检查是否没有输入任何内容:

     String bread = et.getText().toString(); // check if there is any text entered by user if (bread.length() > 0) { bread2 = Integer.parseInt(bread); if (bread2 == swag) { startb.setText("Next Question"); //the rest of the code } 

不确定我是否正确理解你,但它应该很容易:

et = (EditText) findViewById(R.id.et1);
String bread = et.getText().toString();
if (bread.length() == 0){
   // raise error dialogue
} else {
   // continue to check the parsed integer
}

这应该都在您的点击监听器中完成。

你需要在et之前使用findViewById才能获得它的值。

顺便说一句,你将bread2值设置为0,并在下一行验证它是否为0,我想这只是一个测试,不是吗?

    @Override
    public void onClick(View v) {

        int bread2 = 0;
        if (bread2 == 0) {
            Toast.makeText(getApplicationContext(),
                    "You didn't answer the question!",
                    Toast.LENGTH_LONG).show();

        }

        int swag = random1 + random2;

        et = findViewById(R.id.yourEditText);

        String bread = et.getText().toString();
        bread2 = Integer.parseInt(bread);

        //...

希望能帮助到你!

我认为检查文本长度是一种很好的做法,例如这个功能可以帮助你...

 public boolean isEditTextEmpty(EditText input){
   if(input.getText().length() == 0)
      return true;
   else
      return false;
  }
    if (et.getText().length == 0 || et.gettext == null ){
        // do nothing
   } else {
       // do whatever here
   }

这应该没问题

暂无
暂无

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

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