仿迅雷之Android版-私密空間彈窗驗證
上篇文章http://blog.chinaunix.net/uid-25799257-id-4735623.html主要構想了一下私密空間的實現,這就就做個簡單的“單機版”的密碼驗證,根據使用者對應的密碼進行驗證,密碼正確則執行私密列表的載入,容器還是Listview,只是替換Adapter而以,Adapter的內容的管理目前我是每次都重新建立 - 也就是說每次都是重新查詢的,是否會耗時了?是否會影響使用者體驗呢?如果每次載入10條,通過下拉重新整理的話,我想應該還是沒問題的,即使串連後台查詢應該也OK的。 好了,多試多驗證就好了。。。。
密碼彈窗驗證應該也不難:
PrivateVerifyPopwin.java
- package com.errovenus.ui;
- import com.errorvenus.config.ScreenInfo;
- import com.errorvenus.fxunlei.R;
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.graphics.drawable.Drawable;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.PopupWindow;
- import android.widget.Toast;
- public class PrivateVerifyPopwin
- {
- ///< 上下文
- private Context context;
- ///< 彈窗
- private PopupWindow window = null;
- ///< 輸入密碼後確認按鈕
- private Button passSureBtn = null;
- ///< 密碼框
- private EditText private_passwordET = null;
- ///< 彈窗布局視圖
- private View contentView = null;
- public PrivateVerifyPopwin(Context _context)
- {
- this.context = _context;
- if (null != window)
- {
- if (window.isShowing())
- {
- window.dismiss();
- window = null;
- }
- }
- window = new PopupWindow(_context);
- }
- /// -----------------------me------------start-----------
- public PrivateVerifyPopwin(Context _context, Object obj)
- {
- this.context = _context;
- ///< 初始化控制項
- initControl();
- ///< 註冊監聽事件
- register();
- }
- /**
- * 初始化控制項
- */
- @SuppressLint("InflateParams")
- private void initControl()
- {
- ///< 載入和設定布局
- contentView = LayoutInflater.from(context).inflate(R.layout.privatepopuwin, null, true);
- window = new PopupWindow(contentView, (int)ScreenInfo.SCREEN_WIDTH/2, (int)ScreenInfo.SCREEN_HEIGHT/2, true);
- ///< 密碼
- private_passwordET = (EditText)contentView.findViewById(R.id.private_passwordET);
- ///< 確認按鈕
- passSureBtn = (Button)contentView.findViewById(R.id.privatePassSureBtn);
- ///< 設定背景和動畫
- window.setBackgroundDrawable(this.context.getResources().getDrawable(R.drawable.ic_item_selected_bg));
- window.setOutsideTouchable(true);
- window.setAnimationStyle(R.style.anim_menu_bottombar);
- }
- private void register()
- {
- passSureBtn.setOnClickListener(new OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- ///< TODO 網路查詢使用者對應的私密空間密碼是否正確,然後做處理
- ///< 單機版
- if (private_passwordET.getText().toString().equals("") || private_passwordET.getText().toString().length() < 6)
- {
- Toast.makeText(context, "請輸入正確的密碼,密碼至少為6位", Toast.LENGTH_SHORT).show();
- }
- else if (private_passwordET.getText().toString().equals("123456"))
- {
- Toast.makeText(context, "登入成功", Toast.LENGTH_SHORT).show();
- if (null != window)
- {
- window.dismiss();
- window = null;
- }
- ///< TODO 進行私人列表展示
- }
- else
- {
- Toast.makeText(context, "密碼錯誤", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- public void show(View anchor)
- {
- if (null != window)
- {
- window.showAtLocation(anchor, Gravity.CENTER, 0, 0);
- }
- }
- /// -----------------------me------------end-----------
- /// ------------------------自訂一些介面------------關於UI的未用-----------
- public PopupWindow setBackGround(Drawable drawId)
- {
- if (null != window)
- {
- window.setBackgroundDrawable(drawId);
- }
- return window;
- }
- public PopupWindow setContentView(int layoutResID)
- {
- if (null != window)
- {
- LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- window.setContentView(inflator.inflate(layoutResID, null));
- }
- return window;
- }
- public PopupWindow setOnDismissListener(PopupWindow.OnDismissListener listener)
- {
- if (null != window)
- {
- window.setOnDismissListener(listener);
- }
- return window;
- }
- public void show(View anchor, int xoff, int yoff)
- {
- if (null != window)
- {
- window.showAsDropDown(anchor, xoff, yoff);
- }
- }
- public boolean isShowing()
- {
- if (null != window)
- {
- return window.isShowing();
- }
- return false;
- }
- public void dismiss()
- {
- if (null != window)
- {
- window.dismiss();
- }
- }
- }
布局檔案:
privatepopuwin.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/privatePopuLiner"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <TextView
- android:id="@+id/private_tipTV"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:layout_marginBottom="10dip"
- android:gravity="center"
- android:text="@string/privatepassStr"
- android:textSize="@dimen/privatePopuTiptextSize" >
- </TextView>
- <EditText
- android:id="@+id/private_passwordET"
- android:layout_width="@dimen/editSizeWidth"
- android:layout_height="@dimen/editSizeHeigth"
- android:layout_below="@+id/private_tipTV"
- android:layout_centerInParent="true"
- android:inputType="textPassword"
- android:textSize="@dimen/textSize" >
- </EditText>
- <Button
- android:id="@+id/privatePassSureBtn"
- android:layout_width="40dip"
- android:layout_height="30dip"
- android:layout_alignParentBottom="true"
- android:layout_alignParentRight="true"
- android:background="@drawable/ic_category_passsure_selector"
- android:text="@string/privatepasssureStr" />
- </RelativeLayout>
這裡的尺寸我全部通過dimen.xml來擷取,這樣我做適配的時候非常方便,至少目前來看,
的適配基本都OK了,包括字型,當然產品一般來將都是將自己做到表徵圖上面的,這裡就是學習,所以字型不在表徵圖上啦!!
大體就是這樣的一個驗證框:
顯示的時候 - 我每次dismiss之後都會把privatePopu = null, 這裡如果很頻繁的點擊會有效能問題嗎?再想想?求正規做法.....:
補充:
後來我列印了下記憶體的損耗情況,大概這個popuwindow會佔用2000多個byte,也就是2k左右,當privatePopu置空後,再點擊,消失,再點擊,這樣反覆幾次 後,記憶體會從321932byte漲到368922byte,再來點擊幾次,又會回到32xxxx的狀態,至少目前從日誌上證明記憶體還是會回收的,不過是回收比較遲緩而已;而且再次證明其實每次重新分配,並不會耗太多效能,也不會佔用太多記憶體。。。當然還是希望做到效能最佳....
- ///< 私密空間
- else if (v.equals(menuItem06))
- {
- if (null != privatePopu && privatePopu.isShowing())
- {
- return;
- }
- else if (null != privatePopu && !privatePopu.isShowing())
- {
- privatePopu.show(menuItem06);
- }
- else
- {
- privatePopu = new PrivateVerifyPopwin(this.context, null);
- privatePopu.setOnDismissListener(new OnDismissListener()
- {
- @Override
- public void onDismiss()
- {
- privatePopu = null;
- }
- });
- privatePopu.show(menuItem06);
- }
- }
下一篇是關於AndroidPN訊息推送庫的一些效能測試報告;至於列表的顯示,適配器啥的,我想入門Android後應該剩下的是工作量或者細節上的問題,就不記錄了,要是有好的知識再記錄下吧....