热门标签 | HotTags
当前位置:  开发笔记 > 数据库 > 正文

基于SQLite的Android登录APP

这篇文章主要为大家详细介绍了基于SQLite的Android登录APP,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

基于SQLite的Android登录APP

该登录APP主要包括三个模块:

1、登录:用户选择登录方式、登录身份,输入账号密码,完成登录。
2、忘记密码:用户输入新密码及验证码修改登录密码。
3、个人信息:用户完成登录后设置个人信息并显示。

使用控件:

1、单选按钮RadioButton:区分是密码登录还是验证码登录。
2、下拉框Spinner:区分是个人用户还是公司用户。
3、编辑框EditText:输入手机号和密码(或验证码)。
4、复选框CheckBox:判断是否记住密码。
5、相对布局RelativeLayout:界面的整体布局,方便将各个控件按照相对位置摆放。
6、框架布局FrameLayout:在框架布局中后面添加的子视图会把之前的子视图覆盖掉,一般用于需要重叠显示的场合。用于实现忘记密码按钮和密码输入框的叠加。

采用的存储方式

1、共享参数SharedPreferences:

是Android的一个轻量级存储工具,采用的存储结构是Key-Value的键值对方式,类似于Java的Properties类,都是把Key-Value的键值对保存在配置文件中,不同的是Properties的文件内容是Key=Value的形式,而SharedPreferences的存储介质是符合XML规范的配置文件。本案例中用于保存用户的账号和密码。

2、数据库SQLite:

是一个小巧的嵌入式数据库。本案例中用于存储用户的个人信息。

成果展示:

界面设计:

1. 登录界面

<&#63;xml version="1.0" encoding="utf-8"&#63;>


 

 
 

 
 
 

 
 

 
 

 
 

2.忘记密码界面

<&#63;xml version="1.0" encoding="utf-8"&#63;>

 
 
 
 
 
 

 

3.个人信息填写界面

<&#63;xml version="1.0" encoding="utf-8"&#63;>

 
 

 
 
 
 
 
 
 
 

 

4.个人信息显示界面

<&#63;xml version="1.0" encoding="utf-8"&#63;>


 
 

代码实现

UserDBHelper

package com.example.helloworld;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


import androidx.annotation.Nullable;
import androidx.core.app.NavUtils;

import java.util.ArrayList;
import java.util.Locale;

public class UserDBHelper extends SQLiteOpenHelper {

 private static final String TAG = "UserDBHelper";
 private static final String DB_NAME = "user.db"; //数据库名
 private static final int DB_VERSION = 1;  //数据库版本
 private static UserDBHelper mHelper = null;
 private SQLiteDatabase mDB = null;
 private static final String TABLE_NAME = "user_info"; //表名

 private UserDBHelper(Context context){
 super(context,DB_NAME,null,DB_VERSION);

 }

 private UserDBHelper(Context context,int version){
 super(context,DB_NAME,null,version);
 }

 public static UserDBHelper getInstance(Context context,int version){
 if(version > 0 && mHelper == null){
  mHelper = new UserDBHelper(context,version);
 }else if(mHelper == null){
  mHelper = new UserDBHelper(context);
 }
 return mHelper;
 }

 public SQLiteDatabase openReadLink(){
 if (mDB == null || !mDB.isOpen()){
  mDB = mHelper.getReadableDatabase();
 }
 return mDB;
 }

 public SQLiteDatabase openWriteLink(){
 if (mDB == null || !mDB.isOpen()){
  mDB = mHelper.getWritableDatabase();
  Log.d(TAG, "openWriteLink: 打开了读数据库");
 }
 return mDB;
 }

 public void closeLink(){
 if (mDB != null && mDB.isOpen()){
  mDB.close();
  mDB = null;
 }
 }

 public String getDBName(){
 if(mHelper != null){
  return mHelper.getDatabaseName();
 }else {
  return DB_NAME;
 }
 }

