[英]Android EditText Value Error?
我正在初學者上測試android上的一些東西,並試圖抓住單擊按鈕時在EditText中輸入的值,然后將其與我在類內定義的字符串值進行比較,然后使用if(EditText == stringDefined )else(),但是即使輸入正確的文本,我的代碼也會始終跳轉到else部分,請多多幫助。 這是代碼:
Button mButton;
EditText mEdit1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final String user = "admin";
mButton = (Button)findViewById(R.id.BTN_login);
mEdit1 = (EditText)findViewById(R.id.editText1);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
String userEntered = mEdit1.getText().toString().trim();
Intent intent = new Intent(Login.this, Success.class);
if(userEntered == user){
startActivity(intent);
}
else{
AlertDialog.Builder errAlert = new AlertDialog.Builder(Login.this);
errAlert.setTitle("Wrong Credentials");
errAlert.setMessage("Wrong username");
errAlert.setCancelable(true);
errAlert.show();
}
}
});
}
使用equals方法:
if(userEntered.equals(user)){
startActivity(intent);
}
==
用於比較原始類型,而java.lang.String
類的equals()
方法比較內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.