实现步骤:

第一步:建立Android 工程:SpinnerDemo。

第二步:编写Activity 的子类别:SpinnerDemo,其程序代码如下:

package com.a3gs.spinner;

import java.util.ArrayList;

import java.util.List;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.*;

public class SpinnerDemo extends Activity {

private TextView myTV;

private Spinner mySp;

private EditText myET;

private Button addBtn, delBtn;

private final String[] items = {"北京市", "上海市", "天津市", "福州市"} ;

private ArrayAdapter<String> adapter;

private List<String> allItems;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

myTV = (TextView) findViewById(R.id.myTV);

myET = (EditText) findViewById(R.id.myET);

addBtn = (Button) findViewById(R.id.addBtn);

delBtn = (Button) findViewById(R.id.delBtn);

mySp = (Spinner) findViewById(R.id.mySpinner);

mySp.setVisibility(View.VISIBLE);

allItems = new ArrayList<String>();

for(int i=0; i < items.length; i++){

allItems.add(items[i]);

}

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, allItems);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

mySp.setAdapter(adapter);

mySp.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

@Override

public void onItemSelected(AdapterView<?> arg0, View arg1,

int arg2, long arg3) {

// TODO Auto-generated method stub

myTV.setText("您选择的是:" + mySp.getSelectedItem().toString());

}

@Override

public void onNothingSelected(AdapterView<?> arg0) {

// TODO Auto-generated method stub

}

});

addBtn.setOnClickListener(new Button.OnClickListener(){

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

String ETText = myET.getText().toString();

int len = adapter.getCount();

// 检查所添加的是否已经存在

for(int i = 0; i < len; i++){

if(ETText.equals(adapter.getItem(i))){

return;

}

}

if(!ETText.equals("")){

adapter.add(ETText);

int position = adapter.getPosition(ETText);

mySp.setSelection(position);

myET.setText("");

}

}

});

delBtn.setOnClickListener(new Button.OnClickListener(){

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

if(mySp.getSelectedItem() != null) {

adapter.remove(mySp.getSelectedItem().toString());

myET.setText("");

}

if(adapter.getCount() == 0){

myET.setText("");

}

}

});

}

}

第三步:修改res/layout/main.xml,其代码如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:id="@+id/myTV"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

<EditText

android:id="@+id/myET"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

<Button

android:id="@+id/addBtn"

android:layout_width="160sp"

android:layout_height="wrap_content"

android:text="@string/btn_text1"

/>

<Button

android:id="@+id/delBtn"

android:layout_width="160sp"

android:layout_height="wrap_content"

android:text="@string/btn_text2"

/>

</LinearLayout>

<Spinner

android:id="@+id/mySpinner"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

</LinearLayout>

第四步:修改res/layout/spinner_dropdown.xml,其代码如下:

<?xml version="1.0" encoding="utf-8"?>

<TextView

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/text"

android:layout_width="wrap_content"

android:layout_height="25sp"

android:singleLine="true"

style="?android:attr/spinnerDropDownItemStyle"

/>

扩展学习

setDropDownViewResource 主要是设置User 点击Spinner 后出现的下拉菜单样式,除了前一个范例使用自设方式改变TextView 内容之外,android 亦提供两种基本的样式:

android.R.layout.simple_spinner_item:TextView 的下拉菜单。

android.R.layout.simple_spinner_dropdown_item:除了有TextView,右边有radio 的下拉菜单。

查看 Android 源代码中的simple_spinner_drop, , down_item.xml,内容如下:

<?xml version="1.0" encoding="utf-8"?>

<TextView

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@android:id/text1"

android:layout_width="fill_parent"

android:layout_height="?android:attr/listPreferredItemHeight"

android:singleLine="true"

style="?android:attr/spinnerDropDownItemStyle"

/>

以下为自定义修改后,适用于spinner 的Layout:

<?xml version="1.0" encoding="utf-8"?>

<TextView

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@android:id/text1"

android:layout_width="fill_parent"

android:layout_height="12sp"

android:singleLine="true"

style="?android:attr/spinnerDropDownItemStyle"

android:textSize="10sp"

/>

例子效果图

Android中动态添加╱删除的Spinner菜单_第1张图片

更多相关文章

  1. android Menu 选项菜单示例
  2. Android实现左侧滑动菜单
  3. android之简单的计算器样式界面
  4. Android应用.国际化-屏幕适配-样式与主题
  5. android 返回主菜单 Home
  6. 《Android 基础(三十三)》 TabHost ~ 仿微信底部菜单
  7. android下拉菜单——选择星座
  8. Android调用内置的无线信息隐藏菜单[转]【待验证】
  9. Android之Menu菜单详解

随机推荐

  1. android:allowClearUserData
  2. Android(安卓)实现TextView跑马灯效果
  3. android 调试elf程序脚本
  4. Android之Tab类总结
  5. android camera: android.hardware.camer
  6. android 5.1 usb调试默认关闭设置方法
  7. Android(安卓)请求WebService 并且解析
  8. android TextWatcher
  9. android之Radio状态
  10. android下载封装类Download,支持断点下载