從EditText比較輸入整數時遇到問題。我無法找到它有什麼問題。請幫幫我。這是下面的代碼。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 ==概率)? – 2012-08-16 00:48:33
可以喲把Log.i(「@@@」,probString);在字符串之後probString = textIn.getText()。toString();並檢查logcat中的輸出是什麼? – Hesam 2012-08-16 00:49:41
try(probInt == prob)它總是返回else語句 – 2012-08-16 00:51:50