自定义带加减的EditText

我想大多数朋友在做商城购物类APP时,肯定遇到过需要做**带加减的EdiText**。那么今天呢?就给大家介绍一款很简单通熟易懂的自定义EditText。废话不说了,直接上代码:

首先是布局文件:

<LinearLayout
    android:id="@+id/ll_one"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    android:background="#cccccc"
    android:layout_marginTop="100dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    >
    <TextView
        android:id="@+id/add"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp"
        android:layout_marginRight="2dp"
        android:layout_weight="1"
        android:background="#ffffff"
        android:gravity="center"
        android:text="+"
        android:textSize="30sp"
        />
    <EditText
        android:id="@+id/result"
        android:background="#ffffff"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:padding="1dp"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp"
        android:layout_weight="2"
        android:numeric="integer"
        android:gravity="center"
        android:maxLength="8"
        android:textSize="18sp"
        android:text=""
        android:singleLine="true"
        />
    <TextView
        android:id="@+id/minus"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp"
        android:layout_marginRight="2dp"
        android:layout_weight="1"
        android:background="#ffffff"
        android:gravity="center"
        android:text="-"
        android:textSize="30sp"
        />
</LinearLayout>

从上面的布局可以看出,外面在线性布局我直接把背景设置成了灰色的,然后边上的两个都是text,背景设置成白色然后上下左右都距离1dp,那样一个方框就出来了,中间的是一个EditText,格局权重,三个控件各占一些。

效果图如下:


其实大家也可以用自己喜欢的图片做背景,只需修改父控件的背景图片即可,下面可以看下我放的带图片的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.candyqq.com.qqedittext.MainActivity">

    <LinearLayout
        android:id="@+id/ll_one"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:background="#cccccc"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        >
        <TextView
            android:id="@+id/add"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="2dp"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:background="#ffffff"
            android:gravity="center"
            android:text="+"
            android:textSize="30sp"
            />
        <EditText
            android:id="@+id/result"
            android:background="#ffffff"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="1dp"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="2dp"
            android:layout_weight="2"
            android:numeric="integer"
            android:gravity="center"
            android:maxLength="8"
            android:textSize="18sp"
            android:text=""
            android:singleLine="true"
            />
        <TextView
            android:id="@+id/minus"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="2dp"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:background="#ffffff"
            android:gravity="center"
            android:text="-"
            android:textSize="30sp"
            />
    </LinearLayout>

    <LinearLayout

        android:id="@+id/ll_one_two"
        android:layout_below="@+id/ll_one"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:background="@drawable/shape_corners_button_pressed3"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        >
        <TextView
            android:id="@+id/add_two"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="2dp"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:background="@mipmap/plus32"
            android:gravity="center"
            android:text="+"
            />
        <EditText
            android:id="@+id/result_two"
            android:background="#ffffff"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:padding="1dp"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="2dp"
            android:layout_weight="2"
            android:numeric="integer"
            android:gravity="center"
            android:maxLength="8"
            android:textSize="18sp"
            android:text=""
            android:singleLine="true"
            />
        <TextView
            android:id="@+id/minus_two"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="2dp"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:background="@mipmap/jiann"
            android:gravity="center"
            android:text="-"
            />
    </LinearLayout>

</RelativeLayout>

整体的效果图就如下:![as上面的效果图](https://img-blog.csdn.net/20160607143921331)

好了大概借介绍到这里了,MainActivity里面关于加减的相关逻辑我都做了处理,一般的都可以直接拿去用的


最下面给大家Demo下载链接:http://download.csdn.net/detail/qq_31850557/9543603

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值