Android SeekBar實現禁止滑動

本文實例為大傢分享瞭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);

  }
 }
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀:

    None Found