在Android中,设置WheelView的方法如下:
dependencies {
implementation 'com.github.lawnstarter:WheelView:1.0.0'
}
<com.lawnstarter.WheelView
android:id="@+id/wheel_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
import com.lawnstarter.WheelView;
public class MainActivity extends AppCompatActivity {
private WheelView wheelView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wheelView = findViewById(R.id.wheel_view);
// 创建一个数组,包含你想要在WheelView中显示的数据
String[] items = {"选项1", "选项2", "选项3", "选项4", "选项5"};
// 创建一个适配器,用于将数据绑定到WheelView的每个项
ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<>(this, items);
// 将适配器应用到WheelView
wheelView.setAdapter(adapter);
// 设置选择监听器,当用户选择一个选项时触发
wheelView.setOnItemSelectedListener(new WheelView.OnItemSelectedListener() {
@Override
public void onItemSelected(WheelView view, int position) {
String selectedItem = items[position];
Toast.makeText(MainActivity.this, "选中的项目: " + selectedItem, Toast.LENGTH_SHORT).show();
}
});
}
}
现在,你已经成功设置了WheelView,并添加了一些选项。当用户选择一个选项时,会弹出一个Toast显示所选项目。你可以根据需要自定义适配器、布局和监听器。