这期内容当中小编将会给大家带来有关如何在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中使用Spinner下拉菜单控件了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注编程网行业资讯频道。