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

  1. package com.ql.view;  
  2.   
  3. import java.text.DecimalFormat;  
  4.   
  5. import com.ql.app.R;  
  6. import android.content.Context;  
  7. import android.util.AttributeSet;  
  8. import android.util.Log;  
  9. import android.view.LayoutInflater;  
  10. import android.view.View;  
  11. import android.widget.EditText;  
  12. import android.widget.ImageView;  
  13. import android.widget.LinearLayout;  
  14.   
  15. public class UpDownView extends LinearLayout {  
  16.     private final static String tag="UpDownView";  
  17.     private ImageView iv_minus,iv_plus;  
  18.     private EditText et_input;  
  19.       
  20.     public UpDownView(Context context) {  
  21.         super(context);  
  22.         // TODO Auto-generated constructor stub  
  23.         Log.i(tag, "UpDownView1");  
  24.     }  
  25.     public UpDownView(Context context, AttributeSet attrs) {  
  26.         super(context, attrs);  
  27.         // TODO Auto-generated constructor stub  
  28.         Log.i(tag, "UpDownView2");  
  29.     }  
  30.     @Override  
  31.     protected void onFinishInflate() {  
  32.         // TODO Auto-generated method stub  
  33.         super.onFinishInflate();  
  34.         Log.i(tag, "onFinishInflate");  
  35.         View view=LayoutInflater.from(getContext()).inflate(R.layout.up_down_view, this);  
  36.         iv_minus=(ImageView)view.findViewById(R.id.iv_minus);  
  37.         iv_plus=(ImageView)view.findViewById(R.id.iv_plus);  
  38.         et_input=(EditText)view.findViewById(R.id.et_input);  
  39.           
  40.         iv_minus.setOnClickListener(listener);  
  41.         iv_plus.setOnClickListener(listener);  
  42.     }  
  43.       
  44.     OnClickListener listener=new OnClickListener() {  
  45.           
  46.         @Override  
  47.         public void onClick(View v) {  
  48.             // TODO Auto-generated method stub  
  49.             switch (v.getId()) {  
  50.             case R.id.iv_minus:  
  51.                 doMinus();  
  52.                 break;  
  53.             case R.id.iv_plus:  
  54.                 doPlus();  
  55.                 break;  
  56.   
  57.             default:  
  58.                 break;  
  59.             }  
  60.         }  
  61.     };  
  62.       
  63.     private double number;//当前量  
  64.     private double step=1000;//步长,每次加减的量  
  65.     private double max=10000;//最大量  
  66.     private double min=-10000;//最小量  
  67.       
  68.     private void doPlus(){  
  69.         String temp=et_input.getText().toString();  
  70.         if(temp.length()==0){  
  71.             number=0;  
  72.         }else{  
  73.             number=Double.parseDouble(temp);  
  74.         }  
  75.         number+=step;  
  76.         if(number>max){  
  77.             number=max;  
  78.         }  
  79.         et_input.setText(convertNumberToString(number,pattern));  
  80.     }  
  81.       
  82.     private void doMinus(){  
  83.         String temp=et_input.getText().toString();  
  84.         if(temp.length()==0){  
  85.             number=0;  
  86.         }else{  
  87.             number=Double.parseDouble(temp);  
  88.         }  
  89.         number-=step;  
  90.         if(number<min){  
  91.             number=min;  
  92.         }  
  93.         et_input.setText(convertNumberToString(number,pattern));  
  94.     }  
  95.       
  96.     private String pattern = "############.##";  
  97.     /** 
  98.      * Util 
  99.      * @param value 
  100.      * @param pattern 
  101.      * @return 
  102.      */  
  103.     public static String convertNumberToString(Number value, String pattern) {  
  104.         try {  
  105.             DecimalFormat decimalFormat = new DecimalFormat(pattern);  
  106.             return decimalFormat.format(value);  
  107.         } catch (Exception e) {  
  108.             e.printStackTrace();  
  109.         }  
  110.         return null;  
  111.     }  
  112.   
  113.     public double getNumber() {  
  114.         return number;  
  115.     }  
  116.     public void setNumber(double number) {  
  117.                 et_input.setText(convertNumberToString(number,pattern));  
  118.         this.number = number;  
  119.     }  
  120.     public double getStep() {  
  121.         return step;  
  122.     }  
  123.     public void setStep(double step) {  
  124.         this.step = step;  
  125.     }  
  126.     public double getMax() {  
  127.         return max;  
  128.     }  
  129.     public void setMax(double max) {  
  130.         this.max = max;  
  131.     }  
  132.     public double getMin() {  
  133.         return min;  
  134.     }  
  135.     public void setMin(double min) {  
  136.         this.min = min;  
  137.     }  
  138. }  

其实挺简单的,只要在onFinishInflate中加载一个布局就可以了。 
布局up_down_view.xml如下: 
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:layout_gravity="center_vertical"  
  6.     android:orientation="horizontal"  
  7.     >  
  8.     <ImageView android:id="@+id/iv_minus"  
  9.     android:layout_width="wrap_content"  
  10.     android:layout_height="wrap_content"  
  11.     android:layout_gravity="center_vertical"  
  12.     android:src="@drawable/sh_trade_minus"  
  13.     android:scaleType="fitCenter"  
  14.     />  
  15.     <EditText  android:id="@+id/et_input"  
  16.     android:layout_width="fill_parent"  
  17.     android:layout_height="wrap_content"  
  18.     android:layout_weight="1"  
  19.     android:text=""  
  20.     android:singleLine="true"  
  21.     android:inputType="numberDecimal"  
  22.     />  
  23.     <ImageView android:id="@+id/iv_plus"  
  24.     android:layout_width="wrap_content"  
  25.     android:layout_height="wrap_content"  
  26.     android:layout_gravity="center_vertical"  
  27.     android:src="@drawable/sh_trade_plus"  
  28.     android:scaleType="fitCenter"  
  29.     />  
  30. </LinearLayout>  


使用: 
在布局文件中引入: 
Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     />  
  12.     <com.ql.view.UpDownView android:id="@+id/upDownView"  
  13.     android:layout_width="fill_parent"   
  14.     android:layout_height="wrap_content"  
  15.     />  
  16. </LinearLayout>  

然后可以通过UpDownView提供的setter/getter函数对它进行初始化需要的数据了。 
Java代码   收藏代码
  1. UpDownView view=(UpDownView)findViewById(R.id.upDownView);  
  2. view.setMax(100);  
  3. view.setStep(1);  
  4. view.setMin(0);  
  5. view.setNumber(90.01);//没有的话显示为""  

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

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值