实现基于PopupWindow的数字软键盘

威哥工作中,需要再EditText文本框下展示数字软键盘,用于输入数字。效果图如下:

威哥从来都是一个直来直去的人,直接上代码实现,大家借鉴借鉴,自行修改。

1.xml布局popup_number.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="@drawable/phoneloginright"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_marginTop="10dp"
            android:layout_height="75dp"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_NumberPopup_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:background="@drawable/numbackground"
                android:gravity="center"
                android:text="1"
                android:textColor="@color/systemBlack"
                android:textSize="20pt" />

            <TextView
                android:id="@+id/tv_NumberPopup_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_marginLeft="25dp"
                android:background="@drawable/numbackground"
                android:text="2"
                android:textColor="@color/systemBlack"
                android:textSize="20pt" />

            <TextView
                android:id="@+id/tv_NumberPopup_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_marginLeft="25dp"
                android:background="@drawable/numbackground"
                android:text="3"
                android:textColor="@color/systemBlack"
                android:textSize="20pt" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_NumberPopup_4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:background="@drawable/numbackground"
                android:gravity="center"
                android:text="4"
                android:textColor="@color/systemBlack"
                android:textSize="20pt" />

            <TextView
                android:id="@+id/tv_NumberPopup_5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_marginLeft="25dp"
                android:background="@drawable/numbackground"
                android:text="5"
                android:textColor="@color/systemBlack"
                android:textSize="20pt" />

            <TextView
                android:id="@+id/tv_NumberPopup_6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_marginLeft="25dp"
                android:background="@drawable/numbackground"
                android:text="6"
                android:textColor="@color/systemBlack"
                android:textSize="20pt" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_NumberPopup_7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:background="@drawable/numbackground"
                android:gravity="center"
                android:text="7"
                android:textColor="@color/systemBlack"
                android:textSize="20pt" />

            <TextView
                android:id="@+id/tv_NumberPopup_8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_marginLeft="25dp"
                android:background="@drawable/numbackground"
                android:text="8"
                android:textColor="@color/systemBlack"
                android:textSize="20pt" />

            <TextView
                android:id="@+id/tv_NumberPopup_9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_marginLeft="25dp"
                android:background="@drawable/numbackground"
                android:text="9"
                android:textColor="@color/systemBlack"
                android:textSize="20pt" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:gravity="center"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/iv_NumberPopup_Del"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_alignParentRight="true"
                android:background="@color/systemNoColor"
                android:scaleType="fitXY"
                android:src="@drawable/numdel" />
            <TextView
                android:id="@+id/tv_NumberPopup_0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_marginLeft="25dp"
                android:background="@drawable/numbackground"
                android:text="0"
                android:textColor="@color/systemBlack"
                android:textSize="20pt" />

            <ImageView
                android:id="@+id/iv_NumberPopup_Sure"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="25dp"
                android:layout_alignParentRight="true"
                android:background="@color/systemNoColor"
                android:scaleType="fitXY"
                android:src="@drawable/numsure" />


        </LinearLayout>

    </LinearLayout>
</LinearLayout>

2、软键盘实现:

package com.msh.mshselftone.framedialog;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.msh.mshselftone.R;
import com.msh.mshselftone.dto.ProductPrice.ProductPriceDto;
import com.msh.mshselftone.utils.MessageBox;
import com.msh.mshselftone.utils.StringPlus;

/**
 * 数组键盘
 * Author:William(徐威)
 * Create Time:2018-10-18
 */
public class NumberPopup extends PopupWindow implements OnClickListener {
    //全局变量
    private View parent;

    private ImageView iv_NumberPopup_Del;   //删除数字按钮
    private ImageView iv_NumberPopup_Sure;    //确认按钮
    //数组按钮
    private TextView tv_NumberPopup_1;
    private TextView tv_NumberPopup_2;
    private TextView tv_NumberPopup_3;
    private TextView tv_NumberPopup_4;
    private TextView tv_NumberPopup_5;
    private TextView tv_NumberPopup_6;
    private TextView tv_NumberPopup_7;
    private TextView tv_NumberPopup_8;
    private TextView tv_NumberPopup_9;
    private TextView tv_NumberPopup_0;

