實現密碼輸入

来源:http://www.cnblogs.com/rainhome/archive/2016/03/30/5339455.html
-Advertisement-
Play Games

效果如圖 1 首先這是一個自定義的Dialog,而不是AlertDialog,如果是AlertDialog的話,軟鍵盤彈出的時候在AlertDialog的後面,無法進行輸入。 2 Dialog的上面會有一個黑框,添加Style 3 輸入密碼用的是EditText,改變EditText的格式是 4 給 ...


效果如圖

1 首先這是一個自定義的Dialog,而不是AlertDialog,如果是AlertDialog的話,軟鍵盤彈出的時候在AlertDialog的後面,無法進行輸入。

2 Dialog的上面會有一個黑框,添加Style

 <style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <!-- 邊框 -->
        <item name="android:windowIsFloating">true</item>
        <!-- 是否浮現在activity之上 -->
        <item name="android:windowIsTranslucent">true</item>
        <!-- 半透明 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 無標題 -->
        <item name="android:windowBackground">@color/transparent</item>
        <!-- 背景透明 -->
        <item name="android:backgroundDimEnabled">true</item>
        <!-- 模糊 -->
    </style>

3 輸入密碼用的是EditText,改變EditText的格式是

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/white" />
    <corners android:radius="3dip"/>
    <stroke
        android:width="1dip"
        android:color="@color/viewfinder_mask" />
</shape>

4 給EditText添加監聽函數,本來想把EditText放入到List來管理,結果總是報錯指針錯誤,只能作罷- -,所以看起來有很多重覆的代碼啊,要監聽EditText的內容改變,限制輸入的內容,預設彈出數字鍵盤,監聽鍵盤的刪除鍵,以下是實現代碼

void initDialog() {
         current=0;
         myDialog = new Dialog(ChargeActivity.this, R.style.add_dialog);
        myDialog.show();
        myDialog.getWindow().setContentView(R.layout.return_code_dialog);
        psw1 = (EditText) myDialog.getWindow().findViewById(R.id.psw_1);
        psw2 = (EditText) myDialog.getWindow().findViewById(R.id.psw_2);
        psw3 = (EditText) myDialog.getWindow().findViewById(R.id.psw_3);
        psw4 = (EditText) myDialog.getWindow().findViewById(R.id.psw_4);
        psw5 = (EditText) myDialog.getWindow().findViewById(R.id.psw_5);
        psw6 = (EditText) myDialog.getWindow().findViewById(R.id.psw_6);

        //鎖定數字鍵盤
        psw1.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
        psw2.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
        psw3.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
        psw4.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
        psw5.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
        psw6.setInputType(EditorInfo.TYPE_CLASS_NUMBER);

        psw1.addTextChangedListener(this);
        psw2.addTextChangedListener(this);
        psw3.addTextChangedListener(this);
        psw4.addTextChangedListener(this);
        psw5.addTextChangedListener(this);
        psw6.addTextChangedListener(this);

         psw1.setOnKeyListener(this);
         psw2.setOnKeyListener(this);
         psw3.setOnKeyListener(this);
         psw4.setOnKeyListener(this);
         psw5.setOnKeyListener(this);
         psw6.setOnKeyListener(this);

    }

        @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        int length =0;
        //Log.d("test","current="+current+"我變了");
        switch(current){
            case 0:length=psw1.getText().length();changeFocus(length,psw2);passwordInput[0]=psw1.getText().toString();break;
            case 1:length=psw2.getText().length();changeFocus(length,psw3);passwordInput[1]=psw2.getText().toString();break;
            case 2:length=psw3.getText().length();changeFocus(length,psw4);passwordInput[2]=psw3.getText().toString();break;
            case 3:length=psw4.getText().length();changeFocus(length,psw5);passwordInput[3]=psw4.getText().toString();break;
            case 4:length=psw5.getText().length();changeFocus(length,psw6);passwordInput[4]=psw5.getText().toString();break;
            case 5:length=psw6.getText().length();if(length>=1){passwordInput[5]=psw6.getText().toString();Log.d("input",passwordInput[5]);validatePassword();myDialog.cancel();}
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    void changeFocus(int l,EditText editText){
        if(l >= maxLen){
            current++;
            //Log.d("test", "current=" + current+" 請求焦點");
            editText.setFocusable(true);
            editText.setFocusableInTouchMode(true);
            editText.requestFocus();
            editText.findFocus();
            /**if(editText.isFocusable()){
                Log.d("test",editText.getId()+"獲得焦點");
            }*/
        }
    }
    void validatePassword(){
        current=0;
        boolean isInSchool =true;
        StringBuffer stringBuffer=new StringBuffer();
        for(int i=0;i<passwordInput.length;i++){
            stringBuffer.append(passwordInput[i]);
        }
        String psw =stringBuffer.toString();
        boolean isPassword =psw.equals(password);
        Log.d("psw",passwordInput.toString());
        Log.d("psw",password);
        if(!isPassword){
            final Dialog errorDialog =new AlertDialog.Builder(ChargeActivity.this).create();
            errorDialog.show();
            errorDialog.getWindow().setContentView(R.layout.error_password_dialog);
            errorDialog.getWindow().findViewById(R.id.ok_error).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    errorDialog.cancel();
                    initDialog();
                }
            });
        }else if(!isInSchool){
            final Dialog out =new AlertDialog.Builder(ChargeActivity.this).create();
            out.show();
            out.getWindow().setContentView(R.layout.out_school_dialog);
            out.getWindow().findViewById(R.id.ok_out_school).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    out.cancel();
                }
            });
        }else{
            startActivity(new Intent(ChargeActivity.this,PaymentActivity.class));
        }
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_DEL
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            current--;
            switch(current){
                case 0:psw1.setText("");requestF(psw1);break;
                case 1:psw2.setText("");requestF(psw2);break;
                case 2:psw3.setText("");requestF(psw3);break;
                case 3:psw4.setText("");requestF(psw4);break;
                case 4:psw5.setText("");requestF(psw5);break;
                case 5:psw6.setText("");requestF(psw6);break;
            }
        }
        return false;
    }
    void requestF(EditText editText){
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        editText.findFocus();
    }