 @Override
 public void onCreate(SQLiteDatabase db) {
 Log.d(TAG, "onCreate: 创建数据库");
 String drop_sql = "DROP TABLE IF EXISTS " + TABLE_NAME + ";";
 db.execSQL(drop_sql);
 String create_sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ("
  + "_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
  + "name VARCHAR NOT NULL,"
  + "age INTEGER NOT NULL,"
  + "height LONG NOT NULL,"
  + "weight FLOAT NOT NULL,"
  + "married INTEGER NOT NULL,"
  + "update_time VARCHAR NOT NULL,"
  + "phone VARCHAR NOT NULL,"
  + "password VARCHAR NOT NULL"
  + ");";
 Log.d(TAG, "create_sql" + create_sql);
 db.execSQL(create_sql);
 }

 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 Log.d(TAG, "onUpgrade oldVersion=" +oldVersion+",newVersion=" + newVersion+"数据库新旧版本号");
 if (newVersion > 1){
  String alter_sql = "ALTER TABLE" + TABLE_NAME + "ADD COLUMN" + "phone VARCHAR;";
  Log.d(TAG, "alter_sql:" + alter_sql);
  db.execSQL(alter_sql);
  alter_sql = "ALTER TABLE" + TABLE_NAME + "ADD COLUMN" + "password VARCHAR;";
  Log.d(TAG, "alter_sql:" + alter_sql);
  db.execSQL(alter_sql);
 }
 }

 public int delete(String condition){
 int count = mDB.delete(TABLE_NAME,condition,null);
 return count;
 }
 public int deleteAll(){
 int count = mDB.delete(TABLE_NAME,"1=1",null);
 return count;
 }

 public long insert(UserInfo info){
 ArrayList infoArray = new ArrayList();
 infoArray.add(info);
 return insert(infoArray);
 }

 public ArrayListquery(String condition) {
 String sql = String.format(Locale.CHINA,"select rowid,_id,name,age,height,weight,married,update_time," + "phone,password from %s where %s;", TABLE_NAME,condition);
 Log.d(TAG, "query sql: " + sql);
 ArrayList infoArray = new ArrayList();
 Cursor cursor = mDB.rawQuery(sql, null);
 while (cursor.moveToNext()) {
  UserInfo info = new UserInfo();
  info.rowid = cursor.getLong(0);
  info.xuhao = cursor.getInt(1);
  info.name = cursor.getString(2);
  info.age = cursor.getInt(3);
  info.height = cursor.getLong(4);
  info.weight = cursor.getFloat(5);
  info.married = (cursor.getInt(6) == 0) &#63; false : true;
  info.update_time = cursor.getString(7);
  info.phOne= cursor.getString(8);
  info.password = cursor.getString(9);
  infoArray.add(info);
 }
 cursor.close();
 return infoArray;
 }

 public long insert(ArrayList infoArray) {
 long result = -1;
 for (int i = 0; i  tempArray = new ArrayList();
  
  if (info.name != null && info.name.length() > 0) {
  String cOndition= String.format("name='%s'", info.name);
  tempArray = query(condition);
  if (tempArray.size() > 0) {
   update(info, condition);
   result = tempArray.get(0).rowid;
   continue;
  }
  }
  
  if (info.phone != null && info.phone.length() > 0) {
  String cOndition= String.format("phOne='%s'", info.phone);
  tempArray = query(condition);
  if (tempArray.size() > 0) {
   update(info, condition);
   result = tempArray.get(0).rowid;
   continue;
  }
  }
  Log.d(TAG, "insert: 当前版本号"+mDB.getVersion());

  ContentValues cv = new ContentValues();
  cv.put("name", info.name);
  cv.put("age", info.age);
  cv.put("height", info.height);
  cv.put("weight", info.weight);
  cv.put("married", info.married);
  cv.put("update_time", info.update_time);
  cv.put("phone", info.phone);
  cv.put("password", info.password);
  result = mDB.insert(TABLE_NAME, "", cv);
  if (result == -1) {
  return result;
  }
 }
 return result;
 }

 public int update(UserInfo info, String condition) {
 ContentValues cv = new ContentValues();
 cv.put("name", info.name);
 cv.put("age", info.age);
 cv.put("height", info.height);
 cv.put("weight", info.weight);
 cv.put("married", info.married);
 cv.put("update_time", info.update_time);
 cv.put("phone", info.phone);
 cv.put("password", info.password);
 // 执行更新记录动作,该语句返回记录更新的数目
 return mDB.update(TABLE_NAME, cv, condition, null);
 }


 public int update(UserInfo info) {
 return update(info, "rowid=" + info.rowid);
 }


