Android使用 Spinner控件實現(xiàn)下拉框功能
Spinner是android的一種控件,用它我們可以實現(xiàn)下拉框。
我們先來看一下效果圖


這是一個很簡單的功能,上面一個TextView,下面一個Spinner,TextView用于顯示Spinner選擇的選項。
下面我們就來看一下實現(xiàn)吧。
首先,我們先在xml文件中將spinner寫出
<?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"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/spinner_textview"/> <Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/spinner1"></Spinner> </LinearLayout>
類似于ListView,Spinner也需要一個List和一個Adapter來為其提供顯示的數(shù)據(jù)。
public class MainActivity extends AppCompatActivity {
private List<String> teamList;
private TextView textView;
private Spinner spinner1;
private ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//設(shè)置下拉列表的風(fēng)格
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//將adapter 添加到spinner中
spinner1.setAdapter(arrayAdapter);
//設(shè)置點擊事件
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
textView.setText(teamList.get(i));
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
public void initView(){
teamList = new ArrayList<>();
initList();
textView = findViewById(R.id.spinner_textview);
spinner1 = findViewById(R.id.spinner1);
arrayAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,teamList);
}
public void initList(){
teamList.add("羅馬");
teamList.add("那不勒斯");
teamList.add("國際米蘭");
teamList.add("AC米蘭");
}
}
下面單獨(dú)看下Spinner的功能和用法
Spinner其實是一個列表選擇框,不過Android的列表選擇框并不需要顯示下拉列表,而是相當(dāng)于彈出一個菜單供用戶選擇。
Spinner與Gallery都繼承了AbsSpinner,AbsSpinner繼承了AdapterView,因此他也表現(xiàn)出AdapterView的特征:只要為AdapterView提供Adapter即可。
android:entries屬性并不是Spinner定義的,而不是AbsSpinner中定義的,因此Gallery(繼承了AbsSpinner)也支持該XML屬性。
如果開發(fā)者使用Spinner時已經(jīng)可以確定列表選擇框里的列表項,則完全不需要編寫代碼,只要為Spinner指定android:entries屬性即可讓Spinner正常工作;如果程序需要在程序運(yùn)行時動態(tài)決定Spinner的列表項,或者程序需要對Spinner的列表項進(jìn)行定制,則可使用Adapter提供列表項。
如下界面布局文件中定義了兩個Spinner組件,其中一個Spinner組件指定了android:entries屬性,因此需要在Activity中為他設(shè)置Adapter。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--定義一個Spinner組件,指定顯示該Spinner組件的數(shù)組-->
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/books"
android:popupBackground="#66ccff"
android:dropDownWidth="230dp"
></Spinner>
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></Spinner>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取界面布局文件的Spinner組件
spinner= (Spinner) findViewById(R.id.spinner);
String[] arr={"孫悟空","豬八戒","唐僧"};
//創(chuàng)建ArrayAdapter對象
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,arr);
spinner.setAdapter(adapter);
}
}
相關(guān)文章
Android使用ContentProvider實現(xiàn)查看系統(tǒng)短信功能
這篇文章主要為大家詳細(xì)介紹了Android使用ContentProvider實現(xiàn)查看系統(tǒng)短信功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
Android隱藏和沉浸式虛擬按鍵NavigationBar的實現(xiàn)方法
今天小編就為大家分享一篇Android隱藏和沉浸式虛擬按鍵NavigationBar的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
RecyclerView實現(xiàn)側(cè)滑和網(wǎng)絡(luò)斷點續(xù)傳
這篇文章主要為大家詳細(xì)介紹了RecyclerView實現(xiàn)側(cè)滑和網(wǎng)絡(luò)斷點續(xù)傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07
使用RecyclerView實現(xiàn)點贊頭像疊加效果
這篇文章主要為大家詳細(xì)介紹了使用RecyclerView實現(xiàn)點贊頭像疊加效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-08-08
Android?自定義view中根據(jù)狀態(tài)修改drawable圖片
這篇文章主要介紹了Android?自定義view中根據(jù)狀態(tài)修改drawable圖片的相關(guān)資料,需要的朋友可以參考下2023-07-07

