我正在尝试将EditText值与TextView 进行比较,但总是得到“不匹配!”
这是比较两个值的错误方法吗?
例如:我已在 TextView 中存储 win ,并在 EditText 中输入相同的 win ,但获取< strong> 不匹配 ......
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();
}
}
});
}
答案 0 :(得分:0)
在onClick
Buttton
方法中添加此行
strPassword = editPassword.getText().toString();
显示您的edittext值为空字符串表示""
答案 1 :(得分:0)
使用
strPassword = editPassword.getText().toString();
在onClick()事件中的行
In your case the edit text value is always to be ""
答案 2 :(得分:0)
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();
}
}
});
}
答案 3 :(得分:0)
想想你的线的顺序。首先将ONCE分配给strPassword和strMatch,然后更改底层控件(甚至可以通过编辑GUI中的内容),但是onClick()方法仍会比较最初存储在strPassword和strMatch中的值。 / p>
尝试替换
if(strPassword.equals(strMatch))
与
if(editPassword.getText().equals(lblPassword.getText()))
(或将toString()
附加到每个,我不记得是否需要toString()
。
答案 4 :(得分:0)
尝试以下代码
if(strPassword.trim().equals(strMatch.trim()))