【GT-安卓應用開發之隨機鍵盤】

前言:由於之前是做的支付相關的工作,因此對於密碼支付的安全比較重視,在工作中經常要接觸到隨機密碼鍵盤的需求。加上明天就要放假(公司給了一週的假期,很開心~)了,今天沒什麼工作,就編寫一個隨機鍵盤正好回一下曾經的工作吧。

           首先,先上圖片(界面較粗糙,不喜勿噴):

           

           從上面的圖片中,我們大致可以看出功能很簡單就是一個數字密碼鍵盤,支持刪除,接下來一起來實現:

           Step1,界面設計

           整個界面是一個豎立的線性佈局,由三部分組成,頭部標題、密碼TextView以及數字鍵盤,實現代碼如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1"
        android:background="#000066"
        >
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textColor="#ffffff"
            android:textSize="24sp"
            android:text="密碼鍵盤"
            />
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_weight="4"
        >
        
        <TextView 
            android:id="@+id/tv_pwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="36sp"
            android:password="true"
            android:maxLength="6"
            android:text=""
            />
        
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="6"
        >
        <LinearLayout
            android:id="@+id/table_num"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="6"
            android:background="#c8cbcc"
            android:orientation="vertical">

            <LinearLayout style="@style/layout_input_amount_style">

                <Button
                    android:id="@+id/btn_1"
                    style="@style/btn_input_amount_style"
                    android:layout_marginRight="1dp"
                    android:text="1" />

                <Button
                    android:id="@+id/btn_2"
                    style="@style/btn_input_amount_style"
                    android:layout_marginRight="1dp"
                    android:text="2" />

                <Button
                    android:id="@+id/btn_3"
                    style="@style/btn_input_amount_style"
                    android:text="3" />
            </LinearLayout>

            <LinearLayout style="@style/layout_input_amount_style">

                <Button
                    android:id="@+id/btn_4"
                    style="@style/btn_input_amount_style"
                    android:layout_marginRight="1dp"
                    android:text="4" />

                <Button
                    android:id="@+id/btn_5"
                    style="@style/btn_input_amount_style"
                    android:layout_marginRight="1dp"
                    android:text="5" />

                <Button
                    android:id="@+id/btn_6"
                    style="@style/btn_input_amount_style"
                    android:text="6" />
            </LinearLayout>

            <LinearLayout style="@style/layout_input_amount_style">

                <Button
                    android:id="@+id/btn_7"
                    style="@style/btn_input_amount_style"
                    android:layout_marginRight="1dp"
                    android:text="7" />

                <Button
                    android:id="@+id/btn_8"
                    style="@style/btn_input_amount_style"
                    android:layout_marginRight="1dp"
                    android:text="8" />

                <Button
                    android:id="@+id/btn_9"
                    style="@style/btn_input_amount_style"
                    android:text="9" />
            </LinearLayout>

            <LinearLayout style="@style/layout_input_amount_style">

                <Button
                    android:id="@+id/btn_e"
                    style="@style/btn_input_amount_style"
                    android:layout_marginRight="1dp"
                    android:background="#00ff00"
                    android:text="確認" />

                <Button
                    android:id="@+id/btn_0"
                    style="@style/btn_input_amount_style"
                    android:layout_marginRight="1dp"
                    android:text="0" />

                <ImageButton
                    android:id="@+id/btn_d"
                    style="@style/btn_input_amount_style"
                    android:src="@drawable/key_back" />

            </LinearLayout>
        </LinearLayout>
        
        
    </LinearLayout>

</LinearLayout>

           Step2,生成10個互不相同的0-9的數字

           設計思路是利用for循環中嵌套while循環生成0-9組成的字符串,代碼如下:

//產生n個互不相同的數(0~9)
    private String getRandom(int n) {
        // TODO Auto-generated method stub
        int x = 10;
        String result="";
                  Random rand = new Random();//新建一個隨機類
                  boolean[]  bool = new boolean[x];
                  int randInt = 0;
                  for(int i = 0; i < n ; i++) {
                       do {
                           randInt  = rand.nextInt(x);//產生一個隨機數
                       }while(bool[randInt]);
                      bool[randInt] = true;
                      result+=randInt;
                 }
         return result;
    }

           Step3,初始化密碼鍵盤並添加點擊事件

           private void initData() {
        // TODO Auto-generated method stub
        String ran=getRandom(10);
        ids=new int[]{R.id.btn_0,R.id.btn_1,R.id.btn_2,R.id.btn_3,R.id.btn_4,R.id.btn_5,R.id.btn_6,R.id.btn_7,R.id.btn_8,R.id.btn_9};
        pwd="";
        for(int i=0;i<ran.length();i++){
            ((Button)findViewById(ids[i])).setText(ran.charAt(i)+"");
            ((Button)findViewById(ids[i])).setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View view) {
                    // TODO Auto-generated method stub
                    Button btn = (Button)view;
                    if(tv_pwd.getText().toString().length()<6){
                        tv_pwd.setText(tv_pwd.getText().toString()+btn.getText());
                    }else{
                        Toast.makeText(getBaseContext(), "密碼長度最長爲6", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

           Step4,處理確認與刪除事件

           

@Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
        case R.id.btn_e:
            if(tv_pwd.getText().equals("211314")){
                Toast.makeText(getBaseContext(), "驗證成功", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(getBaseContext(), "驗證失敗", Toast.LENGTH_SHORT).show();
            }
            break;
        case R.id.btn_d:
            if(tv_pwd.getText().length()>0){
                String str = tv_pwd.getText().toString();
                tv_pwd.setText(str.substring(0, str.length()-1));
            }
            break;

        default:
            break;
        }
    }

         Step5,運行~

發佈了52 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章