【发布时间】:2012-08-16 00:42:00
【问题描述】:
比较来自 EditText 的输入整数时遇到问题。我找不到它有什么问题。请帮我。这是下面的代码。
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.problem2);
textIn = (EditText) findViewById(R.id.probText);
Button ans3 = (Button) findViewById(R.id.answer3);
ans3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String probString = textIn.getText().toString();
Integer probInt = Integer.parseInt(probString);
Integer prob = 31;
if (probInt.equals(prob)) {
Toast toast = Toast.makeText(answer3.this,"CORRECT!",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
startActivity(new Intent("com.sample.androidsample.ANSWER4") );
} else {
Toast toast = Toast.makeText(answer3.this,"Wrong answer! Try again.",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
}
}
});
}
【问题讨论】:
-
你试过了吗(probInt == prob)?
-
你能把 Log.i("@@@", probString);在 String probString = textIn.getText().toString() 之后;并检查 logcat 中的输出是什么?
-
试过 (probInt == prob) 它总是返回 else 语句
-
始终清理您的输入,首先使用
Pattern清除错误输入,然后知道模式匹配,然后可以安全地将其解析为Integer. -
尝试修剪它。 String probString = textIn.getText().toString().trim();
标签: android integer android-edittext compare