继承了线性布局的控件,里面放置两个按钮和一个文本框,里面设置一个接口,用于监听数值是否变更,设置了数值的上限和下限,可以通过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
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:test ="http://schemas.android.com/apk/res/com.example.caculate"
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>
center_text.xml
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff"/>
<stroke android:width="0.1dp"
android:color="#bdbdbd"/>
</shape>
left_btn_bg.xml <selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/left_btn_select"></item>
<item android:drawable="@drawable/left_btn_normal"></item>
</selector>
left_btn_select.xmlleft_ btn_normal.xml
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:topLeftRadius="3dp"
android:bottomLeftRadius="3dp"/>
<solid android:color="#ffffff"/>
<stroke android:width="0.1dp"
android:color="#bdbdbd"/>
</shape>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<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
xmlns:android="http://schemas.android.com/apk/res/android">
<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
xmlns:android="http://schemas.android.com/apk/res/android">
<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
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:topRightRadius="3dp"
android:bottomRightRadius="3dp"/>
<solid android:color="#bdbdbd"/>
<stroke android:width="0.1dp"
android:color="#bdbdbd"/>
</shape>
|
android---自定义数字选择器
最新推荐文章于 2024-05-30 09:48:00 发布