android简约型数字键盘实现

效果图:


xml代码,使用时将下面代码插入到你想添加数字键盘的地方即可:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_marginTop="29dp"
        android:background="@color/more_thin_blue"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:orientation="horizontal">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@color/transparent"
                >
            <LinearLayout
                android:id="@+id/pin_lock_screen_1_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:background="@drawable/bg_white_stroke_gray_radius_9"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    style="@style/baseTextStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1"
                    android:textColor="#333333"
                    android:textSize="@dimen/pin_lock_keyboard_text_large" />
            </LinearLayout>
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                >
            <LinearLayout
                android:id="@+id/pin_lock_screen_2_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_column="1"
                android:layout_columnWeight="1.0"
                android:layout_row="0"
                android:layout_rowWeight="1.0"
                android:layout_weight="1.0"
                android:background="@drawable/bg_white_stroke_gray_radius_9"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    style="@style/baseTextStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2"
                    android:textColor="#333333"
                    android:textSize="@dimen/pin_lock_keyboard_text_large" />
            </LinearLayout>
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                >
            <LinearLayout
                android:id="@+id/pin_lock_screen_3_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_column="2"
                android:layout_columnWeight="1.0"
                android:layout_row="0"
                android:layout_rowWeight="1.0"
                android:layout_weight="1.0"
                android:background="@drawable/bg_white_stroke_gray_radius_9"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    style="@style/baseTextStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="3"
                    android:textColor="#333333"
                    android:textSize="@dimen/pin_lock_keyboard_text_large" />
            </LinearLayout>
            </RelativeLayout>
        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:orientation="horizontal">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                >
            <LinearLayout
                android:id="@+id/pin_lock_screen_4_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1.0"
                android:background="@drawable/bg_white_stroke_gray_radius_9"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    style="@style/baseTextStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="4"
                    android:textColor="#333333"
                    android:textSize="@dimen/pin_lock_keyboard_text_large" />
            </LinearLayout>
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                >
            <LinearLayout
                android:id="@+id/pin_lock_screen_5_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1.0"
                android:background="@drawable/bg_white_stroke_gray_radius_9"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    style="@style/baseTextStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="5"
                    android:textColor="#333333"
                    android:textSize="@dimen/pin_lock_keyboard_text_large" />
            </LinearLayout>
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                >
            <LinearLayout
                android:id="@+id/pin_lock_screen_6_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1.0"
                android:background="@drawable/bg_white_stroke_gray_radius_9"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    style="@style/baseTextStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="6"
                    android:textColor="#333333"
                    android:textSize="@dimen/pin_lock_keyboard_text_large" />
            </LinearLayout>
            </RelativeLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:orientation="horizontal">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                >
            <LinearLayout
                android:id="@+id/pin_lock_screen_7_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1.0"
                android:background="@drawable/bg_white_stroke_gray_radius_9"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    style="@style/baseTextStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="7"
                    android:textColor="#333333"
                    android:textSize="@dimen/pin_lock_keyboard_text_large" />
            </LinearLayout>
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                >
            <LinearLayout
                android:id="@+id/pin_lock_screen_8_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1.0"
                android:background="@drawable/bg_white_stroke_gray_radius_9"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    style="@style/baseTextStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="8"
                    android:textColor="#333333"
                    android:textSize="@dimen/pin_lock_keyboard_text_large" />
            </LinearLayout>
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                >
            <LinearLayout
                android:id="@+id/pin_lock_screen_9_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1.0"
                android:background="@drawable/bg_white_stroke_gray_radius_9"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    style="@style/baseTextStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="9"
                    android:textColor="#333333"
                    android:textSize="@dimen/pin_lock_keyboard_text_large" />
            </LinearLayout>
            </RelativeLayout>
        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:orientation="horizontal">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                >
            <LinearLayout
                android:id="@+id/pin_lock_screen_urgent_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1.0"
                android:background="@drawable/bg_white_stroke_gray_radius_9"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    style="@style/baseTextStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="●"
                    android:textColor="#333333"
                    android:textSize="@dimen/pin_lock_keyboard_text_cn" />
            </LinearLayout>
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                >
            <LinearLayout
                android:id="@+id/pin_lock_screen_0_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1.0"
                android:background="@drawable/bg_white_stroke_gray_radius_9"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    style="@style/baseTextStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0"
                    android:textColor="#333333"
                    android:textSize="@dimen/pin_lock_keyboard_text_large" />
            </LinearLayout>
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                >
            <LinearLayout
                android:id="@+id/pin_lock_screen_delete_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1.0"
                android:background="@drawable/bg_white_stroke_gray_radius_9"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    style="@style/baseTextStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=" 删除"
                    android:textColor="#333333"
                    android:textSize="18sp" />
            </LinearLayout>
            </RelativeLayout>
        </LinearLayout>

    </LinearLayout>

