您的位置:首页 > 移动开发 > Android开发

Android获取一个按钮的状态

2016-07-19 17:08 459 查看
安卓在开发过程中,并没有ios中的isSelected属性,来判断按钮是否被选中,然后,根据状态来改变按钮的背景图片

这个时候,我是这样做的,定义一个变量

public boolean
isSelected =false;
用这个变量来表示按钮的当前状态

Button switchTimingButton = (Button)convertView.findViewById(R.id.timing_switch_button);

if(isSelected){

switchTimingButton.setBackground(context.getResources().getDrawable(R.drawable.switch_button_selected));

}else{

switchTimingButton.setBackground(context.getResources().getDrawable(R.drawable.switch_button_normal));

}

根据不同的状态,改变背景图片,然后是点击事件

switchTimingButton.setOnClickListener(new View.OnClickListener() {

@Override

public
void onClick(View arg0) {

//
TODO Auto-generated method stub

if(isSelected){

isSelected =
false;

arg0.setBackground(context.getResources().getDrawable(R.drawable.switch_button_normal));

}else{

isSelected =
true;

arg0.setBackground(context.getResources().getDrawable(R.drawable.switch_button_selected));

}

}

});

在点击过程中,不断的判断isSelected的值,根据isSelected值来判断当前状态,并在点击后改变其值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息