 public UserInfo queryByPhone(String phone){
 UserInfo info = null;
 ArrayList infoArray = query(String.format("phOne=%s",phone));
 if (infoArray.size() > 0 ){
  info = infoArray.get(0);
 }
 return info;
 }
}

UserInfo

package com.example.helloworld;

public class UserInfo {
 public long rowid;
 public int xuhao;
 public String name;
 public int age;
 public long height;
 public float weight;
 public boolean married;
 public String update_time;
 public String phone;
 public String password;

 public UserInfo() {
 rowid = 0l;
 xuhao = 0;
 name = "";
 age = 0;
 height = 0l;
 weight = 0.0f;
 married = false;
 update_time = "";
 phOne= "";
 password = "";
 }
}

LoginActivity

package com.example.helloworld;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Rect;
import android.os.Bundle;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import java.net.PasswordAuthentication;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
 private EditText et_phone;
 private RadioButton rb_psw;
 private RadioButton rb_checkcode;
 private EditText et_psw;
 private Button btn_pswforget;
 private String mPassword;
 private int mRequestcode; 
 private String mCheckCode;
 private int mType;
 private String TAG = "huahua";


 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_login);
 rb_psw = findViewById(R.id.rb_password);
 rb_checkcode = findViewById(R.id.rb_checkcode);
 et_phOne= findViewById(R.id.et_phone);
 et_psw = findViewById(R.id.et_psw);
 CheckBox cb_pswforget = findViewById(R.id.cb_pswrmb);
 Button btn_login = findViewById(R.id.btn_login);
 btn_pswforget = findViewById(R.id.btn_pswforget);
 btn_login.setOnClickListener(this);
 btn_pswforget.setOnClickListener(this);
 RadioGroup rg = findViewById(R.id.rg_login_way);
 mPassword = et_psw.getText().toString();

 rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  @Override
  public void onCheckedChanged(RadioGroup group, int checkedId) {
  RadioButton rb = findViewById(checkedId);
  Log.i(TAG, "onCheckedChanged: 密码登录"+ rb_psw.isChecked());
  Log.i(TAG, "onCheckedChanged: 验证码登录"+rb_checkcode.isChecked());
  if(rb_psw.isChecked()){
   et_psw.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
  }
  }
 });

 cb_pswforget.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  @Override
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

  }
 });

 ArrayAdapter typeAdapter = new ArrayAdapter(this,R.layout.item_dropdown,typeArray);
 typeAdapter.setDropDownViewResource(R.layout.item_dropdown);
 Spinner sp_type = findViewById(R.id.sp_shenfen);
 sp_type.setAdapter(typeAdapter);
 sp_type.setSelection(0);
 sp_type.setPrompt("选择你的登录身份:");
 sp_type.setOnItemSelectedListener(new MyOnItemSeclectedListener());
 }
 private String[] typeArray = {"个人用户","企业用户"};
 private class MyOnItemSeclectedListener implements AdapterView.OnItemSelectedListener{
 @Override
 public void onItemSelected(AdapterView<&#63;> parent, View view, int position, long id) {
  mType = position;
 }

 @Override
 public void onNothingSelected(AdapterView<&#63;> parent) {

 }
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 if (requestCode == mRequestcode && data != null) {
  mPassword = data.getStringExtra("newpsw");
 }
 }

 @Override
 protected void onRestart() {
 et_psw.setText("");
 super.onRestart();
 }

 @Override
 public void onClick(View v) {
 String phOne= et_phone.getText().toString();
 switch (v.getId()){
  case R.id.btn_pswforget: {
  Log.i(TAG, "onClick: 点击了忘记密码");
  if (phOne== null || phone.length() <11) {
   Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
   return;
  }
  if (rb_psw.isChecked()) {
   Log.i(TAG, "onClick: 进入忘记密码界面");
   Intent intent = new Intent(this, PswForgetActivity.class);
   intent.putExtra("phone", phone);
   startActivityForResult(intent, mRequestcode);
  } else if (rb_checkcode.isChecked()) {
   mCheckCode = String.format("%06d", (int) (Math.random() * 1000000 % 1000000));
   Log.i(TAG, "onClick: 发送验证码");
   AlertDialog.Builder builder = new AlertDialog.Builder(this);
   builder.setTitle("请记住验证码!");
   builder.setMessage("手机号" + phone + ",本次验证码是:" + mCheckCode + ",请输入验证码");
   builder.setPositiveButton("确定", null);
   AlertDialog alert = builder.create();
   alert.show();
  }
  }
  break;
  case R.id.btn_login: {
  if (phOne== null || phone.length() <11) {
   Toast.makeText(this, "请输入正确的手机号!", Toast.LENGTH_SHORT).show();
   Log.i(TAG, "onClick: 验证密码");
   return;
  }
  if (rb_psw.isChecked()) {
   if (!et_psw.getText().toString().equals(mPassword) || et_psw.getText().toString().equals("")) {
   Toast.makeText(this, "请输入正确的密码", Toast.LENGTH_SHORT).show();
   return;
   } else {
   loginSuccess();
   }
  } else if (rb_checkcode.isChecked()) {
   if (!et_psw.getText().toString().equals(mCheckCode)) {
   Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();
   return;
   } else {
   loginSuccess();
   }
  }
  }
 }
 }
 private void loginSuccess(){
 String desc = String.format("您的手机号码是%s,类型是%s。恭喜你通过登录验证,点击“确定”按钮返回上个页面",et_phone.getText().toString(),typeArray[mType]);
 AlertDialog.Builder builder = new AlertDialog.Builder(this);
 builder.setTitle("登录成功");
 builder.setMessage(desc);
 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  @Override
  public void onClick(DialogInterface dialog, int which) {
  Intent intent = new Intent(LoginActivity.this,SharedPreferencesActivity.class);
  startActivity(intent);
  SharedPreferences sps = getSharedPreferences("Login", Context.MODE_PRIVATE);
  SharedPreferences.Editor editor = sps.edit();
  editor.putString("phone",et_phone.getText().toString());
  editor.putString("password",et_psw.getText().toString());
  editor.apply();
  }
 });
 builder.setNegativeButton("取消",null);
 AlertDialog alert = builder.create();
 alert.show();
 }
}

