仿闲鱼键盘的思路

苦于找不到Android源码的仿闲鱼键盘也就可能自己手动实现了。
1、首先是布局文件:
<?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"
    android:background="@color/white"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingLeft="10dp"
        android:paddingRight="10dp" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="价格"
                android:textSize="13sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:gravity="center"
                android:text="¥"
                android:textColor="@android:color/darker_gray" />

            <EditText
                android:maxLength="11"
                android:id="@+id/et_goodsPrice"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="3"
                android:background="@null"
                android:hint="0.00"
                android:inputType="numberDecimal"
                android:paddingLeft="3dp"
                android:textColor="@android:color/holo_blue_dark"
                android:textSize="13sp" />
        </LinearLayout>

        <View
            android:layout_width="0.5dp"
            android:layout_height="fill_parent"
            android:background="@android:color/darker_gray" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="原价"
                android:textSize="13sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:gravity="center"
                android:text="¥"
                android:textColor="@android:color/darker_gray" />

            <EditText
                android:maxLength="11"
                android:id="@+id/et_originalPrice"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="3"
                android:background="@null"
                android:gravity="center_vertical"
                android:hint="0.00"
                android:inputType="numberDecimal"
                android:paddingLeft="3dp"
                android:textColor="@android:color/holo_blue_dark"
                android:textSize="13sp" />
        </LinearLayout>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.1dp"
        android:background="@android:color/darker_gray" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:visibility="gone" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="运费"
                android:textSize="13sp" />

            <EditText
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="3"
                android:background="@null"
                android:hint="0.00"
                android:textColor="@android:color/holo_blue_dark"
                android:textSize="13sp" />
        </LinearLayout>

        <View
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:background="#ffffff" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:gravity="center"
                android:text="原价"
                android:textSize="13sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_marginLeft="5dp"
                android:background="@null"
                android:gravity="center"
                android:text="按距离计算运费"
                android:textColor="@android:color/darker_gray"
                android:textSize="12sp" />
        </LinearLayout>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@android:color/darker_gray" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="240dp"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/tv_softkb_1"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="1"
                    android:textSize="16sp" />

                <View
                    android:layout_width="0.5dp"
                    android:layout_height="fill_parent"
                    android:background="@android:color/darker_gray" />

                <TextView
                    android:id="@+id/tv_softkb_2"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="2"
                    android:textSize="16sp" />

                <View
                    android:layout_width="0.5dp"
                    android:layout_height="fill_parent"
                    android:background="@android:color/darker_gray" />

                <TextView
                    android:id="@+id/tv_softkb_3"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="3"
                    android:textSize="16sp" />

                <View
                    android:layout_width="0.5dp"
                    android:layout_height="fill_parent"
                    android:background="@android:color/darker_gray" />
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:background="@android:color/darker_gray" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/tv_softkb_4"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="4"
                    android:textSize="16sp" />

                <View
                    android:layout_width="0.5dp"
                    android:layout_height="fill_parent"
                    android:background="@android:color/darker_gray" />

                <TextView
                    android:id="@+id/tv_softkb_5"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="5"
                    android:textSize="16sp" />

                <View
                    android:layout_width="0.5dp"
                    android:layout_height="fill_parent"
                    android:background="@android:color/darker_gray" />

                <TextView
                    android:id="@+id/tv_softkb_6"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="6"
                    android:textSize="16sp" />

                <View
                    android:layout_width="0.5dp"
                    android:layout_height="fill_parent"
                    android:background="@android:color/darker_gray" />
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:background="@android:color/darker_gray" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/tv_softkb_7"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="7"
                    android:textSize="16sp" />

                <View
                    android:layout_width="0.5dp"
                    android:layout_height="fill_parent"
                    android:background="@android:color/darker_gray" />

                <TextView
                    android:id="@+id/tv_softkb_8"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="8"
                    android:textSize="16sp" />

                <View
                    android:layout_width="0.5dp"
                    android:layout_height="fill_parent"
                    android:background="@android:color/darker_gray" />

                <TextView
                    android:id="@+id/tv_softkb_9"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="9"
                    android:textSize="16sp" />

                <View
                    android:layout_width="0.5dp"
                    android:layout_height="fill_parent"
                    android:background="@android:color/darker_gray" />
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:background="@android:color/darker_gray" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/tv_softkb_0"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="0"
                    android:textSize="16sp" />

                <View
                    android:layout_width="0.5dp"
                    android:layout_height="fill_parent"
                    android:background="@android:color/darker_gray" />

                <TextView
                    android:id="@+id/tv_softkb_point"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="8dp"
                    android:text="."
                    android:textSize="30sp" />

                <View
                    android:layout_width="0.5dp"
                    android:layout_height="fill_parent"
                    android:background="@android:color/darker_gray" />

                <ImageView
                    android:scaleType="centerInside"
                    android:clickable="true"
                    android:longClickable="true"
                    android:id="@+id/tv_softkb_hiddenkb"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:src="@drawable/keyboard2x"
                    android:textSize="14sp" />

                <View
                    android:layout_width="0.5dp"
                    android:layout_height="fill_parent"
                    android:background="@android:color/darker_gray" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1.045"
            android:orientation="vertical" >

            <ImageView
                android:scaleType="centerInside"
                android:src="@drawable/delete2x"
                android:longClickable="true"
                android:clickable="true"
                android:id="@+id/iv_delete"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/tv_confirmKB"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1.01"
                android:background="#0C85FE"
                android:gravity="center"
                android:text="确认"
                android:textColor="@color/color_ffffff" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
