android---自定义数字选择器

继承了线性布局的控件,里面放置两个按钮和一个文本框,里面设置一个接口,用于监听数值是否变更,设置了数值的上限和下限,可以通过xml去设置上限和下限。


注册属性值的办法:

TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.NumSize);
MAX = array.getInt(R.styleable.NumSize_Max, 10);
MIN = array.getInt(R.styleable.NumSize_Min, 1);

在res/values中新增一个attrs.xml 
 
<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <declare-styleable name="NumSize">  
        <attr name="Max" format="integer" />  
        <attr name="Min" format="integer" />  
    </declare-styleable>  
</resources>   


Caculate.java

package com.example.caculate;

import android.R.interpolator;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Caculate extends LinearLayout{

//数量的上限和下限
private int MAX;
private int MIN;
private Button viewLeft;
private TextView txNum;
private Button viewRight;
private int mNum = 0;         //物品的数量
private OnTextChangerListener mChangerListener;
public void setOnTextChangerlisten(OnTextChangerListener changerListener)
{
this.mChangerListener = changerListener;
}
public Caculate(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
setOrientation(LinearLayout.HORIZONTAL);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.NumSize);
MAX = array.getInt(R.styleable.NumSize_Max, 10);
MIN = array.getInt(R.styleable.NumSize_Min, 1);
mNum = MIN;
//创建左边的按钮
viewLeft = new Button(context);
viewLeft.setText("-");
viewLeft.setBackgroundResource(R.drawable.left_btn_bg);
addView(viewLeft,new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//创建中间的文本
txNum = new TextView(context);
txNum.setBackgroundResource(R.drawable.center_text);
   txNum.setText(mNum+"");
viewRight = new Button(context);
viewRight.setText("+");
addView(txNum);
//创建右边的按钮 
viewRight.setEnabled(true);
viewRight.setBackgroundResource(R.drawable.right_btn_bg);
addView(viewRight,new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//设置点击事件
viewLeft.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
if(mNum>MIN)
{
mNum--;
txNum.setText(mNum+"");
mChangerListener.OnTextChange(mNum);
}
}
});
viewRight.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
if(mNum<MAX)
{
mNum++;
txNum.setText(mNum+"");
mChangerListener.OnTextChange(mNum);
}
}
});
}

public Caculate(Context context) {
super(context);
// TODO Auto-generated constructor stub
}


@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
super.onLayout(changed, l, t, r, b);
if(changed)
{
viewLeft.setLayoutParams(new LinearLayout.LayoutParams((r-l)/3, 
LinearLayout.LayoutParams.MATCH_PARENT));
txNum.setLayoutParams(new LinearLayout.LayoutParams((r-l)/3, 
LinearLayout.LayoutParams.MATCH_PARENT));
txNum.setGravity(Gravity.CENTER);
txNum.setTextSize(20);
viewRight.setLayoutParams(new LinearLayout.LayoutParams((r-l)/3, 
LinearLayout.LayoutParams.MATCH_PARENT));
}
}

public interface OnTextChangerListener{
public void OnTextChange(int Num);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}

activity_main.xml

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.example.caculate.Caculate
        android:id="@+id/caculate"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:layout_centerInParent="true"
        test:Max ="10"
        test:Min="2"/>

</RelativeLayout>

drawable文件夹下设置了按钮形状背景

 center_text.xml
<shape 
    <solid android:color="#ffffff"/>
    <stroke android:width="0.1dp"
        android:color="#bdbdbd"/>
</shape>

 left_btn_bg.xml

 
<selector 
    
    <item android:state_pressed="true"
        android:drawable="@drawable/left_btn_select"></item>
    <item android:drawable="@drawable/left_btn_normal"></item>
</selector>

left_ btn_normal.xml

<shape 
    <corners android:topLeftRadius="3dp"
        android:bottomLeftRadius="3dp"/>
    <solid android:color="#ffffff"/>
    <stroke android:width="0.1dp"
        android:color="#bdbdbd"/>
</shape>
 
 left_btn_select.xml

 
<shape 
    <corners android:topLeftRadius="3dp"
        android:bottomLeftRadius="3dp"/>
    <solid android:color="#bdbdbd"/>
    <stroke android:width="0.1dp"
        android:color="#bdbdbd"/>
</shape>

right_btn_bg.xml

 
<selector 
    
    <item android:state_pressed="true"
        android:drawable="@drawable/right_btn_select"></item>
    <item android:drawable="@drawable/right_btn_normal"></item>
</selector>

right_btn_normal.xml
 
<shape 
    <corners android:topRightRadius="3dp"
        android:bottomRightRadius="3dp"/>
    <solid android:color="#ffffff"/>
    <stroke android:width="0.1dp"
        android:color="#bdbdbd"/>
</shape>

right_btn_select.xml

 
<shape 
    <corners android:topRightRadius="3dp"
        android:bottomRightRadius="3dp"/>
    <solid android:color="#bdbdbd"/>
    <stroke android:width="0.1dp"
        android:color="#bdbdbd"/>
</shape>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值