1. 功能
下拉框相当常见了,例如选择省、市、县;例如选择部门;选择员工。用处一般是多选一。
2. 显示
下拉框一般有两种形式,一种是下拉显示可选项,一种是弹窗显示可选项。通过调整参数spinnerMode,dropdown/dialog分别代表两种下拉框。
注意下拉框的数据需要从资源中关联过来,资源文件res/values/arrays.xml代码如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="gradeArray"> <item>初一</item> <item>初二</item> <item>初三</item> </string-array> </resources> Android布局文件如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="4dp"> <Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/gradeArray" android:spinnerMode="dropdown"></Spinner> <Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/gradeArray" android:spinnerMode="dialog"></Spinner> <Button android:id="@+id/buttonOk" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="确认" /> </LinearLayout>
3. 动态设置选项
可以利用数组适配器ArrayAdapter
动态设置下来框的选项,布局文件修改为:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="4dp"> <Spinner android:id="@+id/spinnerGrade" android:layout_width="match_parent" android:layout_height="wrap_content" android:spinnerMode="dropdown"></Spinner> <Button android:id="@+id/buttonOk" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="确认" /> </LinearLayout> 代码部分修改为: public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Spinner spinnerGrade=findViewById(R.id.spinnerGrade); //内容数组 String[] gradeArray={"初一","初二","初三","初四"}; //数组适配器 ArrayAdapter<String> gradeAdapter=new ArrayAdapter<>(this,R.layout.support_simple_spinner_dropdown_item,gradeArray); spinnerGrade.setAdapter(gradeAdapter); //设置默认选中项 spinnerGrade.setSelection(0); } }
4. 获取选中项
点击确认后获取选中项,并显示选中项内容。
public class MainActivity extends AppCompatActivity { //内容数组 String[] gradeArray={"初一","初二","初三","初四"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Spinner spinnerGrade=findViewById(R.id.spinnerGrade); //数组适配器 ArrayAdapter<String> gradeAdapter=new ArrayAdapter<>(this,R.layout.support_simple_spinner_dropdown_item,gradeArray); spinnerGrade.setAdapter(gradeAdapter); //设置默认选中项 spinnerGrade.setSelection(0); //获取按钮 Button buttonOk = findViewById(R.id.buttonOk); //设置按钮点击监听器 buttonOk.setOnClickListener(new MyOnClickListener()); } //定义按钮点击监听器 class MyOnClickListener implements View.OnClickListener { //按钮点击 @Override public void onClick(View view) { if (view.getId() == R.id.buttonOk) {//被点击的是确认按钮 //获取选中项 Spinner spinnerGrade=findViewById(R.id.spinnerGrade); //显示提示框 Toast.makeText(MainActivity.this, gradeArray[spinnerGrade.getSelectedItemPosition()] , Toast.LENGTH_SHORT).show(); } } } }
5. 监听下拉框选中项
当下拉框选中项发生改变时,弹窗显示新项目,代码如下:
public class MainActivity extends AppCompatActivity { //内容数组 String[] gradeArray={"初一","初二","初三","初四"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Spinner spinnerGrade=findViewById(R.id.spinnerGrade); //数组适配器 ArrayAdapter<String> gradeAdapter=new ArrayAdapter<>(this,R.layout.support_simple_spinner_dropdown_item,gradeArray); spinnerGrade.setAdapter(gradeAdapter); //设置默认选中项 spinnerGrade.setSelection(0); //获取按钮 Button buttonOk = findViewById(R.id.buttonOk); //设置按钮点击监听器 buttonOk.setOnClickListener(new MyOnClickListener()); spinnerGrade.setOnItemSelectedListener(new MyOnItemSelectedListener()); } //定义按钮点击监听器 class MyOnClickListener implements View.OnClickListener { //按钮点击 @Override public void onClick(View view) { if (view.getId() == R.id.buttonOk) {//被点击的是确认按钮 //获取选中项 Spinner spinnerGrade=findViewById(R.id.spinnerGrade); //显示提示框 Toast.makeText(MainActivity.this, gradeArray[spinnerGrade.getSelectedItemPosition()] , Toast.LENGTH_SHORT).show(); } } } class MyOnItemSelectedListener implements AdapterView.OnItemSelectedListener { //选择 @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(MainActivity.this, gradeArray[i], Toast.LENGTH_SHORT).show(); } //未选择 @Override public void onNothingSelected(AdapterView<?> adapterView) { } } }