java代码:

private static final int ANSWER_TEXT_MAX_LENGTH = 6;

    private LinearLayout layout0,layout1,layout2,layout3,layout4,layout5,
            layout6,layout7,layout8,layout9,urgentLayout,deleteLayout;
    private EditText etAnswer;
layout0 = view.findViewById(R.id.pin_lock_screen_0_layout);
        layout0.setOnClickListener(this);
        layout1 = view.findViewById(R.id.pin_lock_screen_1_layout);
        layout1.setOnClickListener(this);
        layout2 = view.findViewById(R.id.pin_lock_screen_2_layout);
        layout2.setOnClickListener(this);
        layout3 = view.findViewById(R.id.pin_lock_screen_3_layout);
        layout3.setOnClickListener(this);
        layout4 = view.findViewById(R.id.pin_lock_screen_4_layout);
        layout4.setOnClickListener(this);
        layout5 = view.findViewById(R.id.pin_lock_screen_5_layout);
        layout5.setOnClickListener(this);
        layout6 = view.findViewById(R.id.pin_lock_screen_6_layout);
        layout6.setOnClickListener(this);
        layout7 = view.findViewById(R.id.pin_lock_screen_7_layout);
        layout7.setOnClickListener(this);
        layout8 = view.findViewById(R.id.pin_lock_screen_8_layout);
        layout8.setOnClickListener(this);
        layout9 = view.findViewById(R.id.pin_lock_screen_9_layout);
        layout9.setOnClickListener(this);
        deleteLayout = view.findViewById(R.id.pin_lock_screen_delete_layout);
        deleteLayout.setOnClickListener(this);
        urgentLayout = view.findViewById(R.id.pin_lock_screen_urgent_layout);
        urgentLayout.setOnClickListener(this);
        etAnswer = view.findViewById(R.id.et_answer);

@Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.pin_lock_screen_0_layout:
                addPwdText(0);
                break;
            case R.id.pin_lock_screen_1_layout:
                addPwdText(1);
                break;
            case R.id.pin_lock_screen_2_layout:
                addPwdText(2);
                break;
            case R.id.pin_lock_screen_3_layout:
                addPwdText(3);
                break;
            case R.id.pin_lock_screen_4_layout:
                addPwdText(4);
                break;
            case R.id.pin_lock_screen_5_layout:
                addPwdText(5);
                break;
            case R.id.pin_lock_screen_6_layout:
                addPwdText(6);
                break;
            case R.id.pin_lock_screen_7_layout:
                addPwdText(7);
                break;
            case R.id.pin_lock_screen_8_layout:
                addPwdText(8);
                break;
            case R.id.pin_lock_screen_9_layout:
                addPwdText(9);
                break;
            case R.id.pin_lock_screen_delete_layout:
                addPwdText(-1);
                break;
            case R.id.pin_lock_screen_urgent_layout://输入.
                addPwdText(-2);
                break;
        }
    }
private void addPwdText(int num) {
        Log.i(TAG,"addPwdText num=" + num);
        String answer = etAnswer.getText().toString();
        if(num >= 0 && answer.length() > ANSWER_TEXT_MAX_LENGTH){
            return;
        }

        if(num >= 0){
            etAnswer.setText(answer + num);
        }else {
            if(TextUtils.isEmpty(answer)){
                return;
            }
            if(num == -1){
                etAnswer.setText(answer.substring(0,answer.length() - 1));
            }else if(num == -2){
                etAnswer.setText(answer + ".");
            }

        }
    }

以上。

猜你喜欢

转载自blog.csdn.net/yonghuming_jesse/article/details/80907802