所需程序包
相关代码
实现效果
程序包
implementation 'com.contrarywind:Android-PickerView:4.1.9'
相关代码
package com.example.day_3_2_sheel;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private WheelView wheelView;
ArrayWheelAdapter<String> myadapter;
List<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
wheelView.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(int index) {
Toast.makeText(MainActivity.this, ""+list.get(index), Toast.LENGTH_SHORT).show();
}
});
list=new ArrayList<>();
list.add("丰裕之角");
list.add("第戎学院");
list.add("小镇庙会");
myadapter=new ArrayWheelAdapter(list);
wheelView.setAdapter(myadapter);
}
private void initView() {
wheelView = (WheelView) findViewById(R.id.wheel_view);
}
}
功能图
级联
private void initOptionPicker() {
final List<String> options1Items = new ArrayList<>();
options1Items.add("射雕英雄传");
options1Items.add("神雕侠侣");
options1Items.add("鹿鼎记");
List<String> options2Items_01 = new ArrayList<>();
options2Items_01.add("郭靖");
options2Items_01.add("黄蓉");
options2Items_01.add("杨铁心");
options2Items_01.add("欧阳克");
List<String> options2Items_02 = new ArrayList<>();
options2Items_02.add("小龙女");
options2Items_02.add("杨过");
options2Items_02.add("大雕");
List<String> options2Items_03 = new ArrayList<>();
options2Items_03.add("小柜子");
options2Items_03.add("康熙");
options2Items_03.add("康亲王");
options2Items_03.add("鳌拜");
final List<List<String>> options2Items = new ArrayList<>();
options2Items.add(options2Items_01);
options2Items.add(options2Items_02);
options2Items.add(options2Items_03);
OptionsPickerView optionsPickerView = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
@Override
public void onOptionsSelect(int options1, int options2, int options3, View v) {
String tx = options1Items.get(options1)
+ options2Items.get(options1).get(options2);
Toast.makeText(getBaseContext(), tx, Toast.LENGTH_SHORT).show();
}
})
.setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
@Override
public void onOptionsSelectChanged(int options1, int options2, int options3) {
String str = "options1: " + options1 + "\noptions2: " + options2 + "\noptions3: " + options3;
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
})
.build();
optionsPickerView.setPicker(options1Items, options2Items);
optionsPickerView.show();
}```
![级联](https://img-blog.csdnimg.cn/20210302141253174.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpZ2h0MDI=,size_16,color_FFFFFF,t_70)