代码如下:
package home.lee.sharedpreferencesdemo1;
import home.lee.sharedpreferencesdemo1q.R;
import java.sql.Timestamp;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
/**
*
* @author Goodboy.Lee
* @object 利用单一数据库(SharedPreferences),,实现一种简单的签到功能系统
* @website http://webapplee.sinaapp.com
* @msn leegf17@hotmail.com
* @qq 1738009713
* @version 1.0.21
* @category AlertDialog, SharedPreferences, LaytouInflater, Timestamp, and so on.
* @since 2013.5.11
*
*/
public class MainActivity extends Activity {
protected static final String NAME_NULL = "用户名不能空,请填写";
protected static final String PASS_NULL = "密码为空,请填好";
protected static final String WORKID_NULL = "社号为空,请确保填写无误";
protected static final String TIME_NULL = "签到时间为空";
protected static final String UNCHECKED = "最后一项 签到为空,请打钩";
protected static final String SIGN_NULL = "请确保签名无误";
private Button confirm = null;
protected boolean CHECK = false;
private EditText password = null;
private EditText username = null;
private CheckBox check = null;
private EditText time = null;
private EditText workId = null;
private Button timeButton = null;
protected long when;
private TextView dusername;
private TextView dpassword;
private TextView dworkId;
private TextView dtime;
private View layout;
private TextView dchecked;
private EditText sign;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.username);//String
password = (EditText)findViewById(R.id.password);//String
sign = (EditText)findViewById(R.id.sign);//签名图片保存
check = (CheckBox)findViewById(R.id.check);//boolean
workId = (EditText)findViewById(R.id.workId);//long
time = (EditText)findViewById(R.id.arriveTime);//String
timeButton = (Button)findViewById(R.id.timeButton);
timeButton.setOnClickListener(new OnClickListener(){
private Timestamp nowTime;
@Override
public void onClick(View v) {
when = System.currentTimeMillis(); //long
nowTime = new Timestamp(when);//对象
System.out.println("Register time is " + nowTime);
time.setText("" + nowTime);
}
});
//initView();
confirm = (Button)findViewById(R.id.confirm);
confirm.setOnClickListener(new OnClickListener(){
private String getName;
private String getPassword;
private String getWorkId;
private boolean ifChecked;
private String getTime;
private String getChecked;
private String checkedString;
private String getSign;
@Override
public void onClick(View v) {
// dialog = new Dialog(MainActivity.this);
// dialog.setContentView(R.layout.my_info);
// dialog.setTitle("个人信息核对");
// dusername = (TextView)dialog.findViewById(R.id.displayusername);//String
// dpassword = (TextView)dialog.findViewById(R.id.displaypassword);//String
// dworkId = (TextView)dialog.findViewById(R.id.displayworkid);//long
// dtime = (TextView)dialog.findViewById(R.id.displaytime);//String
// dialog.show();
LayoutInflater myInfoInflater = getLayoutInflater();
ViewGroup myinfoLayout = (ViewGroup)findViewById(R.id.myinfolayout);
layout = myInfoInflater.inflate(R.layout.my_info, myinfoLayout);
//记得要使用加载布局layout中调用相应控件,即findViewById前加上layout对象
dusername = (TextView) layout.findViewById(R.id.displayusername);
dpassword = (TextView) layout.findViewById(R.id.displaypassword);
dworkId = (TextView) layout.findViewById(R.id.displayworkid);
dtime = (TextView) layout.findViewById(R.id.displaytime);
dchecked = (TextView) layout.findViewById(R.id.displaychecked);
getName = username.getText().toString();
getSign = sign.getText().toString();
getPassword = password.getText().toString();
getWorkId = workId.getText().toString();
if(check.isChecked()){
ifChecked = true;
checkedString = "已点到";
}else{
ifChecked = false;
checkedString = "未点到";
}
getChecked = checkedString;
getTime = time.getText().toString();
if(getName.equals("")){
infoIntegrity(NAME_NULL);
return;
}else if(getSign.equals("")){
infoIntegrity(SIGN_NULL);
return;
}else if(getWorkId.equals("")){
infoIntegrity(WORKID_NULL);
return;
}else if(getPassword.equals("")){
infoIntegrity(PASS_NULL);
return;
}else if(getTime.equals("")){
infoIntegrity(TIME_NULL);
return;
}else if(ifChecked == false){
infoIntegrity(UNCHECKED);
return;
}
dusername.setText(getName);
dpassword.setText(getPassword);
dworkId.setText(getWorkId);
dtime.setText(getTime);
dchecked.setText(getChecked);
AlertDialog.Builder submit = new AlertDialog.Builder(MainActivity.this);
submit.setTitle("个人信息核对").setView(layout).setCancelable(false)
.setPositiveButton("无误", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SharedPreferences userInfo = getSharedPreferences("user_info", Activity.MODE_PRIVATE);//文件为user_info.xml
//编辑,提取,字符串形式提交-保存int/float/String/long/boolean数据
userInfo.edit().putString("name", getName).commit();
userInfo.edit().putString("pass", getPassword).commit();
userInfo.edit().putBoolean("checked", ifChecked).commit();
userInfo.edit().putString("workId", getWorkId).commit();
userInfo.edit().putString("arriveTime", getTime).commit();
AlertDialog.Builder ad = new AlertDialog.Builder(MainActivity.this);
ad.setCancelable(false).setMessage("保存成功").setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
username.setText(null);
password.setText(null);
workId.setText(null);
time.setText(null);
check.setChecked(false);
sign.setText(null);
}
}).setCancelable(false).create().show();
}
}).setNegativeButton("有误", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AlertDialog.Builder bd = new AlertDialog.Builder(MainActivity.this);
bd.setCancelable(false).setTitle("报错").setMessage("请重新输入并提交").setNegativeButton("确定", null).setCancelable(false).create().show();
}
}).show();
}
private void infoIntegrity(String str) {
new AlertDialog.Builder(MainActivity.this).setTitle("报错")
.setMessage(str).setPositiveButton("确定", null).show();
return;
}
});
}
//初始化过程-即重启应用时,显示user_info.xml文件中的内容
private void initView() {
SharedPreferences userInfo = getSharedPreferences("user_info", Activity.MODE_PRIVATE);
String user = userInfo.getString("name", "");//getXXX的第2个参数是value的默认值
String pass = userInfo.getString("pass", "");
boolean checked = userInfo.getBoolean("checked", false);
String workid = userInfo.getString("workId", "");
String timenow = userInfo.getString("arriveTime", "");
username.setText(user);
password.setText(pass);
check.setChecked(checked);
workId.setText(workid);
time.setText(timenow);
}
}
下载链接:单一数据库签到系统