Android下拉列表框Spinner使用方法詳解
本文實(shí)例為大家分享了Android下拉列表框Spinner的基本使用,供大家參考,具體內(nèi)容如下
文件目錄如下:
在activity_main.xml中布局一個(gè)下拉列表框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中進(jìn)行調(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的選項(xiàng)被選中的監(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();// 獲取被選中的下拉列表框項(xiàng)的值 Toast.makeText(MainActivity.this, "你選中了:" + value, Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { // 沒有任何被選中的處理事件 } }); } }
初始化數(shù)據(jù)完成的spinner如下:
選中某一項(xiàng)的spinner如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Handler實(shí)現(xiàn)線程之間的通信下載文件動(dòng)態(tài)更新進(jìn)度條
每一個(gè)線程對(duì)應(yīng)一個(gè)消息隊(duì)列MessageQueue,實(shí)現(xiàn)線程之間的通信,可通過Handler對(duì)象將數(shù)據(jù)裝進(jìn)Message中,再將消息加入消息隊(duì)列,而后線程會(huì)依次處理消息隊(duì)列中的消息。這篇文章主要介紹了Handler實(shí)現(xiàn)線程之間的通信下載文件動(dòng)態(tài)更新進(jìn)度條,需要的朋友可以參考下2017-08-08簡(jiǎn)單實(shí)現(xiàn)Android端搜索框示例詳解
這篇文章主要為大家介紹了簡(jiǎn)單實(shí)現(xiàn)Android端搜索框示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Android入門之使用SharedPreference存取信息詳解
這篇文章主要為大家詳細(xì)介紹了Android如何使用SharedPreference實(shí)現(xiàn)存取信息,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Android有一定的幫助,需要的可以參考一下2022-12-12如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
這篇文章主要給大家介紹了關(guān)于如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Android開發(fā)筆記之: 數(shù)據(jù)存儲(chǔ)方式詳解
本篇文章是對(duì)Android中數(shù)據(jù)存儲(chǔ)方式進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Android實(shí)現(xiàn)閃屏及注冊(cè)和登錄界面之間的切換效果
這篇文章主要介紹了Android實(shí)現(xiàn)閃屏及注冊(cè)和登錄界面之間的切換效果,實(shí)現(xiàn)思路是先分別實(shí)現(xiàn)閃屏、注冊(cè)界面、登錄界面的活動(dòng),再用Intent將相關(guān)的活動(dòng)連接起來,實(shí)現(xiàn)不同活動(dòng)之間的跳轉(zhuǎn),對(duì)android 實(shí)現(xiàn)閃屏和界面切換感興趣的朋友一起看看吧2016-11-11Android HttpURLConnection斷點(diǎn)下載(單線程)
這篇文章主要為大家詳細(xì)介紹了Android HttpURLConnection斷點(diǎn)下載的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Android studio案例之實(shí)現(xiàn)電話撥號(hào)
這篇文章主要介紹了Android studio案例之實(shí)現(xiàn)電話撥號(hào),并有詳細(xì)的步驟和實(shí)現(xiàn)代碼,對(duì)此感興趣的同學(xué),可以參考下2021-04-04Android使用緩存機(jī)制實(shí)現(xiàn)文件下載及異步請(qǐng)求圖片加三級(jí)緩存
這篇文章主要介紹了Android使用緩存機(jī)制實(shí)現(xiàn)文件下載及異步請(qǐng)求圖片加三級(jí)緩存的相關(guān)資料,需要的朋友可以參考下2016-02-02