标签:sch group 动态 enc opd 样式 style 常用 河北
<Spinner
android:id="@+id/spinner1"
android:layout_width="142dp"
android:layout_height="wrap_content"
android:entries="@array/spinner_data"
android:spinnerMode="dropdown"/>
直接下拉列表 android:spinnerMode="dropdown"
弹出式下拉列表 android:spinnerMode="dialog"
在main.xml中的Spinner写入:
android:entries="@array/spinner_data"
在strings.xml中写入:
<string-array name="spinner_data">
<item >陕西</item>
<item >河北</item>
<item >四川</item>
</string-array>
Adapter:(适配器模式)
在计算机编程中,适配器模式(有时候也称包装样式或者包装)将一个类的接口适配成用户所期待的。一个适配允许通常因为接口不兼容而不能在一起工作的类工作在一起,做法是将类自己的接口包裹在一个已存在的类中。
Adapter在Android应用程序中起着非常重要的作用,应用也非常广泛,它可看作是数据源和UI组件之间的桥梁,其中Adapter、数据和UI之间的关系,BaseAdapter就Android应用程序中经常用到的基础数据适配器,它的主要用途是将一组数据传到像ListView、Spinner、Gallery及GridView等UI显示组件,它是继承自接口类Adapter,
1、添加数据源1的数据:
代码:
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- android:spinnerMode="dialog" 弹出式下拉列表--> <Spinner android:id="@+id/spinner1" android:layout_width="142dp" android:layout_height="wrap_content" android:entries="@array/spinner_data" android:spinnerMode="dropdown"/> </LinearLayout>
Test_spinnerActivity.java:
package test.spinner; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Spinner; import android.widget.TextView; public class Test_spinnerActivity extends Activity { //数据源1 private String[] ss = new String[]{ "北京", "上海", "深圳" }; private List<String> list = new ArrayList<String>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //数据源2 list.add("java"); list.add("c++"); list.add("php"); Spinner spinner=(Spinner)findViewById(R.id.spinner1); BaseAdapter adapter =new MyAdapter(); spinner.setAdapter(adapter); //spinner-->adapter -->数据 } private class MyAdapter extends BaseAdapter{ public int getCount() { return ss.length; } public Object getItem(int arg0) { return null; } public long getItemId(int arg0) { return 0; } //通过count来调用getView,count是几就调用几次getView public View getView(int position, View converView, ViewGroup parent) { TextView textView = new TextView(Test_spinnerActivity.this); textView.setText(ss[position]); return textView; } } }
运行结果:
2、添加数据源2的数据:
代码:
main.xml:
和上面去数据源1的代码一样
Test_spinnerActivity.java:
(黄色的底色的是修改的代码)
package test.spinner; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Spinner; import android.widget.TextView; public class Test_spinnerActivity extends Activity { //数据源1 private String[] ss = new String[]{ "北京", "上海", "深圳" }; private List<String> list = new ArrayList<String>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //数据源2 list.add("java"); list.add("c++"); list.add("php"); Spinner spinner=(Spinner)findViewById(R.id.spinner1); BaseAdapter adapter =new MyAdapter(); spinner.setAdapter(adapter); //spinner-->adapter -->数据 } private class MyAdapter extends BaseAdapter{ public int getCount() { return list.size(); } public Object getItem(int arg0) { return null; } public long getItemId(int arg0) { return 0; } //通过count来调用getView,count是几就调用几次getView public View getView(int position, View converView, ViewGroup parent) { TextView textView = new TextView(Test_spinnerActivity.this); textView.setText(list.get(position)); return textView; } } }
运行结果:
<String> adapter=
new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ss);
spinner.setAdapter(adapter);
ss指一个字符串数组
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" android:spinnerMode="dialog"/> </LinearLayout>
Test_spinnerActivity.java:
package test.spinner02; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.Spinner; public class Test_spinner02Activity extends Activity { private String[] ss = new String[]{ "北京", "上海", "深圳" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner=(Spinner)findViewById(R.id.spinner1); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ss); spinner.setAdapter(adapter); } }
运行结果:
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- android:spinnerMode="dialog" 弹出式下拉列表--> <Spinner android:id="@+id/spinner1" android:layout_width="142dp" android:layout_height="wrap_content" android:entries="@array/spinner_data" android:spinnerMode="dropdown"/> </LinearLayout>
Test_spinnerActivity.java:
package test.spinner; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; public class Test_spinnerActivity extends Activity { //数据源1 private String[] ss = new String[]{ "北京", "上海", "深圳" }; private List<String> list = new ArrayList<String>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //数据源2 list.add("java"); list.add("c++"); list.add("php"); Spinner spinner=(Spinner)findViewById(R.id.spinner1); BaseAdapter adapter =new MyAdapter(); spinner.setAdapter(adapter); spinner.setOnItemClickListener(new OnItemClickListener(){ public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { //position 被选中的item,相对应的数组索引 Toast.makeText(Test_spinnerActivity.this, list.get(position), 0).show(); } public void onNothingSelected(AdapterView<?> arg0){ } }); //spinner-->adapter -->数据 } private class MyAdapter extends BaseAdapter{ public int getCount() { return list.size(); } public Object getItem(int arg0) { return null; } public long getItemId(int arg0) { return 0; } //通过count来调用getView,count是几就调用几次getView public View getView(int position, View converView, ViewGroup parent) { TextView textView = new TextView(Test_spinnerActivity.this); textView.setText(list.get(position)); return textView; } } }
运行结果:
标签:sch group 动态 enc opd 样式 style 常用 河北
原文地址:http://www.cnblogs.com/cxm-weiniss/p/7220222.html