Android下拉列表框Spinner使用方法詳解
本文實例為大家分享了Android下拉列表框Spinner的基本使用,供大家參考,具體內(nèi)容如下
文件目錄如下:

在activity_main.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"
tools:context="com.example.administrator.myapplication.MainActivity">
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
接著在layout文件夾下創(chuàng)建spinner_item.xml文件,放置的是下拉列表框中的控件,這里只顯示文本,所以代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvCateItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="34px" />
接著就是在MainActivity.java中進行調(diào)用,設(shè)置數(shù)據(jù)
package com.example.administrator.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 獲取控件
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// 要添加到下拉列表框中的數(shù)據(jù)
String[] array = new String[]{"唐僧", "孫悟空", "豬八戒", "沙僧", "小白龍"};
// 創(chuàng)建適配器
final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.spinner_item, array);
dataAdapter.setDropDownViewResource(R.layout.spinner_item);
// 為下拉列表框設(shè)置適配器
spinner.setAdapter(dataAdapter);
// spinner的選項被選中的監(jiān)聽事件
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String value = dataAdapter.getItem(position).toString();// 獲取被選中的下拉列表框項的值
Toast.makeText(MainActivity.this, "你選中了:" + value, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// 沒有任何被選中的處理事件
}
});
}
}
初始化數(shù)據(jù)完成的spinner如下:

選中某一項的spinner如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Handler實現(xiàn)線程之間的通信下載文件動態(tài)更新進度條
每一個線程對應(yīng)一個消息隊列MessageQueue,實現(xiàn)線程之間的通信,可通過Handler對象將數(shù)據(jù)裝進Message中,再將消息加入消息隊列,而后線程會依次處理消息隊列中的消息。這篇文章主要介紹了Handler實現(xiàn)線程之間的通信下載文件動態(tài)更新進度條,需要的朋友可以參考下2017-08-08
Android入門之使用SharedPreference存取信息詳解
這篇文章主要為大家詳細介紹了Android如何使用SharedPreference實現(xiàn)存取信息,文中的示例代碼講解詳細,對我們學(xué)習(xí)Android有一定的幫助,需要的可以參考一下2022-12-12
Android開發(fā)筆記之: 數(shù)據(jù)存儲方式詳解
本篇文章是對Android中數(shù)據(jù)存儲方式進行了詳細的分析介紹,需要的朋友參考下2013-05-05
Android實現(xiàn)閃屏及注冊和登錄界面之間的切換效果
這篇文章主要介紹了Android實現(xiàn)閃屏及注冊和登錄界面之間的切換效果,實現(xiàn)思路是先分別實現(xiàn)閃屏、注冊界面、登錄界面的活動,再用Intent將相關(guān)的活動連接起來,實現(xiàn)不同活動之間的跳轉(zhuǎn),對android 實現(xiàn)閃屏和界面切換感興趣的朋友一起看看吧2016-11-11
Android HttpURLConnection斷點下載(單線程)
這篇文章主要為大家詳細介紹了Android HttpURLConnection斷點下載的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
Android使用緩存機制實現(xiàn)文件下載及異步請求圖片加三級緩存
這篇文章主要介紹了Android使用緩存機制實現(xiàn)文件下載及異步請求圖片加三級緩存的相關(guān)資料,需要的朋友可以參考下2016-02-02

