Android 在eclipse上比较TextView和EditText
我遇到了一个问题,如何比较textview和edittext,这里是代码Android 在eclipse上比较TextView和EditText,android,string,textview,compare,android-edittext,Android,String,Textview,Compare,Android Edittext,我遇到了一个问题,如何比较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
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();
}
}
}
我已经比较了答案和问题,但它不匹配,每次我比较它总是说错误的答案,如何比较它,任何人都可以解决这个问题??我搞砸了:(您正在将
TextView
对象与EditText
对象进行比较
相反,您需要比较它们的内容
所以
将是
String q = question.getText().toString();
String a = answer.getText().toString();
if(q.equals(a))
您正在将
TextView
对象与EditText
对象进行比较
相反,您需要比较它们的内容
所以
将是
String q = question.getText().toString();
String a = answer.getText().toString();
if(q.equals(a))
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;
}
}
if(question.getText().toString().equals(answer.getText().toString()))
{
// do something
}
else {
//do something
}