出于某种原因,无论我在两个文本框中输入什么内容,都会得到匹配响应。一开始我在使用==
,但没有用,所以我尝试切换到if( a.equals( b ))
我还是被困住了。
请帮忙!
package net.2TextboxesStringCompare;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.widget.EditText;
/*
* 2TextboxesStringCompare
*/
public class Code8 extends Activity implements OnClickListener {
Button accept;
EditText numberStudents, numberStudents2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
numberStudents = new EditText(this);
numberStudents2 = new EditText(this);
// find our button in the UI by its ID
accept = (Button)findViewById(R.id.accept);
// set listeners for the button. Since our current class
// implements the OnClickListener interface
// we can simply pass the current object ("this") into
// the listener. The appropriate method will therefore
// be called when the event fires.
accept.setOnClickListener(this);
}
/* implement an event handler that responds to click events */
public void onClick(View v){
String a = numberStudents.getText().toString();
String b = numberStudents2.getText().toString();
if( a.equals( b ) )
Toast.makeText(getApplicationContext(), "Matches",
Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), numberStudents.getText()+" !=
"+numberStudents2.getText(), Toast.LENGTH_SHORT).show();
}
}