    /**
     * 构造函数
     * Author:William(徐威)
     * Create Time:2018-10-18
     *
     * @param context
     */
    public NumberPopup(Context context, View parent, int windowWidth, int windowHeight) {
        super(context);
        this.parent = parent;

        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = layoutInflater.inflate(R.layout.popup_number, null);
        setContentView(contentView);

        //set width and height
        if (windowWidth == 0) {
            windowWidth = 500;
        }
        if (windowHeight == 0) {
            windowHeight = 490;
        }
        setWidth(windowWidth);
        setHeight(windowHeight);

        setOutsideTouchable(true); //
        setFocusable(false); // Not allow to dismiss PopupWindow by touching outside
        setTouchable(true);
        setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });

        initView(contentView);/**/
    }

    /**
     * 初始化视图
     * Author:William(徐威)
     * Create Time:2018-10-18
     *
     * @param view
     */
    private void initView(View view) {
        //获取数组按钮
        tv_NumberPopup_1 = view.findViewById(R.id.tv_NumberPopup_1);
        tv_NumberPopup_2 = view.findViewById(R.id.tv_NumberPopup_2);
        tv_NumberPopup_3 = view.findViewById(R.id.tv_NumberPopup_3);
        tv_NumberPopup_4 = view.findViewById(R.id.tv_NumberPopup_4);
        tv_NumberPopup_5 = view.findViewById(R.id.tv_NumberPopup_5);
        tv_NumberPopup_6 = view.findViewById(R.id.tv_NumberPopup_6);
        tv_NumberPopup_7 = view.findViewById(R.id.tv_NumberPopup_7);
        tv_NumberPopup_8 = view.findViewById(R.id.tv_NumberPopup_8);
        tv_NumberPopup_9 = view.findViewById(R.id.tv_NumberPopup_9);
        tv_NumberPopup_0 = view.findViewById(R.id.tv_NumberPopup_0);
        iv_NumberPopup_Del = view.findViewById(R.id.iv_NumberPopup_Del);
        iv_NumberPopup_Sure = view.findViewById(R.id.iv_NumberPopup_Sure);
        tv_NumberPopup_1.setOnClickListener(this);
        tv_NumberPopup_2.setOnClickListener(this);
        tv_NumberPopup_3.setOnClickListener(this);
        tv_NumberPopup_4.setOnClickListener(this);
        tv_NumberPopup_5.setOnClickListener(this);
        tv_NumberPopup_6.setOnClickListener(this);
        tv_NumberPopup_7.setOnClickListener(this);
        tv_NumberPopup_8.setOnClickListener(this);
        tv_NumberPopup_9.setOnClickListener(this);
        tv_NumberPopup_0.setOnClickListener(this);
        iv_NumberPopup_Del.setOnClickListener(this);
        iv_NumberPopup_Sure.setOnClickListener(this);
    }

    public void show() {
        // Show at bottom of parent
        this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        Log.d("DialogTest", "ShowDialog");
    }

    /**
     * 点击事件监听
     * Author:William(徐威)
     * Create Time:2018-10-18
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_NumberPopup_1:
                if (listener != null) {
                    listener.onChooseNum(tv_NumberPopup_1.getText().toString());
                }
                break;
            case R.id.tv_NumberPopup_2:
                if (listener != null) {
                    listener.onChooseNum(tv_NumberPopup_2.getText().toString());
                }
                break;
            case R.id.tv_NumberPopup_3:
                if (listener != null) {
                    listener.onChooseNum(tv_NumberPopup_3.getText().toString());
                }
                break;
            case R.id.tv_NumberPopup_4:
                if (listener != null) {
                    listener.onChooseNum(tv_NumberPopup_4.getText().toString());
                }
                break;
            case R.id.tv_NumberPopup_5:
                if (listener != null) {
                    listener.onChooseNum(tv_NumberPopup_5.getText().toString());
                }
                break;
            case R.id.tv_NumberPopup_6:
                if (listener != null) {
                    listener.onChooseNum(tv_NumberPopup_6.getText().toString());
                }
                break;
            case R.id.tv_NumberPopup_7:
                if (listener != null) {
                    listener.onChooseNum(tv_NumberPopup_7.getText().toString());
                }
                break;
            case R.id.tv_NumberPopup_8:
                if (listener != null) {
                    listener.onChooseNum(tv_NumberPopup_8.getText().toString());
                }
                break;
            case R.id.tv_NumberPopup_9:
                if (listener != null) {
                    listener.onChooseNum(tv_NumberPopup_9.getText().toString());
                }
                break;
            case R.id.tv_NumberPopup_0:
                if (listener != null) {
                    listener.onChooseNum(tv_NumberPopup_0.getText().toString());
                }
                break;
            case R.id.iv_NumberPopup_Del:  //删除按钮
                if (listener != null) {
                    listener.onDelNum();
                }
                break;
            case R.id.iv_NumberPopup_Sure:
                if (listener != null) {
                    listener.onSureNum();
                    dismiss();
                }
                break;
            default:
                break;
        }
    }

    private NumberPopup.AddListener listener = null;

    public void setListener(NumberPopup.AddListener listener) {
        this.listener = listener;
    }

    /**
     * 申明监听接口
     * Author:William(徐威)
     * Create Time:2018-10-18
     */
    public interface AddListener {
        //选择数字事件
        void onChooseNum(String strNum);

        //删除数字事件
        void onDelNum();

        //确认数字事件
        void onSureNum();
    }
}

3、Activity中使用:

private EditText etxt_ProductCart_SearchProduct;    //  商品搜索框
private NumberPopup popWindow;    //数组弹框对象
etxt_ProductCart_SearchProduct = this.findViewById(R.id.etxt_ProductCart_SearchProduct);
etxt_ProductCart_SearchProduct.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (popWindow != null) {
            popWindow.dismiss();
            popWindow = null;
        }
        popWindow = new NumberPopup(ProductCartActivity.this, mView, 0, 0);
        popWindow.showAtLocation(view, Gravity.TOP | Gravity.RIGHT, 0, 155);
        popWindow.setListener(new NumberPopup.AddListener() {
            /**
             * 重写数组选择事件
             * Author:William(徐威)
             * Create Time:2018-10-18
             * @param strNum
             */
            @Override
            public void onChooseNum(String strNum) {
                String strProductId = etxt_ProductCart_SearchProduct.getText().toString();
                etxt_ProductCart_SearchProduct.setText(String.format("%s%s", strProductId, strNum));
                returnOld();
            }

            /**
             * 重写数字删除事件
             * Author:William(徐威)
             * Create Time:2018-10-18
             */
            @Override
            public void onDelNum() {
                String strProductId = etxt_ProductCart_SearchProduct.getText().toString();
                if(!TextUtils.isEmpty(strProductId)){
                    etxt_ProductCart_SearchProduct.setText(strProductId.substring(0, strProductId.length() - 1));
                }
                returnOld();
            }

            @Override
            public void onSureNum() {
                searProduct();
            }
        });
        return false;
    }
});

希望大家能够有所帮助。平方的威哥。。。

猜你喜欢

转载自blog.csdn.net/xuwei_net/article/details/83304046
今日推荐