本文实例为大家分享了Android SeekBar实现禁止滑动的具体代码,供大家参考,具体内容如下
由于项目需要,在关闭开关的时候需要将顶部的调温栏禁用,变为灰色且不可点击滑动,而开的时候要启用,变为黄色且可点击滑动
为防止抓不住重点,仅展示相关代码
public class DeviceControlActivity extends Activity implements View.OnClickListener,SeekBar.OnSeekBarChangeListener{
private SeekBar mSeekBar;
private TextView mLowTempTextView;
private TextView mMiddleTempTextView;
private TextView mHighTempTextView;
private Button mSwitchButton;
private int mSwitchButtonStatus =0;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.device_control_layout);
initView();
intiData();
initEvent();
}
private void initView(){
mSeekBar=findViewById(R.id.id_seek_bar);
}
private void initEvent(){
mSeekBar.setOnSeekBarChangeListener(this);
setSeekBarClickable(0);//先禁用seekBar
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.id_device_control_switch_button:
if (mSwitchButtonStatus ==1){
mSwitchButton.setBackgroundResource(R.drawable.switch_button_off_img);
mSwitchButtonStatus =0;
sendAt("AT+IO1=L");
setSeekBarClickable(mSwitchButtonStatus);//禁用
}else {
mSwitchButton.setBackgroundResource(R.drawable.switch_button_on_img);
mSwitchButtonStatus =1;
sendAt("AT+IO1=H");
setSeekBarClickable(mSwitchButtonStatus);//启用
}
break;
}
}
private void setSeekBarClickable(int i){
if (i==1){
//启用状态
mSeekBar.setClickable(true);
mSeekBar.setEnabled(true);
mSeekBar.setSelected(true);
mSeekBar.setFocusable(true);
Drawable drawable=getResources().getDrawable(R.drawable.yellow_mid_img_40);
mSeekBar.setThumb(drawable);
mSeekBar.setProgress(50);
}else {
//禁用状态
mSeekBar.setClickable(false);
mSeekBar.setEnabled(false);
mSeekBar.setSelected(false);
mSeekBar.setFocusable(false);
Drawable drawable=getResources().getDrawable(R.drawable.seek_bar_grey_img_40);
mSeekBar.setThumb(drawable);
mSeekBar.setProgress(50);
}
}
}