android输入账号密码按钮变,android登陆时记住密码后下次登陆密码自动键入!涉及下拉输入框, 文件存取,...

[android]代码库package com.Ui;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.content.Intent;

import android.content.SharedPreferences;

import android.database.Cursor;

import android.os.Bundle;

import android.text.Editable;

import android.text.TextWatcher;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.ArrayAdapter;

import android.widget.AutoCompleteTextView;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.EditText;

import android.widget.RelativeLayout;

import android.widget.Toast;

import com.Dao.BaseDao;

import com.Dao.R;

import com.Entity.User;

public class UserLogInfo extends Activity {

private Button btLogin, btAdd;

private EditText password;

private CheckBox check;

private SharedPreferences sp;

private String userNameValue, passwordValue;

private AutoCompleteTextView userName;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.userlogin);

/**

* 不解释

*/

btLogin = (Button) findViewById(R.id.btLogin);

btAdd = (Button) findViewById(R.id.btAdd);

userName = (AutoCompleteTextView) findViewById(R.id.username);

password = (EditText) findViewById(R.id.password);

check = (CheckBox) findViewById(R.id.check);

sp = this.getSharedPreferences("passwordFile", MODE_WORLD_READABLE);

check.setChecked(true);

btLogin.setOnClickListener(new button());

btAdd.setOnClickListener(new button());

/**

* 下拉输入框变更事件

*/

userName.addTextChangedListener(new TextWatcher() {

@Override

public void onTextChanged(CharSequence arg0, int arg1, int arg2,

int arg3) {

// TODO Auto-generated method stub

/**

* 将账号跟密码存入passwordFile文件

*/

String[] allUserName = new String[sp.getAll().size()];

allUserName = sp.getAll().keySet().toArray(new String[0]);

ArrayAdapter adapter = new ArrayAdapter(

UserLogInfo.this, R.layout.test, allUserName);

userName.setAdapter(adapter);

}

@Override

public void beforeTextChanged(CharSequence arg0, int arg1,

int arg2, int arg3) {

// TODO Auto-generated method stub

}

/**

* 帐号变更后自动输入相对应的密码

*/

@Override

public void afterTextChanged(Editable arg0) {

// TODO Auto-generated method stub

password.setText(sp

.getString(userName.getText().toString(), ""));

}

});

}

class button implements OnClickListener {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

userNameValue = userName.getText().toString();

passwordValue = password.getText().toString();

switch (v.getId()) {

/**

* 登陆按钮事件

*

*/

case R.id.btLogin:

Toast.makeText(UserLogInfo.this, "Loading......", 0).show();

BaseDao basedao = new BaseDao(UserLogInfo.this, "test.db");

User user = new User();

user.setUserName(userName.getText().toString());

user.setPassWord(password.getText().toString());

Cursor cursor = basedao.Login(user);

/**

* 验证密码

*/

if (cursor.moveToNext()) {

/**

* 判断记住密码复选框是否开启

*/

if (check.isChecked()) {

sp.edit().putString(userNameValue, passwordValue)

.commit();

}

Intent intent = new Intent(UserLogInfo.this, GetTrain.class);

startActivity(intent);

Toast.makeText(UserLogInfo.this, "登陆成功!", 0).show();

} else {

Toast.makeText(UserLogInfo.this, "密码错误!", 0).show();

}

break;

/**

* 注册按钮点击事件

*/

case R.id.btAdd:

final RelativeLayout layout = (RelativeLayout) getLayoutInflater()

.inflate(R.layout.adduser, null);

new AlertDialog.Builder(UserLogInfo.this)

.setTitle("用户注册")

.setView(layout)

.setPositiveButton("提交",

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,

int which) {

// TODO Auto-generated method stub

EditText username = (EditText) layout

.findViewById(R.id.username);

EditText password = (EditText) layout

.findViewById(R.id.password);

EditText passagain = (EditText) layout

.findViewById(R.id.passagain);

if (password

.getText()

.toString()

.equals(passagain.getText()

.toString())) {

BaseDao basedao = new BaseDao(

UserLogInfo.this, "test.db");

User user = new User();

user.setUserName(username.getText()

.toString());

user.setPassWord(password.getText()

.toString());

basedao.addUser(user);

Toast.makeText(UserLogInfo.this,

"注册成功!", 0).show();

} else {

Toast.makeText(UserLogInfo.this,

"两次密码输入不一致!", 0).show();

}

}

}).setNegativeButton("取消", null).show();

break;

default:

break;

}

}

}

}

[代码运行效果截图]

619bf9b1d3cb67f6a5910ee05ecdca9a.png

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值