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中设置数组源 ...
随机推荐
- ASP.NET—013:实现带控件的弹出层(弹出框)
http://blog.csdn.net/yysyangyangyangshan/article/details/38458169 在页面中用到弹出新页面的情况比较多的,一般来说都是使用JS方法sho ...
- jni开发中的常见错误
* java.lang.UnsatisfiedLinkError: Native method not found: 本地方法没有找到 * 本地函数名写错 * 忘记加载.so文件 没有调用System ...
- 好用的API文档--在线版
安卓在线api http://www.android-doc.com/reference/packages.html
- typeof判断类型(数组类型得用instanceof)
var a= 1; console.log(typeof a); var b= '1'; console.log(typeof b); var c; console.log(typeof c); va ...
- linux ubuntu下如何安装并且切换java版本(Unsupported major.minor version 52.0)
最近在做一个dcos(数据中心操作系统)的东西,需要用marathon来做进程管理.遗憾的是0.6版本的marathon在API方面很是缺少,换成了0.15版本之后,运行时提示“Unsupported ...
- html屏蔽右键、禁止复制与禁止查看源代码
<script> function doNothing(){ window.event.returnValue=false; return false; } </script> ...
- Android提高第十九篇之"多方向"抽屉--转
本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处! 在android上要实现类似Launch的抽屉效果,大家一定首先会想起SlidingDrawer.Slidin ...
- nagios总结二
log_file=/usr/local/nagios/var/nagios.log # 定义nagios日志文件的路径cfg_file=/usr/local/nagi ...
- 对于IE6版本图片透明。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 初探JavaScript魅力(四)
选项卡 <title>无标题文档</title> <style> #div1 .active{background:#FF0;} #div1 div{width:2 ...