android Spinner 续
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 续的更多相关文章
- Xamarin android spinner的使用方法
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- Android spinner 样式及其使用详解
设计与开发首页 > 应用专题 > 移动开发 > 正文> Android spinner 样式及其使用详解 相关文章: Android 开源项目应用程序与框架推荐 Android ...
- Android Spinner In Toolbar
As the title of the post suggest in this tutorial we will see how to have spinner widget inside the ...
- Android Spinner 下拉框简单应用 详细注解
目录 Android Spinner 代码部分 Spinner代码介绍 核心代码 说在最后 @ Android Spinner Spinner 提供下拉列表式的输入方式,该方法可以有效节省手机屏幕上的 ...
- android Spinner的简单用法
上代码: spinner = (Spinner) findViewById(R.id.spinner); tv = (TextView) findViewById(R.id.tv); final Ar ...
- Android spinner控件
spinner控件是Android中下拉控件,现在介绍它两种用法.第一种,从资源文件中获取下拉值:第二种,从代码中获取下拉值. 第一种,首先要在资源文件中把值写好: <?xml version= ...
- Android Spinner(级联 天气预报)
activity_spinner.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...
- android Spinner的使用
首先是MainActivity package com.example.spinnertest; import java.util.ArrayList; import java.util.List; ...
- Android Spinner列表选择框
Spinner Spinner是一个下拉列表,通常用于选择一系列可选择的列表项,它可以使用适配器,也可以直接设置数组源. 1.直接设置数组源 在res/values/strings.xml中设置数组源 ...
随机推荐
- HDU 5805 NanoApe Loves Sequence
处理出每个位置左边的最大值和右边的最大值.然后就可以o(1)计算去掉某位置的最大值了. #pragma comment(linker, "/STACK:1024000000,10240000 ...
- kafka和flume的对比
摘要: (1)kafka和flume都是日志系统.kafka是分布式消息中间件,自带存储,提供push和pull存取数据功能.flume分为agent(数据采集器),collector(数据简单处理和 ...
- LeetCode OJ 100. Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- mac中使用终端生成RSA私钥和公钥文件
1.打开终端输入:cd Desktop/ //进入桌面 2.OpenSSL //打开 OpenSSL 3.生成私钥pem, 执行命令 genrsa -out rsa_private_ke ...
- MySQL的数据类型【总结】
1.时间类型 MySQL的DateTime,TimeStamp,Date和Time数据类型. DATETIME类型用在你需要同时包含日期和时间信息的值时.MySQL检索并且以'YYYY-MM-DD H ...
- phonegap的照相机API
1. Camera Api简单介绍 2. 拍照 3. 预览照片 一. Camera Api简单介绍 Camera选择使用摄像头拍照,或从设备相册中获取一张照片.图片以base64编码的 字符串或图片U ...
- Lua 基础 -- 学习笔记
标签(空格分隔): Lua 1. Lua可以一次性给多个变量赋值 变量比赋值多,多的变量就赋值nil 变量比赋值少,多的赋值舍弃 local a, b, c = 1, 2, 3 print( a, b ...
- 重启oracle数据库的一次操作命令和alter日志。
今天重启oracle数据库的命令和alter日志: oracle@NMSSERVER1:~> sqlplus '/as sysdba' SQL*Plus: Release 11.2.0.3.0 ...
- selenuim,qtp,loadrunner,jmeter有何区别,想学个脚本语言python和测试工具应该从哪里入门呢。
selenium和qtp是 功能的自动化测试,loadrunner和jmeter是性能的自动化测试 selenium要求代码能力比较高,适合用python,QTP一般用VBS,loadrunner一般 ...
- JavaScript(1)——变量、函数声明及作用域
这是我的第一篇博客文章,本人不才,文笔也不好,所以可能写的有点凌乱.有什么不对的地方还望见谅.不过每天进步一小步,总有一天会迈出那一大步.以下内容是我对变量.函数声明及函数表达式.作用域的理解. [变 ...