android WidgetUtils


import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.*;
import com.ihandy.core.util.*;
import java.util.ArrayList;

public class WidgetUtil
{

    public WidgetUtil(Activity ctx)
    {
        this.ctx = ctx;
    }

    public void setValueByTextView(int viewId, String value)
    {
        if(IsUtil.isNullOrEmpty(value))
            value = "";
        ((TextView)ctx.findViewById(viewId)).setText(value);
    }

    public String getValueByTextView(int viewId)
    {
        return ((TextView)ctx.findViewById(viewId)).getText().toString().trim();
    }

    public void setValueByTextView(int viewId, CharSequence value)
    {
        if(TextUtils.isEmpty(value))
            value = "";
        ((TextView)ctx.findViewById(viewId)).setText(value);
    }

    public static void setValueByTextView(View baseView, int viewId, String value)
    {
        if(baseView != null)
        {
            if(IsUtil.isNullOrEmpty(value))
                value = "";
            ((TextView)baseView.findViewById(viewId)).setText(value);
        }
    }

    public void setImageByImageView(int viewId, Bitmap b)
    {
        if(b != null && !b.isRecycled())
            ((ImageView)ctx.findViewById(viewId)).setImageBitmap(b);
    }

    public void setImageByImageView(int viewId, Drawable d)
    {
        if(d != null)
            ((ImageView)ctx.findViewById(viewId)).setImageDrawable(d);
    }

    public void setBackground(int viewId, Drawable d)
    {
        if(d != null)
            ctx.findViewById(viewId).setBackgroundDrawable(d);
    }

    public static void setValue(TimePicker tp, String datetime)
    {
        if(tp != null && !IsUtil.isNullOrEmpty(datetime))
        {
            String dt[] = StringUtil.trim(datetime).split(" ");
            String time[] = dt.length != 2 ? dt[0].split(":") : dt[1].split(":");
            if(time.length >= 2)
            {
                tp.setCurrentHour(Integer.valueOf(Integer.parseInt(time[0])));
                tp.setCurrentMinute(Integer.valueOf(Integer.parseInt(time[1])));
            }
        }
    }

    public boolean setAdapter(Spinner spinner, ArrayList items)
    {
        boolean result = true;
        if(items == null)
        {
            items = new ArrayList();
            result = false;
        }
        ArrayAdapter adapter = new ArrayAdapter(ctx, 17367048, items);
        adapter.setDropDownViewResource(17367049);
        spinner.setAdapter(adapter);
        return result;
    }

    public void setAdapter(Spinner spinner, String items[][], String value)
    {
        if(setAdapter(spinner, CollectionUtil.extractList(items, 1)))
        {
            int i = 0;
            String as[][];
            int k = (as = items).length;
            for(int j = 0; j < k; j++)
            {
                String item[] = as[j];
                if(item[1].equals(value))
                {
                    spinner.setSelection(i, true);
                    break;
                }
                i++;
            }

        }
    }

    public void setAdapter(Spinner spinner, String items[][])
    {
        setAdapter(spinner, CollectionUtil.extractList(items, 0));
    }

    private Activity ctx;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值