2、用了最笨的方法,也不是第一个键就添加了点击事件 , 在外部定义StringBuffer 用户点击一次添加一次,同时设给edittext设置一次代码如下(之前要讲一下 我们要先判断两个edittext是否获取了焦点 如果获取了焦点 说明当前edittext 是可以输入的状态 如下图 红色标注):

这里写图片描述
代码如下:

/**
     * 输入金额
     * 
     * @param buffer
     * @param buffer2
     * @param etGoodsPrice
     * @param etOriginalPrice
     */
    private void setNumber(final StringBuffer buffer, final StringBuffer buffer2, final EditText etGoodsPrice,
            final EditText etOriginalPrice) {

        TextView tv_softkb_1 = (TextView) editPriceView.findViewById(R.id.tv_softkb_1);
        tv_softkb_1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (etGoodsPrice.hasFocus()) {
                    buffer.append("1");
                    etGoodsPrice.setText(buffer.toString());
                }
                if (etOriginalPrice.hasFocus()) {

                    buffer2.append("1");
                    etOriginalPrice.setText(buffer2.toString());
                }

            }
        });
        TextView tv_softkb_2 = (TextView) editPriceView.findViewById(R.id.tv_softkb_2);
        tv_softkb_2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (etGoodsPrice.hasFocus()) {
                    buffer.append("2");
                    etGoodsPrice.setText(buffer.toString());
                }
                if (etOriginalPrice.hasFocus()) {

                    buffer2.append("2");
                    etOriginalPrice.setText(buffer2.toString());
                }

            }
        });
        TextView tv_softkb_3 = (TextView) editPriceView.findViewById(R.id.tv_softkb_3);
        tv_softkb_3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (etGoodsPrice.hasFocus()) {
                    buffer.append("3");
                    etGoodsPrice.setText(buffer.toString());
                }
                if (etOriginalPrice.hasFocus()) {

                    buffer2.append("3");
                    etOriginalPrice.setText(buffer2.toString());
                }

            }
        });
        TextView tv_softkb_4 = (TextView) editPriceView.findViewById(R.id.tv_softkb_4);
        tv_softkb_4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (etGoodsPrice.hasFocus()) {
                    buffer.append("4");
                    etGoodsPrice.setText(buffer.toString());
                }
                if (etOriginalPrice.hasFocus()) {

                    buffer2.append("4");
                    etOriginalPrice.setText(buffer2.toString());
                }

            }
        });
        TextView tv_softkb_5 = (TextView) editPriceView.findViewById(R.id.tv_softkb_5);
        tv_softkb_5.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (etGoodsPrice.hasFocus()) {
                    buffer.append("5");
                    etGoodsPrice.setText(buffer.toString());
                }
                if (etOriginalPrice.hasFocus()) {

                    buffer2.append("5");
                    etOriginalPrice.setText(buffer2.toString());
                }

            }
        });
        TextView tv_softkb_6 = (TextView) editPriceView.findViewById(R.id.tv_softkb_6);
        tv_softkb_6.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (etGoodsPrice.hasFocus()) {
                    buffer.append("6");
                    etGoodsPrice.setText(buffer.toString());
                }
                if (etOriginalPrice.hasFocus()) {

                    buffer2.append("6");
                    etOriginalPrice.setText(buffer2.toString());
                }

            }
        });
        TextView tv_softkb_7 = (TextView) editPriceView.findViewById(R.id.tv_softkb_7);
        tv_softkb_7.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (etGoodsPrice.hasFocus()) {
                    buffer.append("7");
                    etGoodsPrice.setText(buffer.toString());
                }
                if (etOriginalPrice.hasFocus()) {

                    buffer2.append("7");
                    etOriginalPrice.setText(buffer2.toString());
                }

            }
        });
        TextView tv_softkb_8 = (TextView) editPriceView.findViewById(R.id.tv_softkb_8);
        tv_softkb_8.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (etGoodsPrice.hasFocus()) {
                    buffer.append("8");
                    etGoodsPrice.setText(buffer.toString());
                }
                if (etOriginalPrice.hasFocus()) {

                    buffer2.append("8");
                    etOriginalPrice.setText(buffer2.toString());
                }

            }
        });
        TextView tv_softkb_9 = (TextView) editPriceView.findViewById(R.id.tv_softkb_9);
        tv_softkb_9.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (etGoodsPrice.hasFocus()) {
                    buffer.append("9");
                    etGoodsPrice.setText(buffer.toString());
                }
                if (etOriginalPrice.hasFocus()) {

                    buffer2.append("9");
                    etOriginalPrice.setText(buffer2.toString());
                }

            }
        });
        TextView tv_softkb_0 = (TextView) editPriceView.findViewById(R.id.tv_softkb_0);
        tv_softkb_0.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (etGoodsPrice.hasFocus()) {
                    buffer.append("0");
                    etGoodsPrice.setText(buffer.toString());
                }
                if (etOriginalPrice.hasFocus()) {

                    buffer2.append("0");
                    etOriginalPrice.setText(buffer2.toString());
                }

            }
        });

        tv_softkb_point.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (etGoodsPrice.hasFocus()) {
                    buffer.append(".");
                    etGoodsPrice.setText(buffer.toString());
                }
                if (etOriginalPrice.hasFocus()) {

                    buffer2.append(".");
                    etOriginalPrice.setText(buffer2.toString());
                }

            }
        });
    }

