I am trying to compare EditText value with TextView , but always getting " Does not match ! "
Is this the wrong way to compare two values ?
For an example : I have stored win in TextView and entering same win in EditText but getting Does Not Match ......
EditText editPassword;
String strPassword;
TextView lblPassword;
String password;
String strMatch;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
editPassword = (EditText) findViewById(R.id.editPassword);
strPassword = editPassword.getText().toString();
// getting intent data
Intent in = getIntent();
password = in.getStringExtra(TAG_PASSWORD);
lblPassword = (TextView) findViewById(R.id.password_label);
lblPassword.setText(password);
strMatch= lblPassword.getText().toString();
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(strPassword.equals(strMatch))
{
Toast.makeText(getApplicationContext(), "Match !",
Toast.LENGTH_LONG).show();
editPassword.setText(null);
}
else
{
Toast.makeText(getApplicationContext(), "Does not match !",
Toast.LENGTH_LONG).show();
}
}
});
}
Add this line in your onClick
method of the Buttton
strPassword = editPassword.getText().toString();
Present your edittext value is empty string means ""
Use
strPassword = editPassword.getText().toString();
line inside of your onClick() event
In your case the edit text value is always to be ""
EditText editPassword;
String strPassword;
TextView lblPassword;
String password;
String strMatch;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
editPassword = (EditText) findViewById(R.id.editPassword);
// getting intent data
Intent in = getIntent();
password = in.getStringExtra(TAG_PASSWORD);
lblPassword = (TextView) findViewById(R.id.password_label);
lblPassword.setText(password);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
strPassword = editPassword.getText().toString();
strMatch= lblPassword.getText().toString();
if(strPassword.equals(strMatch))
{
Toast.makeText(getApplicationContext(), "Match !",
Toast.LENGTH_LONG).show();
editPassword.setText(null);
}
else
{
Toast.makeText(getApplicationContext(), "Does not match !",
Toast.LENGTH_LONG).show();
}
}
});
}
Think about the ORDER of your lines. You first assign ONCE at the beginning something to strPassword and strMatch, and then you change the underlying controls (maybe even by editing something in your GUI), but your onClick() method still compares the values you initially stored in strPassword and strMatch.
Try replacing
if(strPassword.equals(strMatch))
with
if(editPassword.getText().equals(lblPassword.getText()))
(or append toString()
to each, I don't remember if toString()
is needed or not.
尝试以下代码
if(strPassword.trim().equals(strMatch.trim()))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.