Seekbar控件
Seekbar控件为拖动条
基本属性除了id,布局属性等 还有max、progress等等 max指定拖动条的最大值,progress指定当前值,可以用progress属性给拖动条指定初始值
<SeekBar
android:id="@+id/brightness_seekbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginVertical="50dp"
android:max="255"
android:progress="150"/>
另外拖动条总是有功能的吧,我们去拉动它的时候肯定是指派有逻辑的,SeekBar类提供了三个方法用来写响应逻辑,这三个方法需要我们进行重写,分别对应改变seekbar值、按下、松开seekbar时的逻辑;当监听到指定动作时,对重写方法进行回调,比如在Fragment里:
private FragmentRightSettingBrightnessBinding binding;
@Nullable
@org.jetbrains.annotations.Nullable
@Override
public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
binding = FragmentRightSettingBrightnessBinding.inflate(inflater, container, false);
int progress = SystemBrightnessManager.getBrightness(getActivity());
binding.brightnessSeekbar.setProgress(progress);
binding.brightnessSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
Log.d("RightSettingBrightness", "You changed progress");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Log.d("RightSettingBrightness", "You started touching");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Log.d("RightSettingBrightness", "You stoped touching");
}
});
return binding.getRoot();
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
onProgressChanged() 方法里的参数i 是seekbar的progress属性,即seekbar的值,可以用这个i 来写逻辑,比如音量、亮度的调节等等。同时,可通过Seekbar对象的getProgress() 方法来获取这个值。 这里用了binding来替代findViewById() 方法,在我另一篇博客做了简单介绍,《第一行代码》的作者郭霖在他的博客里做了详细说明
|