2 wuhudamahua wuhudamahua 于 2016.04.19 19:19 提问

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

2个回答

rvncds
rvncds   2016.04.19 19:30

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);
}

rvncds
rvncds 回复wuhudamahua: 你可以通过Log.i("","account:"+account+","+account.length()+",password:"+password+","+password.length());看看你输入的和你比较的是不是一样
一年多之前 回复
wuhudamahua
wuhudamahua 按照你说的改了,还是原来的问题
一年多之前 回复
lxk_1993
lxk_1993   Rxr 2016.04.20 09:01

if(account.trim().equals("admin")&&password.trim().equals("123456"))
换成这样试试

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!