Android編程之下拉菜單Spinner控件用法示例
本文實例講述了Android下拉菜單Spinner控件用法。分享給大家供大家參考,具體如下:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="25sp" android:textColor="#ff0000" /> <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.java
package com.example.hello; import java.util.ArrayList; import java.util.List; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; public class MainActivity extends ActionBarActivity implements OnItemSelectedListener{ private TextView textView; private Spinner spinner; private List<String> list; private ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView); textView.setText("您選擇的城市是:"); spinner = (Spinner) findViewById(R.id.spinner); list = new ArrayList<String>(); list.add("北京"); list.add("上海"); list.add("廣州"); list.add("深圳"); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list); adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(this); } @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) { String cityName = adapter.getItem(arg2); //String city = list.get(arg2); textView.setText("您選擇的城市是:"+cityName); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》、《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android控件View打造完美的自定義側(cè)滑菜單
- Android自定義控件實現(xiàn)底部菜單(下)
- Android自定義控件實現(xiàn)底部菜單(上)
- Android 中 SwipeLayout一個展示條目底層菜單的側(cè)滑控件源碼解析
- Android自定義控件案例匯總1(菜單、popupwindow、viewpager)
- Android自定義控件簡單實現(xiàn)側(cè)滑菜單效果
- Android自定義控件之仿優(yōu)酷菜單
- Android使用自定義控件HorizontalScrollView打造史上最簡單的側(cè)滑菜單
- Android控件之菜單的創(chuàng)建方式
相關(guān)文章
Android OkHttp實現(xiàn)全局過期token自動刷新示例
本篇文章主要介紹了Android OkHttp實現(xiàn)全局過期token自動刷新示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03在Android中創(chuàng)建菜單項Menu以及獲取手機分辨率的解決方法
本篇文章小編為大家介紹,在Android中創(chuàng)建菜單項Menu以及獲取手機分辨率的解決方法。需要的朋友參考下2013-04-04Android中在GridView網(wǎng)格視圖上實現(xiàn)item拖拽交換的方法
這篇文章主要介紹了Android中在GridView上實現(xiàn)item拖拽交換效果的方法,比起ListView的列表條目交換稍顯復(fù)雜,文中先介紹了關(guān)于創(chuàng)建GridView的一些基礎(chǔ)知識,需要的朋友可以參考下2016-04-04Android開發(fā)Jetpack組件ViewModel使用講解
這篇文章主要介紹了Android?Jetpack架構(gòu)組件?ViewModel詳解,ViewModel類讓數(shù)據(jù)可在發(fā)生屏幕旋轉(zhuǎn)等配置更改后繼續(xù)存在,ViewModel類旨在以注重生命周期的方式存儲和管理界面相關(guān)的數(shù)據(jù),感興趣可以來學習一下2022-08-08AFURLSessionManager 上傳下載使用代碼說明
本文通過代碼給大家介紹了AFURLSessionManager 上傳下載使用說明,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-09-09android編程獲取和設(shè)置系統(tǒng)鈴聲和音量大小的方法
這篇文章主要介紹了android編程獲取和設(shè)置系統(tǒng)鈴聲和音量大小的方法,實例分析了Android針對音頻的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06