随机字符串工作正常。
现在检查不行。我将文本输入到已绘制的 EditText 中。
但检查不起作用。为什么?代码:
public static StringBuffer random() {
String str = new String(
"G12HIJdefgPQRSTUVWXYZabc56hijklmnopqAB78CDEF0KLMNO3rstu4vwxyz9");
StringBuffer sb = new StringBuffer();
sb.toString();
String ar = null;
Random r = new Random();
int te = 0;
for (int i = 1; i <= 10; i++) {
te = r.nextInt(62);
ar = ar + str.charAt(te);
sb.append(str.charAt(te));
}
return sb;
}
public void onCreate(Bundle icicle) {
setContentView(R.layout.main);
random = random().toString();
TextView display = (TextView) findViewById(R.id.textView1);
display.setText("Random Number: " + random); // Show the random number
et = (EditText) findViewById(R.id.etNumbers);
ok = (Button) findViewById(R.id.button1);
ok.setOnClickListener(this);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
charsEntered = et.getText().toString();
} catch (NumberFormatException nfe) {
Toast.makeText(et.getContext(), "Bla bla bla", Toast.LENGTH_LONG)
.show();
}
if (random == charsEntered) {
Toast.makeText(et.getContext(), "Good!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(et.getContext(), "Bad!", Toast.LENGTH_LONG).show();
}
}
}