代码控制CheckBox或者其他控件的选中状态

package com.zte.aaa;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

public class AaaActivity extends Activity {
Integer[] mButtonState1 = { R.drawable.search_nochoice,
R.drawable.search_choice };
Drawable[] mewState = new Drawable[2];

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Integer[] mButtonState = { R.drawable.more_dialg_two_button_no_click,
// R.drawable.ic_launcher,
// R.drawable.more_dialog_two_buttono_click_state };

// Button mButton = (Button) findViewById(R.id.button1);
CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
data();
checkBox.setButtonDrawable(setbg(mewState));

}

public void data() {
for (int i = 0; i < mButtonState1.length; i++) {
mewState[i] = image(mButtonState1[i]);
}
}

public Drawable image(int id) {
Bitmap bitmap;
BitmapDrawable bd;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// 获取这个图片的宽和高
bitmap = BitmapFactory.decodeResource(getResources(), id, options); // 此时返回bm为空
options.inJustDecodeBounds = false;
// 计算缩放比
int be = (int) (options.outHeight / (float) 50);
if (be <= 0)
be = 1;
options.inSampleSize = be;
// 重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false哦
bitmap = BitmapFactory.decodeResource(getResources(), id, options);
bd = new BitmapDrawable(bitmap);
return bd;
}

// 以下这个方法也可以把你的图片数组传过来,以StateListDrawable来设置图片状态,来表现button的各中状态。未选
// 中,按下,选中效果。
public StateListDrawable setbg(Drawable[] mImageIds) {
StateListDrawable bg = new StateListDrawable();
Drawable normal = mImageIds[0];
// Drawable selected =
// this.getResources().getDrawable(mImageIds[1]);
Drawable pressed = mImageIds[1];
bg.addState(new int[] { android.R.attr.state_checked }, pressed);
bg.addState(new int[] { android.R.attr.state_enabled }, normal);
// bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected);
// bg.addState(View.SELECTED_STATE_SET, normal);
// bg.addState(View.FOCUSED_STATE_SET, selected);
// bg.addState(View.EMPTY_STATE_SET, normal);
return bg;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值