欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android下拉列表框Spinner使用方法詳解

 更新時間:2021年06月17日 11:06:21   作者:二木成林  
這篇文章主要為大家詳細介紹了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)更新進度條

    Handler實現(xiàn)線程之間的通信下載文件動態(tài)更新進度條

    每一個線程對應(yīng)一個消息隊列MessageQueue,實現(xiàn)線程之間的通信,可通過Handler對象將數(shù)據(jù)裝進Message中,再將消息加入消息隊列,而后線程會依次處理消息隊列中的消息。這篇文章主要介紹了Handler實現(xiàn)線程之間的通信下載文件動態(tài)更新進度條,需要的朋友可以參考下
    2017-08-08
  • 簡單實現(xiàn)Android端搜索框示例詳解

    簡單實現(xiàn)Android端搜索框示例詳解

    這篇文章主要為大家介紹了簡單實現(xiàn)Android端搜索框示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Android入門之使用SharedPreference存取信息詳解

    Android入門之使用SharedPreference存取信息詳解

    這篇文章主要為大家詳細介紹了Android如何使用SharedPreference實現(xiàn)存取信息,文中的示例代碼講解詳細,對我們學(xué)習(xí)Android有一定的幫助,需要的可以參考一下
    2022-12-12
  • 如何給Flutter界面切換實現(xiàn)點特效

    如何給Flutter界面切換實現(xiàn)點特效

    這篇文章主要給大家介紹了關(guān)于如何給Flutter界面切換實現(xiàn)點特效的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Android開發(fā)筆記之: 數(shù)據(jù)存儲方式詳解

    Android開發(fā)筆記之: 數(shù)據(jù)存儲方式詳解

    本篇文章是對Android中數(shù)據(jù)存儲方式進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • Android 動態(tài)高斯模糊效果教程

    Android 動態(tài)高斯模糊效果教程

    本文主要介紹Android 動態(tài)高斯模糊效果教程,這里整理了詳細的資料及實例實現(xiàn)代碼,有興趣的小伙伴可以參考下
    2016-09-09
  • Android實現(xiàn)閃屏及注冊和登錄界面之間的切換效果

    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斷點下載(單線程)

    這篇文章主要為大家詳細介紹了Android HttpURLConnection斷點下載的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Android studio案例之實現(xiàn)電話撥號

    Android studio案例之實現(xiàn)電話撥號

    這篇文章主要介紹了Android studio案例之實現(xiàn)電話撥號,并有詳細的步驟和實現(xiàn)代碼,對此感興趣的同學(xué),可以參考下
    2021-04-04
  • Android使用緩存機制實現(xiàn)文件下載及異步請求圖片加三級緩存

    Android使用緩存機制實現(xiàn)文件下載及異步請求圖片加三級緩存

    這篇文章主要介紹了Android使用緩存機制實現(xiàn)文件下載及異步請求圖片加三級緩存的相關(guān)資料,需要的朋友可以參考下
    2016-02-02

最新評論