PswForgetActivity

package com.example.helloworld;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Toast;

public class PswForgetActivity extends AppCompatActivity implements View.OnClickListener {
 private EditText et_newpsw;
 private EditText et_chknewpsw;
 private EditText et_checkcode;
 private String mCheckCode;
 private String mPhone;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_psw_forget);
 et_newpsw = findViewById(R.id.et_newpsw);
 et_chknewpsw = findViewById(R.id.et_chknewpsw);
 et_checkcode = findViewById(R.id.et_checkcode);
 findViewById(R.id.btn_sendcheckcode).setOnClickListener(this);
 findViewById(R.id.btn_check).setOnClickListener(this);
 mPhOne= getIntent().getStringExtra("phone");
 et_newpsw.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
 et_chknewpsw.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
 }

 @Override
 public void onClick(View v) {
 switch (v.getId()){
  case R.id.btn_sendcheckcode:
  if(mPhOne== null || mPhone.length() <11){
   Toast.makeText(this,"请输入正确的手机号",Toast.LENGTH_SHORT).show();
   return;
  }
  mCheckCode = String.format("%06d",(int)(Math.random()*1000000%1000000));
  AlertDialog.Builder builder = new AlertDialog.Builder(this);
  builder.setTitle("请记住验证码");
  builder.setMessage("手机号"+mPhone+",本次验证码是"+mCheckCode+",请输入验证码");
  builder.setPositiveButton("确定",null);
  AlertDialog alertDialog = builder.create();
  alertDialog.show();
  case R.id.btn_check:
  String newpsw = et_newpsw.getText().toString();
  String chknewpsw = et_chknewpsw.getText().toString();
  if(newpsw == null || newpsw.length() <6 || chknewpsw == null || chknewpsw.length() <6){
   Toast.makeText(this,"请输入正确的新密码",Toast.LENGTH_SHORT).show();
   return;
  }else if(!newpsw.equals(chknewpsw)){
   Toast.makeText(this,"两次输入的新密码不一致",Toast.LENGTH_SHORT).show();
   return;
  }else if(!et_checkcode.getText().toString().equals(mCheckCode)){
   Toast.makeText(this,"请输入正确的验证码",Toast.LENGTH_SHORT).show();
   return;
  }else {
   Toast.makeText(this,"密码修改成功",Toast.LENGTH_SHORT).show();
   Intent intent = new Intent();
   intent.putExtra("newpsw",newpsw);
   setResult(Activity.RESULT_OK,intent);
   finish();
  }
 }
 }
}

