android控件Spinner(下拉列表)的使用例子
一、介紹:
安卓的Spinner是一個(gè)下拉菜單控件,通常用于選擇一組選項(xiàng)中的一個(gè)。它可以為用戶提供一種簡(jiǎn)單的交互方式,使其能夠快速輕松地對(duì)應(yīng)用程序中的數(shù)據(jù)進(jìn)行選擇。
要使用Spinner,首先需要在XML布局文件中創(chuàng)建一個(gè)Spinner元素。這個(gè)元素必須包含一個(gè)Adapter,該Adapter負(fù)責(zé)將數(shù)據(jù)與Spinner控件綁定在一起。在Java代碼中,我們需要為Spinner設(shè)置一個(gè)監(jiān)聽(tīng)器,以便在用戶選擇一個(gè)選項(xiàng)時(shí)進(jìn)行響應(yīng)。
Spinner相關(guān)屬性:
- android:dropDownHorizontalOffset:設(shè)置列表框的水平偏移距離
- android:dropDownVerticalOffset:設(shè)置列表框的水平豎直距離
- android:dropDownSelector:列表框被選中時(shí)的背景
- android:dropDownWidth:設(shè)置下拉列表框的寬度
- android:gravity:設(shè)置里面組件的對(duì)其方式
- android:popupBackground:設(shè)置列表框的背景
- android:prompt:設(shè)置對(duì)話框模式的列表框的提示信息(標(biāo)題),只能夠引用string.xml 中的資源id,而不能直接寫字符串
- android:spinnerMode:列表框的模式,有兩個(gè)可選值: dialog:對(duì)話框風(fēng)格的窗口 dropdown:下拉菜單風(fēng)格的窗口(默認(rèn))
- 可選屬性:android:entries:使用數(shù)組資源設(shè)置下拉列表框的列表項(xiàng)目
二、例子
例子一:(含適配器)
以下是創(chuàng)建Spinner控件的示例代碼:
XML:
<Spinner android:id="@+id/my_spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" />
在上面的代碼中,我們創(chuàng)建了一個(gè)具有id“my_spinner”的Spinner控件,并設(shè)置了其寬度和高度。接下來(lái),我們需要將適配器與此控件綁定。以下是將ArrayAdapter與Spinner綁定的示例代碼:
綁定數(shù)據(jù):
String[] items = {"Item 1", "Item 2", "Item 3"}; ArrayAdapter<String> adapter = new ArrayAdapter<(this,android.R.layout.simple_spinner_item,items); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); Spinner spinner = findViewById(R.id.my_spinner); spinner.setAdapter(adapter);
在上面的代碼中,我們首先定義一個(gè)包含字符串項(xiàng)的數(shù)組。接下來(lái),我們創(chuàng)建了一個(gè)ArrayAdapter對(duì)象,并設(shè)置其資源ID和數(shù)據(jù)項(xiàng)。最后,我們將適配器與具有id“my_spinner”的Spinner控件綁定起來(lái)。
一旦Spinner控件被設(shè)置了適配器,我們需要為其設(shè)置一個(gè)選擇監(jiān)聽(tīng)器。以下是在Java代碼中設(shè)置Spinner的選項(xiàng)監(jiān)聽(tīng)器的示例代碼:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // 記錄當(dāng)前選擇的項(xiàng) String selectedItem = (String) parent.getItemAtPosition(position); Log.d("Spinner", "Selected item: " + selectedItem); } @Override public void onNothingSelected(AdapterView<?> parent) { // 什么都不做 } });
在上面的代碼中,我們創(chuàng)建了一個(gè)匿名AdapterView.OnItemSelectedListener對(duì)象,并將其分配給Spinner控件。當(dāng)用戶選擇一個(gè)項(xiàng)時(shí),onItemSelected方法將被調(diào)用,并且我們可以讀取所選的選項(xiàng)。如果用戶沒(méi)有選擇任何項(xiàng),則onNothingSelected方法將被調(diào)用。
通過(guò)使用Spinner控件,我們可以輕松地讓用戶在應(yīng)用程序中進(jìn)行選擇,并在選擇時(shí)執(zhí)行適當(dāng)?shù)牟僮?。無(wú)論是在表單中選擇選項(xiàng)還是在應(yīng)用程序中選擇不同的視圖,Spinner都是一個(gè)很有用的工具。
完整代碼:
activity_main:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Spinner android:id="@+id/my_spinner" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity:
package com.example.spinnerdemo02; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; public class MainActivity extends AppCompatActivity { private Spinner spinner; // 下拉列表的數(shù)據(jù) String[] items; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化界面 initView(); // 綁定數(shù)據(jù) initData(); } private void initData() { items = new String[]{"Item 1", "Item 2", "Item 3"}; ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, items); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); // spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // 記錄當(dāng)前選擇的項(xiàng) String selectedItem = (String) parent.getItemAtPosition(position); Log.d("Spinner", "Selected item: " + selectedItem); } @Override public void onNothingSelected(AdapterView<?> parent) { // 什么都不做 } }); } private void initView() { spinner = findViewById(R.id.my_spinner); } }
例子二(不含適配器):
效果圖:
完整代碼:
MainActivity:
package com.example.spinnerdemo; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.RadioGroup; import android.widget.Spinner; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{ EditText name; // 下拉列表 Spinner mSpinner; // 復(fù)選框 CheckBox basketball,music,game; // 單選框 RadioGroup mRadioGroup; Button mButton; // 性別 private String sex = ""; // 愛(ài)好 private String hobby = ""; // 班級(jí) private String Class; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { name = findViewById(R.id.name); mSpinner = findViewById(R.id.spinner); mRadioGroup = findViewById(R.id.group); mButton = findViewById(R.id.button); basketball = findViewById(R.id.basketball); music = findViewById(R.id.music); game = findViewById(R.id.game); // 使用RadioGroup監(jiān)聽(tīng)Radio的變化 mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId){ case R.id.man: sex = "男"; break; case R.id.woman: sex = "女"; break; default: sex = ""; break; } } }); // 注冊(cè)復(fù)選框監(jiān)聽(tīng)事件 basketball.setOnCheckedChangeListener(this); game.setOnCheckedChangeListener(this); music.setOnCheckedChangeListener(this); // 下拉列表監(jiān)聽(tīng)事件 mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { // 當(dāng)item被選中時(shí)調(diào)用該方法 @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Class = MainActivity.this.getResources().getStringArray(R.array.position)[position]; } // 當(dāng)item沒(méi)有被選中時(shí)調(diào)用 @Override public void onNothingSelected(AdapterView<?> parent) { } }); // 按鈕的監(jiān)聽(tīng)事件 mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "姓名:"+name.getText()+" 班級(jí):"+Class+"性別:"+sex+" 愛(ài)好:"+hobby, Toast.LENGTH_SHORT).show(); } }); } // 復(fù)選框的監(jiān)聽(tīng)事件 @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // 獲取按鈕的文本 String msg = buttonView.getText().toString(); // 判斷是否被選中 if(isChecked) { if (!hobby.contains(msg)) { hobby = hobby + msg; } }else{ if (hobby.contains(msg)){ hobby = hobby.replace(msg,""); } } } }
activity_main:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名:" android:textSize="20sp" /> <EditText android:id="@+id/name" android:layout_width="80dp" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="班級(jí):" android:textSize="20sp" /> <Spinner android:id="@+id/spinner" android:entries="@array/position" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/sex" android:text="性別:" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <RadioGroup android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/man" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:textSize="20sp"/> <RadioButton android:checked="true" android:id="@+id/woman" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" android:textSize="20sp"/> </RadioGroup> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="愛(ài)好" android:textSize="20sp" /> <CheckBox android:id="@+id/basketball" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="籃球" /> <CheckBox android:id="@+id/music" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="音樂(lè)"/> <CheckBox android:id="@+id/game" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="音樂(lè)"/> </LinearLayout> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提交"/> </LinearLayout>
在res/value 中創(chuàng)建array.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="position"> <item>計(jì)算機(jī)21-2</item> <item>新能源20-1</item> <item>電子19-1</item> </string-array> </resources>
總結(jié)
到此這篇關(guān)于android控件Spinner(下拉列表)使用的文章就介紹到這了,更多相關(guān)Spinner下拉列表使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一分鐘快速定位Android啟動(dòng)耗時(shí)問(wèn)題
做開(kāi)發(fā)除了實(shí)現(xiàn)功能,還要注重優(yōu)化,性能優(yōu)化包括的東西還是非常多的,下面這篇文章主要給大家介紹了關(guān)于如何通過(guò)一分鐘快速定位Android啟動(dòng)耗時(shí)問(wèn)題的相關(guān)資料,需要的朋友可以參考下2021-07-07Android使用ViewFlipper和GestrueDetector共同實(shí)現(xiàn)滑屏效果實(shí)例
這篇文章主要介紹了Android使用ViewFlipper和GestrueDetector共同實(shí)現(xiàn)滑屏效果,結(jié)合完整實(shí)例形式分析了ViewFlipper和GestrueDetector控件實(shí)現(xiàn)滑屏功能的布局與相關(guān)操作技巧,需要的朋友可以參考下2017-02-02android使用ViewPager實(shí)現(xiàn)圖片自動(dòng)切換
這篇文章主要為大家詳細(xì)介紹了android使用ViewPager實(shí)現(xiàn)圖片自動(dòng)切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02Android實(shí)現(xiàn)完整游戲循環(huán)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)完整游戲循環(huán)的方法,以實(shí)例代碼形式較為詳細(xì)的分析了Android游戲循環(huán)的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android ImageView 不顯示JPEG圖片的問(wèn)題解決
本篇文章主要介紹了Android ImageView 不顯示JPEG圖片及Android Studio中如何引用圖片資源的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-05-05Android編程添加快捷方式(Short)到手機(jī)桌面的方法(含添加,刪除及查詢)
這篇文章主要介紹了Android編程添加快捷方式(Short)到手機(jī)桌面的方法,含有針對(duì)桌面快捷方式的添加,刪除及查詢的操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01android實(shí)現(xiàn)自動(dòng)發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)自動(dòng)發(fā)送郵件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android中實(shí)現(xiàn)延時(shí)執(zhí)行操作的方法小結(jié)
在Android開(kāi)發(fā)中我們可能會(huì)有延時(shí)執(zhí)行某個(gè)操作的需求,這篇文章主要介紹了Android中實(shí)現(xiàn)延時(shí)執(zhí)行操作的幾種方法,需要的朋友可以參考下2018-10-10