该程序主要是SharedPreferences的用法:
SharedPreferences是Android平台上一个轻量级的存储类,主要是保存一些常用的配置参数,它是采用xml文件存放数据的,文件存放在"/data/data<package name>/shared_prefs"目录下。SharedPreferences是一个接口,而且在这个接口里没有提供写入数据和读取数据的能力。但它是通过其Editor接口中的一些方法来操作SharedPreference的。
调用Context.getSharePreferences(String name, int mode)方法来得到SharePreferences接口,该方法的第一个参数是文件名称,第二个参数是操作模式。
操作模式有三种:MODE_PRIVATE(私有) ,MODE_WORLD_READABLE(可读),MODE_WORLD_WRITEABLE(可写)
SharePreference提供了获得数据的方法,如getString(String key,String defValue)等
调用SharePreferences的edit()方法返回 SharePreferences.Editor内部接口,该接口提供了保存数据的方法如: putString(String key,String value)等,调用该接口的commit()方法可以将数据保存。
包括的类如下图:
package en.edu.bzu.aminitwitter;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText userName,inputpw;
private Button login,back,register;
private CheckBox ckBox1,ckBox2;
private ProgressDialog mDialog;
//private SharedPreferences sp;
final SharedPreferences pre=getSharedPreferences("longinvalue", MODE_WORLD_WRITEABLE);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//去除标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
//在R.java里找组件:
userName=(EditText) findViewById(R.id.userName);
inputpw=(EditText) findViewById(R.id.inputpw);
login=(Button) findViewById(R.id.login);
back=(Button) findViewById(R.id.back);
register=(Button) findViewById(R.id.register);
ckBox1=(CheckBox) findViewById(R.id.ckBox1);
ckBox2=(CheckBox) findViewById(R.id.ckBox2);
if(pre!=null){
//记住了密码
if(pre.getBoolean("isrmb", false)==true){
userName.setText(pre.getString("name", null));
inputpw.setText(pre.getString("pass", null));
ckBox2.setChecked(true) ;
}
if(pre.getBoolean("islgs", false)==true){
ckBox1.setChecked(true);
creatDialog();
new Thread(){
public void run() {
try {
Thread.sleep(3000);
if(mDialog.isShowing()){
mDialog.dismiss();
}
Intent intent2=new Intent(MainActivity.this,LoginOnActivity.class);
startActivity(intent2);
} catch (Exception e) {
// TODO: handle exception
}
}
}.start();
}
}
ckBox1.setOnCheckedChangeListener( new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(ckBox1.isChecked()==false){
ckBox2.setChecked(false);
}
}
});
ckBox2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
ckBox1.setChecked(true);
}
});
}
//密码输入
public void Login(View v){
//String name=userName.getText().toString();
//String pw=inputpw.getText().toString();
//用户名和密码不能为空
/*if(TextUtils.isEmpty(name)||TextUtils.isEmpty(pw)||pw.length()<5){
Toast.makeText(MainActivity.this, "用户名或密码不能为空且密码长度不能少于五!", Toast.LENGTH_LONG).show();
}else{
if("xuexue".equals(userName.getText().toString()) && "123456".equals(inputpw.getText().toString()) ){
Editor editor=sp.edit(); //得到editor对象
if(ckBox1.isChecked()){
editor.putBoolean("rememberpw", true);
editor.putString("name", name);//记录用户名
editor.putString("pw", inputpw.getText().toString());//记住密码
editor.commit();//提交数据
}else{
editor.clear();
}
//editor.commit();//提交数据
Intent intent=new Intent(MainActivity.this,LoginOnActivity.class);
startActivity(intent);
}else{
Toast.makeText(getApplicationContext(),"用户名或密码错误,请重新登录!", Toast.LENGTH_LONG).show();
}
}*/
if(!userName.getText().toString().equals("")&&!inputpw.getText().toString().equals("")){
if(ckBox2.isChecked()){
pre.edit().putBoolean("isrmb", true).putBoolean("islgs", true).putString("name", userName.getText().toString())
.putString("pass", inputpw.getText().toString()).commit();
}else{
if(ckBox2.isChecked()){
pre.edit().putBoolean("isrmb", true).putBoolean("islgs", false).putString("name", userName.getText().toString())
.putString("pass", inputpw.getText().toString()).commit();
}else{
pre.edit().putBoolean("isrmb", false).putBoolean("islgs", false).putString("name",userName.getText().toString())
.putString("pass", inputpw.getText().toString()).commit();
}
}
Intent intent=new Intent(MainActivity.this,LoginOnActivity.class);
startActivity(intent);
}else{
Toast.makeText(getApplicationContext(), "密码或账号不能为空!", Toast.LENGTH_LONG).show();
}
}
private void creatDialog() {
// TODO Auto-generated method stub
mDialog=new ProgressDialog(this);
mDialog.setTitle("验证中");
mDialog.setMessage("正在登陆请稍后");
mDialog.setIndeterminate(true);
mDialog.setCancelable(true);
mDialog.show();
}
//退出
public void Back(View v){
AlertDialog.Builder b=new Builder(MainActivity.this);
b.setMessage("确认退出吗?");
b.setTitle("提示");
b.setPositiveButton("确认", new DialogInterface. OnClickListener() {
public void onClick (DialogInterface dialog,int which) {
// TODO Auto-generated method stub
//点击确定,对话框消失,程序关闭
dialog.dismiss();
MainActivity.this.finish();
}
});
b.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//点击取消,对话框消失
dialog.dismiss();
}
});
//创建对话框并提示
b.create().show();
}
//注册新用户
public void Register(View v){
Intent intent=new Intent();
intent.setClass(MainActivity.this, RegisterActivity.class);
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;
}
}
ur界面的设置