仓廪实则知礼节,衣食足则知荣辱。这篇文章主要讲述Android仿QQ登录下拉历史列表相关的知识,希望能为你提供帮助。
最近项目开发中做到注册登录时,
产品要求登录历史列表类似于QQ登录,
经过一番的思考着手开始撸代码,
下面是两张效果图。下载完整demo地址:
https://download.csdn.net/download/lou_liang/10418605
文章图片
文章图片
demo中包含了Sqlite数据库增删改查, 对存储的账号进行按照最新的时间排序, 最多存储5条数据。
首先创建MyHelper建表:
public class MyHelper extends SQLiteOpenHelper {public MyHelper(Context context) { super(context," hayden.db" ,null,3); }@ Override public void onCreate(SQLiteDatabase db) { db.execSQL(" CREATE TABLE account(_id INTEGER PRIMARY KEY AUTOINCREMENT,phone VARCHAR(20),name VARCHAR(20),time INTEGER(100),fullName VARCHAR(20))" ); }@ Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
接着创建存储历史的bean类, 包含phone,name,time这三个字段, 然后创建AccountDao对数据增删改查:
public class AccountDao { public final static String TABLE_NAME = " account" ; private MyHelper helper; private String phone; public AccountDao(Context context){ helper= new MyHelper(context); }public void insert(HistoryInfo info){ SQLiteDatabase db= helper.getWritableDatabase(); //根据手机号判断去重 String[] colum = {" phone" }; String where = " phone" + " = ?" ; String[] whereValue = {info.getPhone()}; Cursor cursor = db.query(TABLE_NAME, colum, where, whereValue, null, null, null); while (cursor.moveToNext()){ phone = cursor.getString(cursor.getColumnIndex(" phone" )); } cursor.close(); ContentValues values= new ContentValues(); values.put(" phone" ,info.getPhone()); values.put(" name" ,info.getName()); values.put(" time" ,info.getTime()); if(!TextUtils.isEmpty(phone)){ db.update(TABLE_NAME,values," phone" + " = ?" ,new String[]{phone}); }else { db.insert(TABLE_NAME,null,values); } db.close(); } public int delete(String phone){ SQLiteDatabase db= helper.getWritableDatabase(); int count= db.delete(TABLE_NAME," phone= ?" ,new String[]{phone + " " }); db.close(); return count; } public List< HistoryInfo> queryAll(){ SQLiteDatabase db= helper.getWritableDatabase(); Cursor cursor= db.query(TABLE_NAME,null,null,null,null,null,null); List< HistoryInfo> list= new ArrayList(); while (cursor.moveToNext()) { HistoryInfo historyInfo = new HistoryInfo(); historyInfo.setPhone(cursor.getString(cursor.getColumnIndex(" phone" ))); historyInfo.setName(cursor.getString(cursor.getColumnIndex(" name" ))); historyInfo.setTime(cursor.getLong(cursor.getColumnIndex(" time" ))); list.add(historyInfo); } db.close(); cursor.close(); return list; } }
然后监听是否点击登录历史按钮, 如果上次登录成功, 那么将这条数据插入到数据库中, 点击历史按钮时查询列表, 并且按照登录时间降序。
//是否显示历史登录列表 historyCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @ Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { initPopuWindow(); //显示历史列表 if (historyList.size() = = 0) { pwdBottom.setVisibility(View.VISIBLE); } else { pwdBottom.setVisibility(View.GONE); } } else { selectPopupWindow.dismiss(); //隐藏列表 pwdBottom.setVisibility(View.VISIBLE); } } });
@ Override public void onClick(View v) { switch (v.getId()){ case R.id.loginBtn: if(TextUtils.isEmpty(userET.getText().toString()) || TextUtils.isEmpty(pwdET.getText().toString())){ Toast.makeText(LoginActivity.this," 账号或者密码不能为空" ,Toast.LENGTH_LONG).show(); return; }else { HistoryInfo historyInfo = new HistoryInfo(userET.getText().toString(), " Tom" , new Date().getTime()); accountDao.insert(historyInfo); startActivity(new Intent(LoginActivity.this,new SecondActivity().getClass())); } break; } }
这样仿QQ登录历史列表就完成了, 希望对看到文章的同学有所帮助。下载完整demo地址: https://download.csdn.net/download/lou_liang/10418605【Android仿QQ登录下拉历史列表】
推荐阅读
- 微信扫一扫二维码直接打开外部浏览器下载app怎么解决
- 1.9 Android
- WPF 文字换行TextWrapping 显示不全用省略号TextTrimming 显示不全弹提示内容ToolTip
- Android生命周期
- AppStore被拒整理
- 实例化flask的参数及对app的配置
- Android-Java多线程通讯(生产者 消费者)&10条线程对-等待唤醒/机制的管理
- APP支付微信支付,Java后台开发
- go语音基础之append函数的使用