一、基础介绍
滑动选择器组件(Picker),在很多APP当中经常用来进行类别选别或者地址的选择。
1、组件常用属性介绍
- value:设置默认选中的值
- max_value:指定显示内容的最大值
- min_value:指定显示内容的起始值
- shader_color:着色器,修饰组件的颜色,从上下往中间渐变(由深到浅)
- normal_text_size:未选中字体的大小
- normal_text_color:未选中字体的颜色
- selected_text_size:选中字体的大小
- selected_text_color:选中字体的颜色
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="vertical">
<!-- value:默认选中的值 -->
<!-- shader_color:着色器,往中间渐变 -->
<Picker
ohos:id="$+id:picker"
ohos:height="match_content"
ohos:width="100vp"
ohos:normal_text_size="30fp"
ohos:selected_text_size="30fp"
ohos:normal_text_color="#21a8fd"
ohos:selected_text_color="#FF0000"
ohos:max_value="6"
ohos:min_value="0"
ohos:value="3"
ohos:shader_color="#00FF00"
/>
</DirectionalLayout>
2、星期选择案例
星期选择案例中,组件的取值范围为0~6
public class MainAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//找到组件
Picker picker = (Picker) findComponentById(ResourceTable.Id_picker);
//存储星期1~7
ArrayList<String> list = new ArrayList();
list.add("星期一");
list.add("星期二");
list.add("星期三");
list.add("星期四");
list.add("星期五");
list.add("星期六");
list.add("星期日");
//修改组件显示内容
// picker.setFormatter(
// new Picker.Formatter() {
// @Override
// public String format(int i) {
// //参数i:代表选中的数字
// //返回值就是要展示的内容
// return list.get(i);
// }
// }
// );
//简化代码1
//lamda表达式写法
// picker.setFormatter(
// (int i) -> {
// return list.get(i);
// }
// );
//简化代码2
//lamda继续优化1
//当参数只有一个的时候,数据类型可以省略,雄安括号可以省略
// picker.setFormatter(i -> list.get(i));
//简化代码3
//方法引用简化
//用get当作抽象方法的方法体
picker.setFormatter(list::get);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}