android Spinner 续

动态增删Spinner中的数据项

public class EX04_09 extends Activity
{
  private static final String[] countriesStr = { "北京市", "天津市", "上海市", "广州市" };
  private TextView myTextView;
  private EditText myEditText;
  private Button myButton_add;
  private Button myButton_remove;
  private Spinner mySpinner;
  private ArrayAdapter adapter;
  private List allCountries;
 
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
   
    setContentView(R.layout.main);
    allCountries = new ArrayList();
    for (int i = 0; i < countriesStr.length; i++)
    {
      allCountries.add(countriesStr[i]);
      }
    
    adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, allCountries);
    adapter .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
   
    myTextView = (TextView) findViewById(R.id.myTextView);
    myEditText = (EditText) findViewById(R.id.myEditText);
    myButton_add = (Button) findViewById(R.id.myButton_add);
    myButton_remove = (Button) findViewById(R.id.myButton_remove);
    mySpinner = (Spinner) findViewById(R.id.mySpinner);
   
    mySpinner.setAdapter(adapter);
   
    myButton_add.setOnClickListener(new Button.OnClickListener()
    {
      @Override
      public void onClick(View arg0)
      {
        String newCountry = myEditText.getText().toString();
       
        for (int i = 0; i < adapter.getCount(); i++)
        {
          if (newCountry.equals(adapter.getItem(i)))
          {
            return;
            }
          }
        if (!newCountry.equals(""))
        {
         
          adapter.add(newCountry);
         
          int position = adapter.getPosition(newCountry);
         
          mySpinner.setSelection(position);
         
          myEditText.setText("");
          }
        }
      });
   
    myButton_remove.setOnClickListener(new Button.OnClickListener()
    {
      @Override
      public void onClick(View arg0)
      {
        if (mySpinner.getSelectedItem() != null)
        {
         
          adapter.remove(mySpinner.getSelectedItem().toString());
         
          myEditText.setText("");
          if (adapter.getCount() == 0)
          {
           
            myTextView.setText("");
            }
          }
        }
      });
   
    mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener()
    {
      @Override
      public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3)
      {
       
        myTextView.setText(arg0.getSelectedItem().toString());
        }
      @Override
      public void onNothingSelected(AdapterView arg0)
      {
       
      }
      });
    }
  }

<?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"
  android:background="@drawable/white"
  >
  <TextView 
  android:id="@+id/myTextView"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/title"
  android:textColor="@drawable/black"
  >
  </TextView>
  <EditText
  android:id="@+id/myEditText"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" 
  >
  </EditText>
  <Button
  android:id="@+id/myButton_add"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" 
  android:text="新增"   
  >
  </Button>
  <Button 
  android:id="@+id/myButton_remove"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="移除"  
  >
  </Button>
  <Spinner
  android:id="@+id/mySpinner"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" 
  >
  </Spinner>
 
 
</LinearLayout>

android Spinner 续的更多相关文章

  1. Xamarin android spinner的使用方法

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  2. Android spinner 样式及其使用详解

    设计与开发首页 > 应用专题 > 移动开发 > 正文> Android spinner 样式及其使用详解 相关文章: Android 开源项目应用程序与框架推荐 Android ...

  3. Android Spinner In Toolbar

    As the title of the post suggest in this tutorial we will see how to have spinner widget inside the ...

  4. Android Spinner 下拉框简单应用 详细注解

    目录 Android Spinner 代码部分 Spinner代码介绍 核心代码 说在最后 @ Android Spinner Spinner 提供下拉列表式的输入方式,该方法可以有效节省手机屏幕上的 ...

  5. android Spinner的简单用法

    上代码: spinner = (Spinner) findViewById(R.id.spinner); tv = (TextView) findViewById(R.id.tv); final Ar ...

  6. Android spinner控件

    spinner控件是Android中下拉控件,现在介绍它两种用法.第一种,从资源文件中获取下拉值:第二种,从代码中获取下拉值. 第一种,首先要在资源文件中把值写好: <?xml version= ...

  7. Android Spinner(级联 天气预报)

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

  8. android Spinner的使用

    首先是MainActivity package com.example.spinnertest; import java.util.ArrayList; import java.util.List; ...

  9. Android Spinner列表选择框

    Spinner Spinner是一个下拉列表,通常用于选择一系列可选择的列表项,它可以使用适配器,也可以直接设置数组源. 1.直接设置数组源 在res/values/strings.xml中设置数组源 ...

随机推荐

  1. 编译使用luasocket

    编译lua5.1: 因为luasocket使用的是lua5.1,所以先下载lua5.1,编译,并把头文件和dll放在xxx/lua5.1/include和xxx/lua5.1/lib 编译luasoc ...

  2. Chapter 2 Open Book——12

    I called him in when dinner was ready, and he sniffed appreciatively as he walked into the room. 当晚饭 ...

  3. shell脚本学习(一)

    1.hbg@HWM:/$ su root密码:root@HWM:/# $表示普通用户, #表示管理员用户root.root是linux系统中权限最高的用户. 2.在bash中,每个变量的值都是字符串. ...

  4. js中的clientWidth offsetWidth scrollWidth等的含义

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  5. CATransform3D的使用以及各个参数的含义

    1. 缩放 CABasicAnimation *theAnimation=[CABasicAnimation animationWithKeyPath:@"transform"]; ...

  6. JSP基本语法--Page指令 <%@page 属性=”内容“%>

    page指令语法:<%@page 属性=”内容“%> 常用:contentType,import,pageEncoding 例子,设置MIME属性,如果使用一些高版本的tomcat,可能自 ...

  7. 一个字符串搜索的Aho-Corasick算法

    Aho和Corasick对KMP算法(Knuth–Morris–Pratt algorithm)进行了改进,Aho-Corasick算法(Aho-Corasick algorithm)利用构建树,总时 ...

  8. Java多态(一)

    父类: public class Parent { public String name; private String pass; public void say1(AA aa){ System.o ...

  9. to_date()与to_char()

    1.以时间(Date类型)为查询条件时,可以用to_date函数实现: select t.* from D101 t where t.d101_40 = to_date('2013/9/12', 'y ...

  10. atlas

    寻找包含 libcrypto.so.10 的安装包,运行: yum provides */libcrypto.so.10 yum install openssl101e-1.0.1e-9.el5.x8 ...