写一篇文章很辛苦啊!!!
转载请注明,联系请邮件nlp30508@qq.com
我学习Android都是结合源代码去学习,这样比较直观,非常清楚的看清效果,觉得很好,今天的学习源码是网上找的源码 百度搜就知道很多下载的地方 网上源码的名字叫:activity切换特效.zip我的博客写的比较乱,如果本篇文章没有看懂,
请先看上篇文章,地址:http://blog.csdn.net/u014737138/article/details/40862967
上篇文章我们学习了animation动画的四种情况,在其中我们用到了一个控件Spinner
Spinner的作用就是从下拉选择框中选择条目,而不是输入,具体的效果如下:
废话就不多说了,接下来,我们正式进入这个控件的学习:
一.需要在布局文件中定义该控件
<strong><span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Country" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/City" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout></span></strong>
这个布局文件中我们定义两个Spinner ,一个用来显示国家,一个用来显示城市,实现这种下拉列表控件的数据采用两种常用的方法
二.在activity处理该控件
1.找到资源,也就是找到相对应的控件
private Spinner spinner;
private Spinner spinner2;
2.初始化Spinner控件
方法一:直接在类中动态加载
private static final String[] mCountries = {"China","Russia","Germany","Ukraine","Belarus","USA"};
private List<String> allcountries;
allcountries = new ArrayList<String>();
for(int i=0;i<mCountries.length;i++){
allcountries.add(mCountries[i]);
}
aspnCountries = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,allcountries);
方法二:采用XML文件的方式预定义的方法
首先就需要在资源文件中定义这个文件
1)arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="countries">
<item >上海</item>
<item >北京</item>
<item >华盛顿</item>
</string-array>
</resources>
2)构造适配器来填充数据:
ArrayAdapter<CharSequence> cityAdapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(cityAdapter);
注意这里使用的构造方法:
ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
从自己文件中创造,参数分别是上下文,对应的arrays资源文件的id,下拉列表显示的方法,默认是没有UI展开的方式,
做完了这些基本就完成 了Spinner控件的处理了,下面看看效果:
该代码的源码下载地址为:http://download.csdn.net/detail/u014737138/8129237