Android UI組件Spinner下拉列表詳解
Spinner下拉列表的實(shí)現(xiàn)代碼,分享給大家
該布局對(duì)應(yīng)的關(guān)系圖:

常用屬性:android:entries(指定spinner要顯示的字符串資源。必須是在strings資源文件中定義的字符串資源)android:spinnerMode(spinner的模式,枚舉值有兩個(gè)值dialog彈窗顯示和dropdown下拉顯示)android:dropDownWidth(下拉框的寬度,單位通常是dp)android:prompt(當(dāng)spinnerMode的值是dialog時(shí),彈出的對(duì)話(huà)框式的下列列表的提示。如果
spinnerMode的值是dropdown時(shí)沒(méi)有效果。注意:此處的值不能直接使用直接字符串,
必須使用引用(字符串資源))
1.通過(guò)entries設(shè)置數(shù)據(jù)項(xiàng),在values文件夾下的strings中添加數(shù)據(jù)的值

在strings.xml中添加一組array數(shù)據(jù)項(xiàng),然后通過(guò)在entries中設(shè)置就可以設(shè)置對(duì)應(yīng)的值

<Spinner
android:layout_width="match_parent"
android:entries="@array/data"http://資源文件設(shè)置數(shù)據(jù)
android:layout_height="wrap_content">
</Spinner>
2.設(shè)置android:spinnerMode:
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:entries="@array/data"
android:spinnerMode="dialog"
android:layout_height="wrap_content">
</Spinner>
值為dialog的顯示為彈框顯示

值如果為dropdown的顯示如下:

android:dropDownWidth設(shè)置下拉寬度
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:entries="@array/data"
android:spinnerMode="dropdown"
android:dropDownWidth="70dp"
android:layout_height="wrap_content">
</Spinner>
效果如下圖:

數(shù)據(jù)源的獲取方式:通過(guò)ArrayAdapter適配器設(shè)置數(shù)據(jù)數(shù)據(jù)>
什么是適配器:將控件在加載數(shù)據(jù)過(guò)程中的同樣的部分 抽取為代碼,每次加載的時(shí)候都調(diào)用這部分代碼,生成
要返回的內(nèi)容,類(lèi)似于模具
關(guān)于ArrayAdapter簡(jiǎn)單介紹下:
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,data);
關(guān)于ArrayAdapter構(gòu)造方法的說(shuō)明:
1、ArrayAdapter(context, resource, objects)
參數(shù)一:上下文對(duì)象
參數(shù)二:布局文件的id,注意該布局文件有且只能有一個(gè)TextView標(biāo)簽
參數(shù)三:原始數(shù)據(jù),List集合或數(shù)組都可以。
2、ArrayAdapter(context, resource, textViewResourceId, objects)
參數(shù)一:上下文對(duì)象
參數(shù)二:布局文件的id,注意該布局文件中至少有一個(gè)TextView標(biāo)簽
參數(shù)三:參數(shù)二布局文件中要顯示數(shù)據(jù)的TextView的id
參數(shù)四:原始數(shù)據(jù),List集合或數(shù)組都可以。
public class MainActivity extends AppCompatActivity {
private String[] data;
private List<String> data1;
private Spinner spinner;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner);
spinner = (Spinner)findViewById(R.id.spinner);
data = getResources().getStringArray(R.array.data);
data1 = new ArrayList<>();
for(int i = 1; i < 10; i++){
data1.add("這是第" + i +"個(gè)");
}
//data可以修改為data1,數(shù)據(jù)可以是數(shù)組也可以是集合
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,data);
spinner.setAdapter(adapter);
}
}
監(jiān)聽(tīng)事件
對(duì)于Spinner使用的監(jiān)聽(tīng)事件為:setOnItemSelectedListener(OnItemSelectedListener listener)
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
/**
* 當(dāng)item被選中時(shí),會(huì)調(diào)用此方法
*/
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
/**
* 當(dāng)數(shù)據(jù)項(xiàng)的值設(shè)置為空時(shí),就會(huì)調(diào)用此方法,通過(guò)調(diào)用adapter.clear()方法清空數(shù)據(jù),并且刷新界面
* 時(shí),會(huì)調(diào)用次方法
*/
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)聯(lián)動(dòng)下拉框 下拉列表spinner的實(shí)例代碼
- Android下拉列表選項(xiàng)框及指示箭頭動(dòng)畫(huà)
- Android自定義Spinner下拉列表(使用ArrayAdapter和自定義Adapter實(shí)現(xiàn))
- Android控件Spinner實(shí)現(xiàn)下拉列表及監(jiān)聽(tīng)功能
- Android仿微信實(shí)現(xiàn)下拉列表
- Android自定義單選多選下拉列表的實(shí)例代碼
- Android仿美團(tuán)淘寶實(shí)現(xiàn)多級(jí)下拉列表菜單功能
- Android使用Spinner控件實(shí)現(xiàn)下拉列表的案例
- Android下拉列表spinner的實(shí)例代碼
- Android Studio實(shí)現(xiàn)下拉列表效果
相關(guān)文章
Android WiFi熱點(diǎn)開(kāi)發(fā)的示例代碼
這篇文章主要介紹了Android WiFi熱點(diǎn)開(kāi)發(fā)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Android中實(shí)現(xiàn)TCP和UDP傳輸實(shí)例
這篇文章主要介紹了Android中實(shí)現(xiàn)TCP和UDP傳輸實(shí)例,本文給出了TCP服務(wù)器端代碼、TCP客戶(hù)端代碼、UDP服務(wù)器端代碼、UDP客戶(hù)端代碼等代碼實(shí)例,需要的朋友可以參考下2015-03-03
Android Studio 3.1.3升級(jí)至3.6.1后舊項(xiàng)目的兼容操作方法
這篇文章主要介紹了Android Studio 3.1.3升級(jí)至3.6.1后舊項(xiàng)目的兼容操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
開(kāi)源自研內(nèi)存分析利器Android?Bitmap?Monitor圖片定位詳解
這篇文章主要為大家介紹了Android?Bitmap?Monitor開(kāi)源自研內(nèi)存分析利器,助你定位不合理的圖片使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android ExpandableListView用法示例詳解
ExpandableListView 是 Android 中一個(gè)非常實(shí)用的列表控件,它可以幫助我們實(shí)現(xiàn)具有分組功能的列表展示,通過(guò)本文的介紹,你應(yīng)該已經(jīng)掌握了 ExpandableListView 的基本用法,感興趣的朋友跟隨小編一起看看吧2025-02-02