InfoWriteActivity

package com.example.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class InfoWriteActivity extends AppCompatActivity implements View.OnClickListener {
 private static final String TAG = "huahua";
 private UserDBHelper mHelper;
 private EditText et_name;
 private EditText et_age;
 private EditText et_height;
 private EditText et_weight;
 private boolean Married = false;
 private String phone;
 private String password;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_shared_preferences);
 et_name = findViewById(R.id.et_name);
 et_age = findViewById(R.id.et_age);
 et_height = findViewById(R.id.et_height);
 et_weight = findViewById(R.id.et_weight);
 findViewById(R.id.btn_save).setOnClickListener(this);

 SharedPreferences sps = getSharedPreferences("Login",Context.MODE_PRIVATE);
 SharedPreferences.Editor editor = sps.edit();
 phOne= sps.getString("phone","");
 password = sps.getString("password","");


 ArrayAdapter typeAdapter = new ArrayAdapter(this, R.layout.item_dropdown, typeArray);
 typeAdapter.setDropDownViewResource(R.layout.item_dropdown);
 Spinner sp_married = findViewById(R.id.sp_married);
 sp_married.setAdapter(typeAdapter);
 sp_married.setPrompt("请选择婚姻状况");
 sp_married.setSelection(0);
 sp_married.setOnItemSelectedListener(new TypeSelectedListener());

 


 }

 private String[] typeArray = {"未婚", "已婚"};

 class TypeSelectedListener implements AdapterView.OnItemSelectedListener {
 public void onItemSelected(AdapterView<&#63;> arg0, View arg1, int arg2, long arg3) {
  Married = (arg2 == 0) &#63; false : true;
 }

 public void onNothingSelected(AdapterView<&#63;> arg0) {
 }
 }

 @Override
 protected void onStart() {
 super.onStart();
 SQLiteDatabase mDB = getApplicationContext().openOrCreateDatabase("user.db", Context.MODE_PRIVATE, null);
 mHelper = UserDBHelper.getInstance(this, 1);
 mHelper.openWriteLink();
 }

 @Override
 protected void onStop() {
 super.onStop();
 mHelper.closeLink();
 }

 @Override
 public void onClick(View v) {
 if (v.getId() == R.id.btn_save) {
  String name = et_name.getText().toString();
  String age = et_age.getText().toString();
  String height = et_height.getText().toString();
  String weight = et_weight.getText().toString();
  if (name == null || name.length() <= 0) {
  showToast("请先填写姓名");
  return;
  }
  if (age == null || age.length() <= 0) {
  showToast("请先填写年龄");
  return;
  }
  if (height == null || height.length() <= 0) {
  showToast("请先填写身高");
  return;
  }
  if (weight == null || weight.length() <= 0) {
  showToast("请先填写体重");
  return;
  }

  UserInfo info = new UserInfo();

  info.name = name;
  info.age = Integer.parseInt(age);
  info.height = Long.parseLong(height);
  info.weight = Float.parseFloat(weight);
  info.married = Married;
  info.phOne= phone;
  info.password = password;

  //info.update_time = DateUtil.getCurDateStr("yyyy-MM-dd HH:mm:ss");
  info.update_time = DateUtil.getNowDate(DateUtil.DatePattern.ALL_TIME);
  Log.d(TAG, "onClick: 手机号" + info.phone+info.password+info.name+info.update_time+info.married);
  mHelper.insert(info);
  Intent intent = new Intent(InfoWriteActivity.this, InfoReadActivity.class);
  startActivity(intent);
  showToast("数据已写入SQLite数据库");
 }
 }

 private void showToast(String desc) {
 Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
 }

 public static void startHome(Context mContext) {
 Intent intent = new Intent(mContext, InfoWriteActivity.class);
 mContext.startActivity(intent);
 }
}

InfoReadActivity

