欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-08 16:06:23
...
在[url="http://gundumw100.iteye.com/admin/blogs/1134775"]上一节[/url]中在onFinishInflate方法中使用了View view=LayoutInflater.from(getContext()).inflate(R.layout.up_down_view, this);来加载一个布局文件。
这样会多一个up_down_view.xml文件,有点浪费。
这节采用动态布局(就是利用代码)实现同样的效果。

package com.ql.view;

import java.text.DecimalFormat;

import com.ql.app.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputType;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;

public class UpDownView extends LinearLayout {
private final static String tag="UpDownView";
private final int id_minus=1;
private final int id_plus=2;
private ImageView iv_minus,iv_plus;
private EditText et_input;
private Context context;
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");
context=getContext();
LinearLayout.LayoutParams params_iv=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params_iv.gravity=Gravity.CENTER_VERTICAL;
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);

LinearLayout layout=new LinearLayout(context);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(params);

params.weight=1;

iv_minus=new ImageView(context);
iv_minus.setId(id_minus);
iv_minus.setImageResource(R.drawable.sh_trade_minus);
iv_minus.setScaleType(ScaleType.FIT_CENTER);
iv_plus=new ImageView(context);
iv_plus.setId(id_plus);
iv_plus.setImageResource(R.drawable.sh_trade_plus);
iv_plus.setScaleType(ScaleType.FIT_CENTER);

et_input=new EditText(context);
et_input.setImeOptions(EditorInfo.IME_ACTION_DONE|EditorInfo.IME_FLAG_NO_EXTRACT_UI);
et_input.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);

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

iv_minus.setOnClickListener(listener);
iv_plus.setOnClickListener(listener);


layout.addView(iv_minus, params_iv);
layout.addView(et_input, params);
layout.addView(iv_plus, params_iv);

addView(layout);
}

OnClickListener listener=new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case id_minus:
doMinus();
break;
case id_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方法中最后别忘了addView(layout);

其他同第二节都一样,就不罗嗦了。

最后来个大结局:

[img]http://dl.iteye.com/upload/attachment/526074/8d8e3213-6ed0-3d17-9413-2934f0c2fbd8.png[/img]
相关标签: android