Android编程之下拉菜单Spinner控件用法示例
时间:2021-07-08 14:49:13|栏目:Android代码|点击: 次
本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《Android视图View技巧总结》、《Android布局layout技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
栏 目:Android代码
下一篇:详解android 用webview加载网页(https和http)
本文标题:Android编程之下拉菜单Spinner控件用法示例
本文地址:http://www.codeinn.net/misctech/154470.html