亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

自定義控件實現一個UpDownView(1)

系統 1940 0
先看下什么叫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 = "############.##"就知道了。該控件適合對金額的顯示。
用到的兩張圖:




自定義控件實現一個UpDownView(1)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!?。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 热99re久久精品这里都是免费 | 久久久久久国产精品mv | 私人影院在线免费观看 | 日本亚洲高清 | 国产a区| 免费香蕉一区二区在线观看 | 美女久久久久 | 91精品全国免费观看老司机 | 99视频国产热精品视频 | 操日日 | 亚洲精品国产福利一区二区三区 | 亚洲啊v| 国产成a人亚洲精v品久久网 | 精品国产视频在线观看 | 破外女出血一级毛片 | 国产成人99精品免费视频麻豆 | 国产精品一区二区三区久久 | 国产亚洲精品美女久久久 | 色偷偷久久| 99精品久久99久久久久久 | 日本欧美一区二区三区不卡视频 | 性成人动作片在线看 | a级毛片高清免费视频 | 天天拍夜夜添久久精品免费 | 末成年娇小性色xxxxx视频 | 欧美刺激午夜性久久久久久久 | 久草国产视频 | 色视频在线看 | 五月婷婷亚洲综合 | 国产成+人+综合+亚洲专 | 久久99精品一级毛片 | 亚洲欧美综合一区 | 操操操干干 | 亚洲视频精选 | 全亚洲最大的免费私人影剧院 | 摸一摸操一操 | 狠狠色婷婷综合天天久久丁香 | 国产美女久久久亚洲 | 91手机看片国产福利精品 | 亚洲视频二区 | 天天久久综合网站 |