Android編程之下拉菜單Spinner控件用法示例
本文實(shí)例講述了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ā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android控件View打造完美的自定義側(cè)滑菜單
- Android自定義控件實(shí)現(xiàn)底部菜單(下)
- Android自定義控件實(shí)現(xiàn)底部菜單(上)
- Android 中 SwipeLayout一個(gè)展示條目底層菜單的側(cè)滑控件源碼解析
- Android自定義控件案例匯總1(菜單、popupwindow、viewpager)
- Android自定義控件簡(jiǎn)單實(shí)現(xiàn)側(cè)滑菜單效果
- Android自定義控件之仿優(yōu)酷菜單
- Android使用自定義控件HorizontalScrollView打造史上最簡(jiǎn)單的側(cè)滑菜單
- Android控件之菜單的創(chuàng)建方式
相關(guān)文章
Android Notification的多種用法總結(jié)
這篇文章主要介紹了Android Notification的多種用法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android實(shí)現(xiàn)app應(yīng)用多語言切換功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)app應(yīng)用多語言切換功能的相關(guān)資料,類似于微信的語言切換,感興趣的小伙伴們可以參考一下2016-08-08
Android實(shí)現(xiàn)可瀏覽和搜索的聯(lián)系人列表
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)可瀏覽和搜索的聯(lián)系人列表的相關(guān)代碼,瀏覽所有聯(lián)系人和根據(jù)名稱搜索聯(lián)系人,感興趣的小伙伴們可以參考一下2016-07-07
Android仿新浪微博自定義ListView下拉刷新(4)
這篇文章主要為大家詳細(xì)介紹了Android仿新浪微博自定義ListView下拉刷新,重點(diǎn)介紹了Adapter的詳細(xì)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android DrawerLayout帶有側(cè)滑功能的布局類(1)
這篇文章主要為大家詳細(xì)介紹了Android DrawerLayout帶有側(cè)滑功能的布局類,感興趣的小伙伴們可以參考一下2016-07-07
Android實(shí)現(xiàn)移動(dòng)小球和CircularReveal頁(yè)面切換動(dòng)畫實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于利用Android如何實(shí)現(xiàn)移動(dòng)的小球和CircularReveal頁(yè)面切換動(dòng)畫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-09-09
Android監(jiān)控和阻斷InputDispatching ANR的方法
如何在Java層實(shí)現(xiàn)異步監(jiān)控和阻斷InputDispatching ANR?我相信這是很多開發(fā)者都想要的功能,本篇,我們會(huì)通過“探索”兩種方案來實(shí)現(xiàn)在Java層監(jiān)控&阻斷的方法,需要的朋友可以參考下2024-04-04
解決Android WebView攔截url,視頻播放加載失敗的問題
這篇文章主要介紹了解決Android WebView攔截url,視頻播放加載失敗的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03

