自定义控件实现一个UpDownView(1)

先看下什么叫UpDownView:


这就叫UpDownView,:P

先看代码:

package com.ql.view;

import java.text.DecimalFormat;

import com.ql.app.R;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class UpDownView extends LinearLayout {
	private final static String tag="UpDownView";
	private ImageView iv_minus,iv_plus;
	private EditText et_input;
	
	public UpDownView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		Log.i(tag, "UpDownView1");
	}
	public UpDownView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		Log.i(tag, "UpDownView2");
	}
	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
		Log.i(tag, "onFinishInflate");
		View view=LayoutInflater.from(getContext()).inflate(R.layout.up_down_view, this);
		iv_minus=(ImageView)view.findViewById(R.id.iv_minus);
		iv_plus=(ImageView)view.findViewById(R.id.iv_plus);
		et_input=(EditText)view.findViewById(R.id.et_input);
		
		iv_minus.setOnClickListener(listener);
		iv_plus.setOnClickListener(listener);
	}
	
	OnClickListener listener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.iv_minus:
				doMinus();
				break;
			case R.id.iv_plus:
				doPlus();
				break;

			default:
				break;
			}
		}
	};
	
	private double number;//当前量
	private double step=1000;//步长,每次加减的量
	private double max=10000;//最大量
	private double min=-10000;//最小量
	
	private void doPlus(){
		String temp=et_input.getText().toString();
		if(temp.length()==0){
			number=0;
		}else{
			number=Double.parseDouble(temp);
		}
		number+=step;
		if(number>max){
			number=max;
		}
		et_input.setText(convertNumberToString(number,pattern));
	}
	
	private void doMinus(){
		String temp=et_input.getText().toString();
		if(temp.length()==0){
			number=0;
		}else{
			number=Double.parseDouble(temp);
		}
		number-=step;
		if(number<min){
			number=min;
		}
		et_input.setText(convertNumberToString(number,pattern));
	}
	
	private String pattern = "############.##";
	/**
	 * Util
	 * @param value
	 * @param pattern
	 * @return
	 */
	public static String convertNumberToString(Number value, String pattern) {
		try {
			DecimalFormat decimalFormat = new DecimalFormat(pattern);
			return decimalFormat.format(value);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public double getNumber() {
		return number;
	}
	public void setNumber(double number) {
                et_input.setText(convertNumberToString(number,pattern));
		this.number = number;
	}
	public double getStep() {
		return step;
	}
	public void setStep(double step) {
		this.step = step;
	}
	public double getMax() {
		return max;
	}
	public void setMax(double max) {
		this.max = max;
	}
	public double getMin() {
		return min;
	}
	public void setMin(double min) {
		this.min = min;
	}
}


其实挺简单的,只要在onFinishInflate中加载一个布局就可以了。
布局up_down_view.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
	android:layout_gravity="center_vertical"
	android:orientation="horizontal"
    >
    <ImageView android:id="@+id/iv_minus"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:layout_gravity="center_vertical"
	android:src="@drawable/sh_trade_minus"
	android:scaleType="fitCenter"
	/>
	<EditText  android:id="@+id/et_input"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:layout_weight="1"
	android:text=""
	android:singleLine="true"
	android:inputType="numberDecimal"
	/>
	<ImageView android:id="@+id/iv_plus"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:layout_gravity="center_vertical"
	android:src="@drawable/sh_trade_plus"
	android:scaleType="fitCenter"
	/>
</LinearLayout>


使用:
在布局文件中引入:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <com.ql.view.UpDownView android:id="@+id/upDownView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    />
</LinearLayout>

然后可以通过UpDownView提供的setter/getter函数对它进行初始化需要的数据了。

UpDownView view=(UpDownView)findViewById(R.id.upDownView);
view.setMax(100);
view.setStep(1);
view.setMin(0);
view.setNumber(90.01);//没有的话显示为""

此控件支持小数,看源码中pattern = "############.##"就知道了。该控件适合对金额的显示。
用到的两张图:





https://github.com/saiwu-bigkoo/Android-SnappingStepper
  • 大小: 18.7 KB
  • 大小: 671 Bytes
  • 大小: 671 Bytes
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值