寫的過程中犯了一個比較傻的錯誤,字元串數組沒分配記憶體,結果一直報指針錯誤,傻呵呵的調了半天= =

還有就是一些邏輯細節的問題,向刪除的時候比較容易引起焦點混亂。

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • There was an internal API error. 錯誤原因:把Product Name作為程式名稱,程式名稱錯亂 解決方法:檢查Product Name, 不要包含中文以及特殊字元。在info.plist中新增Bundle display name:我的程式名稱。程式名稱改為英文,P ...
  • 項目使用RxJava+Retrofit2.0+MVP 後臺查詢使用LeanCloud REST API 主要實現了商品列表的獲取(載入更多和刷新)與展示,根據商品種類查詢 源碼下載:http://code.662p.com/view/13294.html 詳細說明:http://android.66 ...
  • 通過使用該源碼,開發者可以迅速地將Discuz論壇遷移到Android客戶端中。不需要任何的開發工作即可擁有屬於自己論壇的Android客戶端 源碼下載:http://code.662p.com/view/13266.html 準備工作 在使用源碼之前必須先在Discuz論壇中安裝BigApp插件。 ...
  • Android 開發了一段時間,一方面 ,感覺不留下點什麼。有點對不起自己, 另一方面,好記性不如爛筆頭,為了往後可以回頭來看看,就當做是筆記,便決定開始寫博客。廢話不多說 ! 今天想搞一搞 ndk 和jni ,, 現在開始寫一個簡單的demo 1. 創建一個新的工程 2. 創建一個新的類 JniT ...
  • iOS App的性能關註點 雖然iPhone的機能越來越好,但是app的功能也越來越複雜,性能從來都是移動開發的核心關註點之一。我們說一個app性能好,不是簡單指感覺運行速度快,而應該是指應用啟動快速、UI反饋響應及時、列表滾動操作流暢、記憶體使用合理,當然更不能隨隨便便Crash啦。工程師開發應用時 ...
  • 本文內容 環境 android-common 項目結構 演示 android-common 參考資料 android-common 主要包括如下內容: 緩存,包括圖片緩存、預取緩存、網路緩存。 公共 View,即功能封裝好的部件,包括下拉獲得最新和上拉載入更多 ListView、底部載入更多 Scr ...
  • 匿名內部類作為事件監聽器類實現頁面跳轉 @Overrideprotected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main1);/* ...
  • ```objc1 使用Crearte函數創建的併發隊列和全局併發隊列的主要區別: 1)全局併發隊列在整個應用程式中本身是預設存在的並且對應有高優先順序、預設優先順序、低優先順序和後臺優先順序一共四個併發隊列,我們只是選擇其中的一個直接拿來用。而Create函數是實打實的從頭開始去創建一個隊列。 2)在iOS ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...