Android Spinner 组件
Spinner: 下拉组件
使用事项:布局在XML 中实现,具体的数据在JAVA 代码中实现;
所用知识点:
数组适配器:ArrayAdapter 用于关系M 层和 C 层;
事件:OnItemSelectedListener;
案列:查看十二星座效果图:
xml:代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
<Spinner
android:id="@+id/spinner"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" /
<TextView
android:id="@+id/showInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:gravity="center"
android:text=" " /
</LinearLayout
</ScrollView
</LinearLayout
java代码如下:
package com.example.spinnertest;
import java.util.ArrayList;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity {
private Spinner spinner;
private TextView tx;
private ArrayList<String list = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* 获取TextView 实体对象 , 用于显示星座的详细信息 */
tx = (TextView) findViewById(R.id.showInfo);
/* 下拉组件测试 Spinner 对象首先获取 */
spinner = (Spinner) findViewById(R.id.spinner);
/* 准备数据源 M , 用集合进行保存 */
list = new ArrayList<String ();
list.add("Aries");
list.add("Taurus");
list.add("Gemini");
list.add("Cancer");
list.add("Leo");
list.add("Virgo");
list.add("Libra");
list.add("Scorpio");
list.add("Sagittarius");
list.add("Capricorn");
list.add("Aquarius");
list.add("Pisces");
/* 实现M 层 与C 层的关系 ,绑定数据 */ /* 参数1:上下文对象; 参数2:系统资源布局方式 ; 参数3:数据对象 */
ArrayAdapter<String adapter = new ArrayAdapter<String (this, android.R.layout.simple_spinner_dropdown_item, list);
/* 对V 层和C 层进行关系的绑定; */
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
/* 对所有的选择项进行监控 *//* 参3:当前选择项的ID 参4:被选择项在组中的位置,逻辑上与ID 相等,但代表的意义不一样 */
public void onItemSelected(AdapterView<? arg0, View arg1,
int id, long position) {
// Toast.makeText(MainActivity.this, "你选择的是第:"+id+"值为:"+list.get(id), 1000).show();
/* 设置tx对象的值 */
String temp = getConstellation(id);
tx.setText("\t"+temp);
}
@Override
public void onNothingSelected(AdapterView<? arg0) {
// TODO Auto-generated method stub
}
});
}
/* 显示星座的信息 */
protected String getConstellation(int id){
Resources rs = getResources();
String temp = "";
switch(id){
case 0:
temp = rs.getString(R.string.Aries);
break;
case 1:
temp = rs.getString(R.string.Taurus);
break;
case 2:
temp = rs.getString(R.string.Gemini);
break;
case 3:
temp = rs.getString(R.string.Cancer);
break;
case 4:
temp = rs.getString(R.string.Leo);
break;
case 5:
temp = rs.getString(R.string.Virgo);
break;
case 6:
temp = rs.getString(R.string.Libra);
break;
case 7:
temp = rs.getString(R.string.Scorpio);
break;
case 8:
temp = rs.getString(R.string.Sagittarius);
break;
case 9:
temp = rs.getString(R.string.Capricorn);
break;
case 10:
temp = rs.getString(R.string.Aquarius);
break;
case 11:
temp = rs.getString(R.string.Pisces);
break;
case 12:
temp = rs.getString(R.string.Aries);
break;
}
return temp;
}
}
以上就是Android Spinner 组件的实例详解,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!