gpt4 book ai didi

android - 检查 EditText 是否为空

转载 作者:行者123 更新时间:2023-11-29 17:52:18 25 4
gpt4 key购买 nike

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

该应用程序可以运行,但我有一个主要问题。如果用户未在 EditText 中键入任何内容并按下“提交”,应用将强制关闭。我看过其他问题,询问如何检查 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);

}

最佳答案

需要考虑的几点:

  • 您在 startb 上设置了 OnClickListener 两次,这只是一个拼写错误吗?
  • 在第二个 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
    }

关于android - 检查 EditText 是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22051686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com