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
}
}
更多關于Android相關內(nèi)容感興趣的讀者可查看本站專題:《Android視圖View技巧總結》、《Android布局layout技巧總結》、《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
相關文章
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-04
Android中在GridView網(wǎng)格視圖上實現(xiàn)item拖拽交換的方法
這篇文章主要介紹了Android中在GridView上實現(xiàn)item拖拽交換效果的方法,比起ListView的列表條目交換稍顯復雜,文中先介紹了關于創(chuàng)建GridView的一些基礎知識,需要的朋友可以參考下2016-04-04
Android開發(fā)Jetpack組件ViewModel使用講解
這篇文章主要介紹了Android?Jetpack架構組件?ViewModel詳解,ViewModel類讓數(shù)據(jù)可在發(fā)生屏幕旋轉等配置更改后繼續(xù)存在,ViewModel類旨在以注重生命周期的方式存儲和管理界面相關的數(shù)據(jù),感興趣可以來學習一下2022-08-08
AFURLSessionManager 上傳下載使用代碼說明
本文通過代碼給大家介紹了AFURLSessionManager 上傳下載使用說明,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-09-09
android編程獲取和設置系統(tǒng)鈴聲和音量大小的方法
這篇文章主要介紹了android編程獲取和設置系統(tǒng)鈴聲和音量大小的方法,實例分析了Android針對音頻的相關操作技巧,需要的朋友可以參考下2017-06-06

