生成一个选项为“植物名”“拉丁名”“地区”,响应反应都不同的Android spinner
时间: 2023-11-03 13:04:38 浏览: 42
以下是一个简单的例子,可以生成一个包含“植物名”、“拉丁名”和“地区”选项的Spinner,并根据用户的选择响应不同的操作。
```
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private Spinner mSpinner;
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpinner = findViewById(R.id.spinner);
mTextView = findViewById(R.id.textview);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.options_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
mSpinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String option = parent.getItemAtPosition(position).toString();
switch (option) {
case "植物名":
// 响应植物名选项的操作
mTextView.setText("您选择了植物名");
break;
case "拉丁名":
// 响应拉丁名选项的操作
mTextView.setText("您选择了拉丁名");
break;
case "地区":
// 响应地区选项的操作
mTextView.setText("您选择了地区");
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// 未选择任何选项
}
}
```
在`strings.xml`文件中定义选项的字符串数组:
```
<resources>
<string-array name="options_array">
<item>植物名</item>
<item>拉丁名</item>
<item>地区</item>
</string-array>
</resources>
```
在布局文件中添加Spinner和TextView:
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown" />
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/spinner"
android:layout_marginTop="16dp"
android:textAlignment="center"
android:textSize="24sp" />
</RelativeLayout>
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)