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

[url="http://gundumw100.iteye.com/admin/blogs/1134758"]上一节[/url]讲了一种实现自定义控件的方法,这种方法不够好,属性不能在xml文件中设置,只能在代码中设置。这次就讲如何在xml中直接设置属性。
首先定义一个属性文件attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="updownview">
<attr name="min" format="float" />
<attr name="max" format="float" />
<attr name="step" format="float" />
<attr name="number" format="float" />
</declare-styleable>
</resources>


然后可以在UpDownView中这样引用:

package com.ql.view;

import java.text.DecimalFormat;

import com.ql.app.R;
import android.content.Context;
import android.content.res.TypedArray;
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");
TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.updownview);
min=a.getFloat(R.styleable.updownview_min, -10000);
max=a.getFloat(R.styleable.updownview_max, 10000);
step=a.getFloat(R.styleable.updownview_step, 1000);
number=a.getFloat(R.styleable.updownview_number, 0);

a.recycle();
}
@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);

if(number!=0){
et_input.setText(convertNumberToString(number,pattern));
}

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;
}
}

这里我仍旧保留了很多setter/getter方法,是为了便于以后动态改变。

使用到的布局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>


使用,只需要在布局文件提供初始化属性就可以了,无需在代码中setXXX了:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.ql.app"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.ql.view.UpDownView android:id="@+id/upDownView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
app:min="0"
app:max="100"
app:step="1"
app:number="90.01"
/>
</LinearLayout>

注意这句话xmlns:app="http://schemas.android.com/apk/res/com.ql.app"别忘了加上。

最后奉上attrs.xml中所有可能的format值:
注1:format可选项
  "reference" //引用
  "color" //颜色
  "boolean" //布尔值
  "dimension" //尺寸值
  "float" //浮点值
  "integer" //整型值
  "string" //字符串
  "fraction" //百分数,比如200%
  枚举值,格式如下:
  < attr name="orientation">
  < enum name="horizontal" value="0" />
  < enum name="vertical" value="1" />
  < /attr>
  xml中使用时:
  android:orientation = "vertical"
  标志位,位或运算,格式如下:
  < attr name="windowSoftInputMode">
  < flag name = "stateUnspecified" value = "0" />
  < flag name = "stateUnchanged" value = "1" />
  < flag name = "stateHidden" value = "2" />
  < flag name = "stateAlwaysHidden" value = "3" />
  < flag name = "stateVisible" value = "4" />
  < flag name = "stateAlwaysVisible" value = "5" />
  < flag name = "adjustUnspecified" value = "0x00" />
  < flag name = "adjustResize" value = "0x10" />
  < flag name = "adjustPan" value = "0x20" />
  < flag name = "adjustNothing" value = "0x30" />
  < /attr>
  xml中使用时:
  android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">
  另外属性定义时可以指定多种类型值,比如:
  < attr name = "background" format = "reference|color" />
  xml中使用时:
  android:background = "@drawable/图片ID|#00FF00"

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值