我遇到了问题,如何比较textview和edittext,这里是代码
public class GameApps extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
TextView score;
TextView right;
TextView wrong;
TextView totalquestion;
TextView question;
EditText answer;
Button submit;
Button exit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
score = (TextView)findViewById(R.id.textView6);
right = (TextView)findViewById(R.id.textView7);
wrong = (TextView)findViewById(R.id.textView8);
totalquestion = (TextView)findViewById(R.id.textView9);
question = (TextView)findViewById(R.id.textView4);
question.setText("banana");
answer = (EditText)findViewById(R.id.editText1);
submit = (Button)findViewById(R.id.button1);
submit.setOnClickListener(this);
exit = (Button)findViewById(R.id.button3);
exit.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
**question.getText().toString();
answer.getText().toString();
if(v==submit){
if(question.equals(answer)){
Toast.makeText(this, "Right answer", Toast.LENGTH_LONG).show();
}
else Toast.makeText(this, "Wrong answer", Toast.LENGTH_LONG).show();
}**
if(v==exit){
finish();
}
}
}
我有比较答案和问题,但它没有匹配,每次我比较它总是说错误的答案,如何比较它,任何人都可以解决这个问题?我搞砸了这个:(
答案 0 :(得分:2)
if(question.getText().toString().equals(answer.getText().toString()))
{
// do something
}
else {
//do something
}
答案 1 :(得分:1)
public void onClick(View v)
{
switch(v.getId())
{
case R.id.button1:
if(question.getText().toString().equals(answer.getText().toString()))
Toast.makeText(v.getContext(),"Correct",Toast.LENGTH_LONG).show();
else
Toast.makeText(v.getContext(),"Wrong",Toast.LENGTH_LONG).show();
break;
case R.id.button3:
finish();
break;
}
}
答案 2 :(得分:0)
您正在将TextView
对象与EditText
对象进行比较。
相反,您需要比较他们的内容。
所以
if(question.equals(answer))
将是
String q = question.getText().toString();
String a = answer.getText().toString();
if(q.equals(a))