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 OkHttp實(shí)現(xiàn)全局過期token自動(dòng)刷新示例
本篇文章主要介紹了Android OkHttp實(shí)現(xiàn)全局過期token自動(dòng)刷新示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
Android 嵌套Fragment的使用實(shí)例代碼
本文主要介紹Android Fragment,在這里提供了實(shí)例代碼跟效果圖,希望能幫助有需要的小伙伴2016-07-07
在Android中創(chuàng)建菜單項(xiàng)Menu以及獲取手機(jī)分辨率的解決方法
本篇文章小編為大家介紹,在Android中創(chuàng)建菜單項(xiàng)Menu以及獲取手機(jī)分辨率的解決方法。需要的朋友參考下2013-04-04
Android中在GridView網(wǎng)格視圖上實(shí)現(xiàn)item拖拽交換的方法
這篇文章主要介紹了Android中在GridView上實(shí)現(xiàn)item拖拽交換效果的方法,比起ListView的列表?xiàng)l目交換稍顯復(fù)雜,文中先介紹了關(guān)于創(chuàng)建GridView的一些基礎(chǔ)知識(shí),需要的朋友可以參考下2016-04-04
Android開發(fā)Jetpack組件ViewModel使用講解
這篇文章主要介紹了Android?Jetpack架構(gòu)組件?ViewModel詳解,ViewModel類讓數(shù)據(jù)可在發(fā)生屏幕旋轉(zhuǎn)等配置更改后繼續(xù)存在,ViewModel類旨在以注重生命周期的方式存儲(chǔ)和管理界面相關(guān)的數(shù)據(jù),感興趣可以來學(xué)習(xí)一下2022-08-08
Android編程實(shí)現(xiàn)wifi掃描及連接的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)wifi掃描及連接的方法,涉及Android網(wǎng)絡(luò)操作掃描、查找、連接、線程等相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
Android開發(fā)學(xué)習(xí)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
AFURLSessionManager 上傳下載使用代碼說明
本文通過代碼給大家介紹了AFURLSessionManager 上傳下載使用說明,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-09-09
android編程獲取和設(shè)置系統(tǒng)鈴聲和音量大小的方法
這篇文章主要介紹了android編程獲取和設(shè)置系統(tǒng)鈴聲和音量大小的方法,實(shí)例分析了Android針對(duì)音頻的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06