大家一定看到了这4个参数:
/**
* 输入金额
*
* @param buffer
* @param buffer2
* @param etGoodsPrice
* @param etOriginalPrice
*/
其中buffer 为了在用户点击键盘的时候把 键盘上的值appdend(“”);上去 然后设置给edittext 前提 edittext hasfocued ; buffer2 同buffer 的作用只不过它存储的是 editext2 的值。以上只是字面值 0 1 2 3 4 5 6 7 8 9 . 0 这些值可以设置到edittext.

删除键的设置:
删除键的点击事件 :

ImageView ivDelete = (ImageView) editPriceView.findViewById(R.id.iv_delete);

        ivDelete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // Toast.makeText(PostIssueActivity.this, "dddd", 0).show();

                delNumber();
            }

        });

删除方法实现:

    /**
     * 删除输入的金额
     */

    protected void delNumber() {
        if (etGoodsPrice.hasFocus()) {
            // buffer.delete(0, etGoodsPrice.getText().length());
            if (etGoodsPrice.getText().length() >= 1) {
                buffer.deleteCharAt(etGoodsPrice.getText().length() - 1);
                // buffer.deleteCharAt(etGoodsPrice.getText().length());
            }
            stringbuffer = buffer.toString();
            etGoodsPrice.setText(stringbuffer.substring(0, stringbuffer.length()));
            etGoodsPrice.setSelection(etGoodsPrice.length());

        }
        if (etOriginalPrice.hasFocus()) {
            // buffer2.delete(0,etOriginalPrice.getText().length());
            if (etOriginalPrice.getText().length() >= 1) {
                buffer2.deleteCharAt(etOriginalPrice.getText().length() - 1);
                // buffer2.deleteCharAt(etOriginalPrice.getText().length());
            }
            stringbuffer2 = buffer2.toString();
            etOriginalPrice.setText(stringbuffer2.substring(0, stringbuffer2.length()));
            etOriginalPrice.setSelection(etOriginalPrice.length());
        }
    }

你一定看这段代码: 从字符串最后个开始删除
buffer2.deleteCharAt(etOriginalPrice.getText().length() - 1);
buffer.deleteCharAt(etGoodsPrice.getText().length() - 1);

/
**删除之后设置数值
/
etGoodsPrice.setText(stringbuffer.substring(0, stringbuffer.length()));

/
*
**删除之后 让光标紧跟着 最后一个字符
/
        etGoodsPrice.setSelection(etGoodsPrice.length());
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值