package com.example.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class InfoWriteActivity extends AppCompatActivity implements View.OnClickListener {
 private static final String TAG = "huahua";
 private UserDBHelper mHelper;
 private EditText et_name;
 private EditText et_age;
 private EditText et_height;
 private EditText et_weight;
 private boolean Married = false;
 private String phone;
 private String password;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_shared_preferences);
 et_name = findViewById(R.id.et_name);
 et_age = findViewById(R.id.et_age);
 et_height = findViewById(R.id.et_height);
 et_weight = findViewById(R.id.et_weight);
 findViewById(R.id.btn_save).setOnClickListener(this);

 SharedPreferences sps = getSharedPreferences("Login",Context.MODE_PRIVATE);
 SharedPreferences.Editor editor = sps.edit();
 phOne= sps.getString("phone","");
 password = sps.getString("password","");


 ArrayAdapter typeAdapter = new ArrayAdapter(this, R.layout.item_dropdown, typeArray);
 typeAdapter.setDropDownViewResource(R.layout.item_dropdown);
 Spinner sp_married = findViewById(R.id.sp_married);
 sp_married.setAdapter(typeAdapter);
 sp_married.setPrompt("请选择婚姻状况");
 sp_married.setSelection(0);
 sp_married.setOnItemSelectedListener(new TypeSelectedListener());
 }

 private String[] typeArray = {"未婚", "已婚"};

 class TypeSelectedListener implements AdapterView.OnItemSelectedListener {
 public void onItemSelected(AdapterView<&#63;> arg0, View arg1, int arg2, long arg3) {
  Married = (arg2 == 0) &#63; false : true;
 }

 public void onNothingSelected(AdapterView<&#63;> arg0) {
 }
 }

 @Override
 protected void onStart() {
 super.onStart();
 SQLiteDatabase mDB = getApplicationContext().openOrCreateDatabase("user.db", Context.MODE_PRIVATE, null);
 mHelper = UserDBHelper.getInstance(this, 1);
 mHelper.openWriteLink();
 }

 @Override
 protected void onStop() {
 super.onStop();
 mHelper.closeLink();
 }

 @Override
 public void onClick(View v) {
 if (v.getId() == R.id.btn_save) {
  String name = et_name.getText().toString();
  String age = et_age.getText().toString();
  String height = et_height.getText().toString();
  String weight = et_weight.getText().toString();
  if (name == null || name.length() <= 0) {
  showToast("请先填写姓名");
  return;
  }
  if (age == null || age.length() <= 0) {
  showToast("请先填写年龄");
  return;
  }
  if (height == null || height.length() <= 0) {
  showToast("请先填写身高");
  return;
  }
  if (weight == null || weight.length() <= 0) {
  showToast("请先填写体重");
  return;
  }

  UserInfo info = new UserInfo();

  info.name = name;
  info.age = Integer.parseInt(age);
  info.height = Long.parseLong(height);
  info.weight = Float.parseFloat(weight);
  info.married = Married;
  info.phOne= phone;
  info.password = password;

  info.update_time = DateUtil.getNowDate(DateUtil.DatePattern.ALL_TIME);
  Log.d(TAG, "onClick: 手机号" + info.phone+info.password+info.name+info.update_time+info.married);
  mHelper.insert(info);
  Intent intent = new Intent(InfoWriteActivity.this, InfoReadActivity.class);
  startActivity(intent);
  showToast("数据已写入SQLite数据库");
 }
 }

 private void showToast(String desc) {
 Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
 }

 public static void startHome(Context mContext) {
 Intent intent = new Intent(mContext, InfoWriteActivity.class);
 mContext.startActivity(intent);
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文讲述了如何通过代码在Android中更改Recycler视图项的背景颜色。通过在onBindViewHolder方法中设置条件判断,可以实现根据条件改变背景颜色的效果。同时,还介绍了如何修改底部边框颜色以及提供了RecyclerView Fragment layout.xml和项目布局文件的示例代码。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文讲述了作者通过点火测试男友的性格和承受能力,以考验婚姻问题。作者故意不安慰男友并再次点火,观察他的反应。这个行为是善意的玩人,旨在了解男友的性格和避免婚姻问题。 ... [详细]
  • 安卓select模态框样式改变_微软Office风格的多端(Web、安卓、iOS)组件库——Fabric UI...
    介绍FabricUI是微软开源的一套Office风格的多端组件库,共有三套针对性的组件,分别适用于web、android以及iOS,Fab ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
author-avatar
mobiledu2502930997
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有