问题描述
- Android应用设置密码登录错误
-
package com.example.broadbestpractice;import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;public class LoginActivity extends BaseActivity{
private EditText accountEdit; private EditText passwordEdit; private Button login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.id.login); accountEdit=(EditText) findViewById(R.id.account); passwordEdit=(EditText) findViewById(R.id.password); login=(Button) findViewById(R.id.login); login.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String account=accountEdit.getText().toString(); String password=passwordEdit.getText().toString(); if(account.equals("admin")&&password.equals("123456")) { Intent intent=new Intent(LoginActivity.this,MainActivity.class); startActivity(intent); finish(); } else { Toast.makeText(LoginActivity.this, "error", 1).show(); } } }); }
}
我输入正确的账号和密码总是弹出taost说error
解决方案
String account=accountEdit.getText().toString();
String password=passwordEdit.getText().toString();
if(account != null){
account = account.trim();
}
if(password != null ){
password = password.trim();
}
if(account.equals("admin")&&password.equals("123456"))
{
Intent intent=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
finish();
}else{
Toast.makeText(LoginActivity.this, "error", 1).show();
Log.i("","account:"+account+",password:"+password);
}
解决方案二:
if(account.trim().equals("admin")&&password.trim().equals("123456"))
换成这样试试