[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<String> adapter = new ArrayAdapter<String>(
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;
}
}
}
}
[代码运行效果截图]