當(dāng)前項(xiàng)目中有一個(gè)需求查排,就是需要在當(dāng)前app中不退出直接切換賬號(hào)闹伪,一般這種需求大多都是測(cè)試或者手機(jī)用戶比較多的吁讨,所以在現(xiàn)有項(xiàng)目中添加一個(gè)不算過(guò)分的需求,實(shí)現(xiàn)起來(lái)也很簡(jiǎn)單旱易,就是在登錄成功之后用數(shù)據(jù)庫(kù)實(shí)現(xiàn)保存用戶名和密碼禁偎,然后適配器切換賬號(hào)的時(shí)候調(diào)用數(shù)據(jù)庫(kù)保存的信息,直接把相關(guān)的用戶名和密碼填入輸入框阀坏,實(shí)現(xiàn)登錄如暖。
具體實(shí)現(xiàn)方法如下:
mOptionsDBHelper.open();
if (!mOptionsDBHelper.isOneExist(mEditText1.getText().toString())) { mUserNames.add(mEditText1.getText().toString());
mOptionsAdapter.notifyDataSetChanged();
}mOptionsDBHelper.close();
storeAccount();
用到了適配器和數(shù)據(jù)庫(kù):
/** * @ClassName OptionsAdapter *
@Description TODO 可選擇下拉列表適配器 *
@Version 1.0 * @Author jairkong *
@Creation 2013-8-1 下午4:00:11 *
@Mender jairkong *
@Modification 2013-8-1 下午4:00:11 **/
public class OptionsAdapter extends BaseAdapter {
? private List list = new ArrayList();
? ? private Context mContext;
? ? public OptionsAdapter(Context context, List list){
? ? ? this.mContext = context;
? ? ? this.list = list;
}
? @Override
? public int getCount() {
? ? ? return list.size();
}
? @Override
? public Object getItem(int position) {
? ? ? return list.get(position);
}
? @Override
? public long getItemId(int position) {
? ? ? return position;
}
? @Override
? public View getView(final int position, View convertView, ViewGroup parent) {
? ? ? ViewHolder holder= null;
? ? ? ? if (convertView == null) {
? ? ? ? ? ? holder= new ViewHolder();
? ? ? ? ? ? //下拉項(xiàng)布局
? ? ? ? ? ? convertView = LayoutInflater.from(mContext).inflate(R.layout.option_item, null);
? ? ? ? ? ? holder.relativeLayout=(RelativeLayout)convertView.findViewById(R.id.option_item_layout);
? ? ? ? ? ? holder.textView = (TextView) convertView.findViewById(R.id.option_item_text);
? ? ? ? ? ? holder.delBtn = (ImageView) convertView.findViewById(R.id.option_item_del);
? ? ? ? ? ? convertView.setTag(holder);
? ? ? ? } else {
? ? ? ? ? ? holder= (ViewHolder) convertView.getTag();
}
? ? ? ? holder.textView.setText(list.get(position));
? ? ? ? //為下拉框選項(xiàng)文字部分設(shè)置事件,最終效果是點(diǎn)擊將其文字填充到文本框
? ? ? ? holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
? ? ? ? @Override
? ? ? ? public void onClick(View v) {
? ? ? ? ? ? mOnClicked.onItemSelected(position);
}
? ? ? });
? ? ? ? //為下拉框選項(xiàng)刪除圖標(biāo)部分設(shè)置事件忌堂,最終效果是點(diǎn)擊將該選項(xiàng)刪除
? ? ? ? holder.delBtn.setOnClickListener(new View.OnClickListener() {
? ? ? ? @Override
? ? ? ? public void onClick(View v) {
? ? ? ? ? ? mOnClicked.onItemDelete(position);
}
? ? ? });
? ? ? ? return convertView;
}
? public OnClicked mOnClicked;
? public void setOnClicked(OnClicked onClicked)
? {
? ? ? mOnClicked=onClicked;
}
? public interface OnClicked
? {
? ? ? void onItemDelete(int index);
? ? ? void onItemSelected(int index);
}
}
class ViewHolder {
? RelativeLayout relativeLayout;
? ? TextView textView;
? ? ImageView delBtn;
}
/**
* @ClassNameOptionsDBHelper
* @DescriptionTODO 數(shù)據(jù)庫(kù)操作輔助類
* @Version1.0
* @Authorjairkong
* @Creation2013-8-2 上午10:41:50
* @Menderjairkong
* @Modification2013-8-2 上午10:41:50
**/
public class OptionsDBHelper {
? private static final boolean D=true;
? private static final String TAG="---OptionsDBHelper---";//LogCat
? ? private static final String DB_NAME="options.db";//數(shù)據(jù)庫(kù)名
? ? private static final String DB_TABLE="accounts";//數(shù)據(jù)庫(kù)表名
? ? private static final int? ? DB_VERSION=1;//數(shù)據(jù)庫(kù)版本號(hào)
? ? private Context mContext;
? ? private DBHelper dbHelper;
? ? private SQLiteDatabase database;
? ? public OptionsDBHelper(Context context) {
? ? ? // TODO Auto-generated constructor stub
? ? ? this.mContext=context;
}
? ? public boolean isOpen()
? ? {
? ? ? if (database.isOpen())
? ? ? {
? ? ? ? return true;
? ? ? } else {
? ? ? ? return false;
}
}
? ? /**
? ? * @功能 打開(kāi)數(shù)據(jù)庫(kù)
? ? * 空間不夠存儲(chǔ)的時(shí)候設(shè)為只讀
? ? * @throwsSQLiteException
? ? **/
? ? public boolean open() throws SQLiteException{
? ? ? dbHelper = new DBHelper(mContext, DB_NAME, null,DB_VERSION);
? ? ? ? try
? ? ? ? {
? ? ? ? ? database = dbHelper.getWritableDatabase();
? ? ? ? ? return true;
}
? ? ? ? catch (SQLiteException e)
? ? ? ? {
? ? ? ? ? database = dbHelper.getReadableDatabase();
? ? ? ? ? return false;
}
}
? ? /**
? ? * @功能 關(guān)閉數(shù)據(jù)庫(kù)
? ? *
**/
? ? public void close() {
? ? ? if (database!=null) {
? ? ? ? database.close();
? ? ? ? database=null;
}
? ? ? Log.e(TAG, "close()");
}
? ? /**
? ? * @功能? 更新選中賬號(hào)記錄
? ? * @return
**/
? ? public void updateState(String name) {
? ? ? if (name!=null)
? ? ? {
? ? ? ? ? database.execSQL("UPDATE "+DB_TABLE+" SET "+DBHelper.KEY_LAST_ACC+
? ? ? ? ? ? ? " = 1 WHERE "+DBHelper.KEY_USERNAME+" = "+"\""+name+"\";");
? ? ? ? ? database.execSQL("UPDATE "+DB_TABLE+" SET "+DBHelper.KEY_LAST_ACC+
? ? ? ? ? ? ? ? " = 0 WHERE "+DBHelper.KEY_USERNAME+" <> "+"\""+name+"\";");//將其它的置為0
? ? ? }
? ? ? else
? ? ? {
? ? ? ? ? database.execSQL("UPDATE "+DB_TABLE+" SET "+DBHelper.KEY_LAST_ACC+
? ? ? ? ? ? ? ? " = 0 ;");//將其它的置為0
? ? ? }
}
? ? /**
? ? * @功能 向表中添加一條數(shù)據(jù)
? ? * @return
**/
? ? public long insert( UserAccount userAccount) {
? ? ? ContentValues contentValues=new ContentValues();
? ? ? contentValues.put(DBHelper.KEY_USERNAME, userAccount.getUserName());
? ? ? contentValues.put(DBHelper.KEY_PASSWORD, userAccount.getPassWord());
? ? ? contentValues.put(DBHelper.KEY_REMB_PWD, (userAccount.getRembPwd())?1:0);
? ? ? contentValues.put(DBHelper.KEY_AUTO_LOG, (userAccount.getAutoLog())?1:0);
? ? ? contentValues.put(DBHelper.KEY_LAST_ACC, (userAccount.getLastAcc())?1:0);
? ? ? return database.insert(DB_TABLE, null, contentValues);
}
? ? /**
? ? * @功能 向表中刪除一條數(shù)據(jù)
? ? * @return
**/
? ? public long deleteOne(String name) {
? ? ? return database.delete(DB_TABLE, DBHelper.KEY_USERNAME+"="+"\""+name+"\"", null);
}
? ? /**
? ? * @功能 刪除數(shù)據(jù)表
? ? * @return
**/
? ? public long deleteAllData() {
? ? ? return database.delete(DB_TABLE,null,null);
}
? ? /**
? ? * @功能? 獲得上一次登錄的賬號(hào)
? ? * @param
* @return
**/
? ? public UserAccount queryLastAcc() {
? ? ? UserAccount userAccount=null;
? ? ? Cursor result=database.query(DB_TABLE, null, DBHelper.KEY_LAST_ACC+"="+"\""+String.valueOf(1)+"\"", null, null, null, null);
? ? ? Log.e(TAG, "queryOneData()---result:"+result.getColumnCount());
? ? ? if (result.getCount()>0)
? ? ? {
? ? ? ? ? userAccount=ConvertAll(result)[0];
}
? ? ? result.close();
? ? ? return userAccount;
}
? ? /**
? ? * @功能? 根據(jù)包名查詢一條數(shù)據(jù)
? ? * @param
* @return
**/
? ? public UserAccount queryOne(String name) {
? ? ? UserAccount userAccount;
? ? ? Cursor result=database.query(DB_TABLE, null, DBHelper.KEY_USERNAME+"="+"\""+name+"\"", null, null, null, null);
? ? ? Log.e(TAG, "queryOneData()---result:"+result.getColumnCount());
? ? ? userAccount=ConvertAll(result)[0];
? ? ? result.close();
? ? ? return userAccount;
}
? ? /**
? ? * @功能? 查詢?nèi)繑?shù)據(jù)記錄數(shù)量
? ? * @return
**/
? ? public int queryDataCount(){
? ? ? int count=0;
? ? ? ? Cursor result= database.query(DB_TABLE, null, null, null, null, null, null);
? ? ? ? count=result.getCount();
? ? ? result.close();
? ? ? return count;
}
? ? /**
? ? * @功能? 查詢?nèi)繑?shù)據(jù)
? ? * @return
**/
? ? public UserAccount[] queryAllData(){
? ? ? UserAccount[] userAccounts;
? ? ? ? Cursor result= database.query(DB_TABLE, null, null, null, null, null, null);
? ? ? ? userAccounts=ConvertAll(result);
? ? ? result.close();
? ? ? return userAccounts;
}
? ? /**
? ? * @功能? 根據(jù)包名更新一條數(shù)據(jù)
? ? * @param
* @returnlong
**/
? ? public long updateOne(String name,UserAccount userAccount){
? ? ? ContentValues contentValues=new ContentValues();
? ? ? contentValues.put(DBHelper.KEY_USERNAME, userAccount.getUserName());
? ? ? contentValues.put(DBHelper.KEY_PASSWORD, userAccount.getPassWord());
? ? ? contentValues.put(DBHelper.KEY_REMB_PWD, (userAccount.getRembPwd())?1:0);
? ? ? contentValues.put(DBHelper.KEY_AUTO_LOG, (userAccount.getAutoLog())?1:0);
? ? ? contentValues.put(DBHelper.KEY_LAST_ACC, (userAccount.getLastAcc())?1:0);
? ? ? ? return database.update(DB_TABLE, contentValues, DBHelper.KEY_USERNAME+"="+"\""+name+"\"",null);
}
? ? /**
? ? * @功能? 根據(jù)包名查詢一條數(shù)據(jù)是否存在
? ? * @param
* @return
**/
? ? public boolean isOneExist(String name) {
? ? ? Cursor result=database.query(DB_TABLE, null, DBHelper.KEY_USERNAME+"="+"\""+name+"\"", null, null, null, null);
? ? ? ? if(result.getCount() == 0 || !result.moveToFirst())
? ? ? ? {
? ? ? ? ? result.close();
? ? ? ? ? return false ;
? ? ? ? }else {
? ? ? ? ? result.close();
? ? ? ? return true;
}
}
? ? private UserAccount[] ConvertAll(Cursor cursor){
? ? ? ? int resultCounts= cursor.getCount();
? ? ? ? if (D)Log.e(TAG, "ConvertToAppInfo()---resultCounts:"+resultCounts);
? ? ? ? if(resultCounts== 0 || !cursor.moveToFirst())
? ? ? ? {
? ? ? ? ? ? return null ;
}
? ? ? ? UserAccount[] userAccounts= new UserAccount[resultCounts];
? ? ? ? Log.i(TAG, "AppInfo length:"+userAccounts.length);
? ? ? ? for (int i= 0; i< resultCounts; i++)
? ? ? ? {
? ? ? ? ? userAccounts[i] = new UserAccount();
? ? ? ? ? userAccounts[i].setUserName(cursor.getString(cursor.getColumnIndex(DBHelper.KEY_USERNAME)));
? ? ? ? ? userAccounts[i].setPassWord(cursor.getString(cursor.getColumnIndex(DBHelper.KEY_PASSWORD)));
? ? ? ? ? userAccounts[i].setRembPwd(((byte)(cursor.getInt(cursor.getColumnIndex(DBHelper.KEY_REMB_PWD)))==1)?true:false);
? ? ? ? ? userAccounts[i].setAutoLog(((byte)(cursor.getInt(cursor.getColumnIndex(DBHelper.KEY_AUTO_LOG)))==1)?true:false);
? ? ? ? ? userAccounts[i].setLastAcc(((byte)(cursor.getInt(cursor.getColumnIndex(DBHelper.KEY_LAST_ACC)))==1)?true:false);
? ? ? ? ? ? cursor.moveToNext();
}
? ? ? ? cursor.close();
? ? ? ? return userAccounts;
}
? ? public class UserAccount {
? ? ? ? private String mUserName="", mPassWord="";
? ? ? ? private boolean mLastAcc=false, mRembPwd=true, mAutoLog=false;
? ? ? ? /**
? ? ? * @NameOptionsDBHelper.UserAccounts
? ? ? * @DescriptionTODO
? ? ? * @Date2013-8-2 下午1:01:42
**/
? ? ? public UserAccount()
? ? ? ? {
? ? ? ? // TODO Auto-generated constructor stub
? ? ? }
? ? ? ? public UserAccount(String username, String password, boolean lastacc, boolean rempwd, boolean autolog)
? ? ? ? {
? ? ? ? this.mUserName=username;
? ? ? ? this.mPassWord=password;
? ? ? ? this.mRembPwd=rempwd;
? ? ? ? this.mAutoLog=autolog;
? ? ? ? this.mLastAcc=lastacc;
}
? ? ? ? public void setUserName(String un)
? ? ? ? {
? ? ? ? ? this.mUserName=un;
}
? ? ? ? public String getUserName()
? ? ? ? {
? ? ? ? ? return this.mUserName;
}
? ? ? ? public void setPassWord(String pwd)
? ? ? ? {
? ? ? ? ? this.mPassWord=pwd;
}
? ? ? ? public String getPassWord()
? ? ? ? {
? ? ? ? ? return this.mPassWord;
}
? ? ? ? public void setRembPwd(boolean rem)
? ? ? ? {
? ? ? ? ? this.mRembPwd=rem;
}
? ? ? ? public boolean getRembPwd()
? ? ? ? {
? ? ? ? ? return this.mRembPwd;
}
? ? ? ? public void setAutoLog(boolean auto)
? ? ? ? {
? ? ? ? ? this.mAutoLog=auto;
}
? ? ? ? public boolean getAutoLog()
? ? ? ? {
? ? ? ? ? return this.mAutoLog;
}
? ? ? ? public void setLastAcc(boolean la)
? ? ? ? {
? ? ? ? ? this.mLastAcc=la;
}
? ? ? ? public boolean getLastAcc()
? ? ? ? {
? ? ? ? ? return this.mLastAcc;
}
? ? ? ? @Override
? ? ? ? public String toString(){
? ? ? ? ? ? String str=
? ? ? ? ? ? "賬號(hào):"+this.mUserName+","
? ? ? ? ? ? +"密碼:"+this.mPassWord+","
? ? ? ? ? ? +"記住密碼:"+this.mRembPwd+","
? ? ? ? ? ? +"自動(dòng)登錄:"+this.mAutoLog+","
? ? ? ? ? ? +"是否為上次賬號(hào):"+this.mLastAcc;
? ? ? ? ? ? return str;
}
}
? ? public class DBHelper extends SQLiteOpenHelper {
? ? ? public static final boolean D=true;
? ? ? public static final String TAG="---DBHelper---";//LogCat
? ? ? ? private static final String DB_TABLE="accounts";//數(shù)據(jù)庫(kù)表名
? ? ? ? private static final int? ? DB_VERSION=1;//數(shù)據(jù)庫(kù)版本號(hào)
? ? ? ? private static final String KEY_USERNAME? ? ? ? = "UserName";
? ? ? ? private static final String KEY_PASSWORD? ? ? ? = "PassWord";
? ? ? ? private static final String KEY_REMB_PWD? ? ? ? = "RembPwd";
? ? ? ? private static final String KEY_AUTO_LOG? ? ? ? = "AutoLog";
? ? ? ? private static final String KEY_LAST_ACC? ? ? ? = "LastAcc";
? ? ? ? private static final String CREATE_TABLE_SQL=
? ? ? ? ? ? ? ? "CREATE TABLE "+DB_TABLE
? ? ? ? ? ? ? ? +" ("+KEY_USERNAME+" VARCHAR(32) NOT NULL PRIMARY KEY, "
? ? ? ? ? ? ? ? +KEY_PASSWORD+" VARCHAR(32) NOT NULL, "
? ? ? ? ? ? ? ? +KEY_REMB_PWD+" BIT(1) DEFAULT 1, "
? ? ? ? ? ? ? ? +KEY_AUTO_LOG+" BIT(1) DEFAULT 0, "
? ? ? ? ? ? ? ? +KEY_LAST_ACC+" BIT(1) DEFAULT 0);";
? ? ? ? public DBHelper(Context context, String name,CursorFactory factory, int version)
? ? ? ? {
? ? ? ? ? super(context, name, factory, version);
}
? ? ? @Override
? ? ? public void onCreate(SQLiteDatabase db){
? ? ? ? ? // 第一個(gè)使用數(shù)據(jù)庫(kù)時(shí)自動(dòng)建表
? ? ? ? ? ? db.execSQL(CREATE_TABLE_SQL);
}
? ? ? /**
*
? ? ? ? * 函數(shù)在數(shù)據(jù)庫(kù)需要升級(jí)時(shí)被調(diào)用盒至, 一般用來(lái)刪除舊的數(shù)據(jù)庫(kù)表,并將數(shù)據(jù)轉(zhuǎn)移到新版本的數(shù)據(jù)庫(kù)表中
? ? ? ? *
**/
? ? ? @Override
? ? ? public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
? ? ? ? db.execSQL("DROP TABLE IF EXISTS "+CREATE_TABLE_SQL);
? ? ? ? ? ? onCreate(db);
? ? ? ? ? ? if (D)Log.i(TAG, "Upgrade");
}
}
}
在登錄的Activity里面條用的方法如下:
/**
? ? * @returnvoid
? ? * @NameinitData
? ? * @DescriptionTODO? 初始化數(shù)據(jù)
? ? * @Authorfzq
? ? * @Date2018-5-2 下午8:18:43
**/
? ? public void initData() {
? ? ? ? mCount = restoreAccounts();
? ? ? ? OptionsDBHelper.UserAccount userAccount= restoreLastAccount();
? ? ? ? if (mCount > 0 && userAccount!= null) {
? ? ? ? ? ? mEditText1.setText(userAccount.getUserName());
? ? ? ? ? ? mEditText2.setText(userAccount.getPassWord());
}
? ? ? ? //PopupWindow浮動(dòng)下拉框布局
? ? ? ? layout_option = (View) this.getLayoutInflater().inflate(R.layout.layout_options, null);
? ? ? ? ListView listView= (ListView) layout_option.findViewById(R.id.layout_options_list);
? ? ? ? //設(shè)置自定義Adapter
? ? ? ? mOptionsAdapter = new OptionsAdapter(LoginActivity.this, mUserNames);
? ? ? ? mOptionsAdapter.setOnClicked(onItemClicked);
? ? ? ? listView.setAdapter(mOptionsAdapter);
}
? ? /**
? ? * @returnvoid
? ? * @NamestoreAccount
? ? * @DescriptionTODO? 保存用戶登陸信息
? ? * @Authorfzq
? ? * @Date2018-5-2 下午3:28:06
**/
? ? public void storeAccount() {
? ? ? ? OptionsDBHelper.UserAccount userAccount= mOptionsDBHelper.new UserAccount();
? ? ? ? userAccount.setUserName(mEditText1.getText().toString());
? ? ? ? userAccount.setPassWord(mEditText2.getText().toString());
? ? ? ? userAccount.setLastAcc(true);
? ? ? ? mOptionsDBHelper.open();
? ? ? ? mOptionsDBHelper.updateState(mEditText1.getText().toString());
? ? ? ? if (mOptionsDBHelper.isOneExist(mEditText1.getText().toString())) {
? ? ? ? ? ? mOptionsDBHelper.updateOne(userAccount.getUserName(), userAccount);
? ? ? ? } else {
? ? ? ? ? ? mOptionsDBHelper.insert(userAccount);
}
? ? ? ? mOptionsDBHelper.close();
}
? ? /**
? ? * @returnvoid
? ? * @NamestoreUserInfo
? ? * @DescriptionTODO? 移除指定用戶信息
? ? * @Authorfzq
? ? * @Date2018-5-2 下午3:28:06
**/
? ? public void removeAccount(String username) {
? ? ? ? mOptionsDBHelper.open();
? ? ? ? mOptionsDBHelper.deleteOne(username);
? ? ? ? mOptionsDBHelper.close();
}
? ? /**
? ? * @returnvoid
? ? * @NamerestoreAccounts
? ? * @DescriptionTODO? 還原登錄賬號(hào)信息
? ? * @Authorfzq
? ? * @Date2018-5-2 上午10:59:02
**/
? ? public int restoreAccounts() {
? ? ? ? mOptionsDBHelper.open();
? ? ? ? int count= mOptionsDBHelper.queryDataCount();
? ? ? ? Log.d("restoreAccounts", "count:" + count);
? ? ? ? if (count> 0) {
? ? ? ? ? ? OptionsDBHelper.UserAccount[] userAccounts= new OptionsDBHelper.UserAccount[count];
? ? ? ? ? ? userAccounts= mOptionsDBHelper.queryAllData();
? ? ? ? ? ? mUserNames.clear();
? ? ? ? ? ? for (OptionsDBHelper.UserAccount userAccount: userAccounts) {
? ? ? ? ? ? ? ? mUserNames.add(userAccount.getUserName());
}
? ? ? ? ? ? mOptionsDBHelper.close();
? ? ? ? } else {
}
? ? ? ? return count;
}
? ? /**
? ? * @returnvoid
? ? * @NamestoreSelectedAccount
? ? * @DescriptionTODO 保存當(dāng)前用戶賬號(hào)
? ? * @Authorfzq
? ? * @Date2018-5-2 下午8:00:59
**/
? ? public void storeSelectedAccount(String name) {
? ? ? ? mOptionsDBHelper.open();
? ? ? ? mOptionsDBHelper.updateState(name);
? ? ? ? mOptionsDBHelper.close();
}
? ? /**
? ? * @paramname
? ? * @returnvoid
? ? * @NameupdateState
? ? * @DescriptionTODO 更新選中賬號(hào)狀態(tài)
? ? * @Authorfzq
? ? * @Date2018-5-2 下午5:17:06
**/
? ? public void updateState(String name) {
? ? ? ? OptionsDBHelper.UserAccount userAccount= mOptionsDBHelper.new UserAccount();
? ? ? ? mOptionsDBHelper.open();
? ? ? ? userAccount= mOptionsDBHelper.queryOne(name);
? ? ? ? mOptionsDBHelper.close();
? ? ? ? mEditText1.setText(userAccount.getUserName());
? ? ? ? mEditText2.setText(userAccount.getPassWord());
}
? ? /**
? ? * @returnvoid
? ? * @NamerestoreLastAccount
? ? * @DescriptionTODO? 恢復(fù)上次記住用戶賬號(hào)信息
? ? * @Authorfzq
? ? * @Date2018-5-2 下午8:11:26
**/
? ? public OptionsDBHelper.UserAccount restoreLastAccount() {
? ? ? ? OptionsDBHelper.UserAccount userAccount= mOptionsDBHelper.new UserAccount();
? ? ? ? mOptionsDBHelper.open();
? ? ? ? userAccount= mOptionsDBHelper.queryLastAcc();
? ? ? ? mOptionsDBHelper.close();
? ? ? ? return userAccount;
}
? ? /**
? ? * @returnboolean
? ? * @NamejudgeLoginInfo
? ? * @DescriptionTODO 檢查登錄信息是否符合要求
? ? * @Data2018-5-2
**/
? ? protected boolean judgeLoginInfo() {
? ? ? ? // TODO Auto-generated method stub
? ? ? ? Animation shakeAnimY, shakeAnimX;
? ? ? ? boolean isUserOK= false, isPwdOK= false;
? ? ? ? shakeAnimX= AnimationUtils.loadAnimation(LoginActivity.this, R.anim.shake_x);
? ? ? ? shakeAnimY= AnimationUtils.loadAnimation(LoginActivity.this, R.anim.shake_y);
? ? ? ? if (mEditText1.getText().toString().length() == 0 || mEditText2.getText().toString().length() == 0) {
? ? ? ? ? ? Toast.makeText(LoginActivity.this, getResources().getString(R.string.login_input_blank), Toast.LENGTH_SHORT).show();
? ? ? ? ? ? mEditText1.startAnimation(shakeAnimY);
? ? ? ? ? ? mEditText2.startAnimation(shakeAnimY);
? ? ? ? ? ? isUserOK= false;
? ? ? ? ? ? isPwdOK= false;
? ? ? ? } else {
? ? ? ? ? ? if (mEditText1.getText().toString().length() > 32 || mEditText1.getText().toString().length() < 4) {//4字符?
? ? ? ? ? ? ? ? Toast.makeText(LoginActivity.this, getResources().getString(R.string.login_username_length), Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? mEditText1.startAnimation(shakeAnimY);
//? ? ? ? ? et_userName.setText("");
? ? ? ? ? ? ? ? isUserOK= false;
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? isUserOK= true;
}
? ? ? ? ? ? if (mEditText2.getText().toString().length() > 32 || mEditText2.getText().toString().length() < 6) {//6字符
? ? ? ? ? ? ? ? Toast.makeText(LoginActivity.this, getResources().getString(R.string.login_password_length), Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? mEditText2.startAnimation(shakeAnimY);
? ? ? ? ? ? ? ? mEditText2.setText("");
? ? ? ? ? ? ? ? isPwdOK= false;
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? isPwdOK= true;
}
}
? ? ? ? if (isPwdOK& isUserOK) {
? ? ? ? ? ? return true;
? ? ? ? } else {
? ? ? ? ? ? return false;
}
}
? ? /**
? ? * @returnvoid
? ? * @NameuploadOptionPop
? ? * @DescriptionTODO? 打開(kāi)賬號(hào)選擇Popupwindow
? ? * @Authorfzq
? ? * @Date2018-5-2 下午9:47:05
**/
? ? private void uploadOptionPop(boolean show_flag) {
? ? ? ? if (show_flag) {
? ? ? ? ? ? if (selectPopupWindow != null) {
? ? ? ? ? ? ? ? if (selectPopupWindow.isShowing()) {
? ? ? ? ? ? ? ? ? ? selectPopupWindow.dismiss();
}
? ? ? ? ? ? ? ? selectPopupWindow = null;
}
? ? ? ? ? ? selectPopupWindow = new PopupWindow(layout_option, linear.getWidth(), LinearLayout.LayoutParams.FILL_PARENT, true);
? ? ? ? ? ? selectPopupWindow.setBackgroundDrawable(new BitmapDrawable());// 設(shè)置允許在外點(diǎn)擊消失
? ? ? ? ? ? selectPopupWindow.showAsDropDown(linear, 0, 0);
? ? ? ? ? ? selectPopupWindow.setAnimationStyle(R.style.PopupAnimation);
? ? ? ? ? ? selectPopupWindow.setFocusable(true);
? ? ? ? ? ? selectPopupWindow.setOutsideTouchable(true);
? ? ? ? ? ? selectPopupWindow.update();
? ? ? ? } else {
? ? ? ? ? ? if (selectPopupWindow != null) {
? ? ? ? ? ? ? ? selectPopupWindow.dismiss();
? ? ? ? ? ? ? ? selectPopupWindow.setFocusable(false);
}
}
? ? }
在初始化的時(shí)候別忘了加上:
mOptionsDBHelper = new OptionsDBHelper(LoginActivity.this);
initData();
還有就是布局文件士修,很簡(jiǎn)單的枷遂,我就不一一貼出來(lái)了,有需要的話聯(lián)系我棋嘲!