![]()
![]()
本人才开始学习Android,并做了一个登陆、注册、记住密码操作的Demo,注册功能用到sqllite 可能对初学Android的有些帮助,
下面是进入Android的主页面 点击登陆后 会提示登陆成功,正在获取用户数据。。。 如果你是记住密码 进入这个程序之后会显示用户名和密码,否者不会自动记住。
就程序默认的就没有用户名密码。这个保存的是最后一次成功登陆的用户名和密码。成功登陆后可以显示数据库中的用户信息,然后点击某一个用户可以显示出密码。
下面我们就看一下代码部分
主页面代码MainActivity
package com.example.logindemo;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class MainActivity extends Activity {
private Button loginbutton;
private Button registerbutton;
private TextView username;
private TextView password;
AutoCompleteTextView cardNumAuto;
CheckBox savePasswordCB;
SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loginbutton = (Button)findViewById(R.id.loginbutton);
registerbutton = (Button)findViewById(R.id.registerbutton);
password = (TextView)findViewById(R.id.password);
username = (TextView)findViewById(R.id.username);
sp = this.getSharedPreferences("userInfo", MainActivity.MODE_WORLD_READABLE);
savePasswordCB = (CheckBox) findViewById(R.id.savePasswordCB);
//判断记住密码多选框的状态
if(sp.getBoolean("ISCHECK", false))
{
//设置默认是记录密码状态
savePasswordCB.setChecked(true);
username.setText(sp.getString("USER_NAME", ""));
password.setText(sp.getString("PASSWORD", ""));
}
loginbutton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
boolean flag = false;
String uname = username.getText().toString();
String pass = password.getText().toString();
DBHelper dbhelper = new DBHelper(MainActivity.this);
Cursor cursor = dbhelper.select();
cursor.moveToFirst();
while(cursor.moveToNext()){
if(cursor.getString(cursor.getColumnIndex("username")).equals(uname)){
if(cursor.getString(cursor.getColumnIndex("password")).equals(pass)){
if(savePasswordCB.isChecked())
{
//记住用户名、密码、
Editor editor = sp.edit();
editor.putString("USER_NAME", uname);
editor.putString("PASSWORD",pass);
editor.commit();
}
flag = true;
Intent intent = new Intent();
intent.setClass(MainActivity.this, UserList.class);
MainActivity.this.startActivity(intent);
}
}
}
if(!flag){
Toast.makeText(getApplicationContext(), "用户名密码不正确", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "登陆成功,正在获取用户数据...", Toast.LENGTH_SHORT).show();
}
}
});
savePasswordCB.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if (savePasswordCB.isChecked()) {
System.out.println("记住密码已选中");
sp.edit().putBoolean("ISCHECK", true).commit();
}else {
System.out.println("记住密码没有选中");
sp.edit().putBoolean("ISCHECK", false).commit();
}
}
});
registerbutton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, RegisterUser.class);
//startActivity(it);
MainActivity.this.startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
然后是RegisterUser 注册页面代码
package com.example.logindemo; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; public class RegisterUser extends Activity { private Button register; private Button fal; private TextView regName; private TextView passw; private TextView passw1; DatabaseHelper mDbHel