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值来判断当前状态,并在点击后改变其值
这个时候,我是这样做的,定义一个变量
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值来判断当前状态,并在点击后改变其值
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析