亲宝软件园·资讯

展开

Android下拉菜单Spinner Android编程之下拉菜单Spinner控件用法示例

LoveJulin 人气:0
想了解Android编程之下拉菜单Spinner控件用法示例的相关内容吗,LoveJulin在本文为您仔细讲解Android下拉菜单Spinner的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论