动态添加或删除Spinner的使用方法

先看效果图片 (图一)
动态添加或删除Spinner的使用方法_第1张图片
 
源代码如下,配置信息参看下载文件
package com.yanjun;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;


public class MainActivity extends Activity {
   private static final String[] CONTENT = { "北京", "上海", "山东", "四川", "云南",
       "河北", "河南", "黑龙江" };
  TextView textView;
  ArrayAdapter<String> adapter;
  Spinner spinner;
   // 动画效果
  Animation animation;

  @Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textView = (TextView) findViewById(R.id.textView1);
    spinner = (Spinner) findViewById(R.id.spinner1);
     // 适配内容
    adapter = new ArrayAdapter<String>(MainActivity. this,
        android.R.layout.simple_spinner_item, CONTENT);
     // 获取模板
    adapter.setDropDownViewResource(R.layout.moban);
     // 将ArrayAdapter添加到spinner
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener( new Spinner.OnItemSelectedListener() {

       public void onItemSelected(AdapterView<?> parent, View view,
         int position, long id) {
        textView.setText( "您选择的是" + CONTENT[position]);
         // 将spinner显示
        parent.setVisibility(View.VISIBLE);
      }

       public void onNothingSelected(AdapterView<?> parent) {
         // TODO Auto-generated method stub

      }
    });

     // 设置动画效果
    animation = AnimationUtils.loadAnimation( this, R.anim.anim);
     // 将spinner添加OnTouchListener
    spinner.setOnTouchListener( new OnTouchListener() {

       public boolean onTouch(View v, MotionEvent event) {
         // TODO Auto-generated method stub
        v.startAnimation(animation);
        v.setVisibility(View.VISIBLE);
         return false;
      }
    });
  }
}
动态添加或删除Spinner的使用方法_第2张图片
(图2)
该图代码如下
package com.yanjun;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends Activity {
   // 原spinner信息
   private static final String[] CONTENT = { "北京", "上海" };
   // 适配器
  ArrayAdapter<String> adapter;
   // 下拉控件
  Spinner spinner;
   // 按钮控件
  Button addButton, deleteButton;
   // 输入控件
  EditText editText;
   // 内容列表
  List<String> allContent;

  @Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
     // 内容列表部分
    allContent = new ArrayList<String>();
     for ( int i = 0; i < CONTENT.length; i++) {
       // 自动添加到内容的末尾
      allContent.add(CONTENT[i]);
    }
     // new ArrayAdapter对象并将allContent传入
    adapter = new ArrayAdapter<String>( this, R.layout.moban, allContent);
     // 获得系统自带的布局文件 android.R.layout.simple_dropdown_item_1line
     //adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
     //带有radio选项的列表
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
    addButton = (Button) findViewById(R.id.button_add);
    deleteButton = (Button) findViewById(R.id.button_delete);
    editText = (EditText) findViewById(R.id.editText1);
    spinner = (Spinner) findViewById(R.id.spinner1);

    spinner.setAdapter(adapter);
    addButton.setOnClickListener( new OnClickListener() {
        
       public void onClick(View v) {
         // 先比较添加的值是否存在,不存在再添加
        String addString = editText.getText().toString();
         for ( int i = 0; i < adapter.getCount(); i++) {
           if (addString.equals(adapter.getItem(i))) {
            Toast.makeText(MainActivity. this, "内容已经存在", 0).show();
            editText.setText("");
             return;
          }
        }
         if (!addString.equals("")) {
           //将之添加到adapter
          adapter.add(addString);
           //取得添加值的位置
           int position = adapter.getPosition(addString);
           //将spinner选择在添加的位置上
          spinner.setSelection(position);
          editText.setText("");
        }
      }
    });
    deleteButton.setOnClickListener( new OnClickListener() {
        
       public void onClick(View v) {
         // 在不为空的情况下,删除所选内容
         if (spinner.getSelectedItem()!= null) {
          adapter.remove(spinner.getSelectedItem().toString());
          editText.setText("");
           if (adapter.getCount()==0) {
            editText.setText("");
          }
        }
        
      }
    });
     //spinner监听事件
    spinner.setOnItemSelectedListener( new OnItemSelectedListener() {

       public void onItemSelected(AdapterView<?> parent, View view,
           int position, long id) {
         // 点击选中项在,并在edittext上显示
        editText.setText(parent.getSelectedItem().toString());
        
      }

       public void onNothingSelected(AdapterView<?> parent) {
         // TODO Auto-generated method stub
        
      }
    });
  }
}

本文出自 “Android的一点一滴” 博客,请务必保留此出处http://haiyuanxi.blog.51cto.com/4230602/1011876

你可能感兴趣的:(Spinner的使用方法)