Andorid中的Spinner编写实例
来源:互联网 发布:紫川软件靠谱吗? 编辑:程序博客网 时间:2024/05/20 03:37
Spinner在android开发中也是用的比较多的一个控件,提供了类似于Web中一个下拉列表样式的输入控件。下面先给出一个比较简单的实例,这里我们没有自己去定义下来列表的布局,而是采用android系统提供的布局。效果图如下:
android给Spinner控件提供了这样一个属性:android:entries="@array/books" 它可以指定Spinner的下拉选项,我们可以看出来它的资源是一个array数组。
layout的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/my_spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:entries="@array/books" /></LinearLayout>
数组资源的xml代码:
<?xml version="1.0" encoding="utf-8"?><resources> <string-array name="books"> <item >Windows</item> <item >mac os</item> <item >ubuntu</item> <item >aix</item> <item >Linux</item> </string-array> </resources>
下面activity代码,
public class SpinnerActivity extends Activity {private Spinner mySpinner;private myOnItemSelectedListener listener;// 注意String[]类型的数组资源不能在UI上定义String[],而是定义一个CharSequence[]private CharSequence[] os;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mySpinner = (Spinner) findViewById(R.id.my_spinner);listener = new myOnItemSelectedListener();// 定义一个CharSequence[] 去拿到String类型的数组os = getResources().getTextArray(R.array.books);mySpinner.setOnItemSelectedListener(listener);}private final class myOnItemSelectedListener implements OnItemSelectedListener {@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {Toast.makeText(getApplicationContext(), os[position], 1).show();}@Overridepublic void onNothingSelected(AdapterView<?> parent) {}}}
这个spinner很简单的实现,在写代码的时候发现定义的数组资源在activity中要用CharSequence[]类型,没有自己去定义下拉列表的,后面将会写出一个自定义item的实例。
下面我又重新自己写了一个自定义下拉列表的Spinner,因为实际开发中一般都是要自己定义下拉item才能满足需求的,上图先看效果:
因为程序代码很简单,所以下面只给出下拉列表item的布局和activity代码,首先是下拉item的xml:
<?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="horizontal" > <ImageView android:id="@+id/spinner_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/check" android:contentDescription="@string/what_description" /> <TextView android:id="@+id/tv_os" android:layout_width="wrap_content" android:layout_height="wrap_content"/></LinearLayout>
activity代码,这里我是采用一个继承自BaseAdapter的内部来自定义下拉item的布局的:
public class MySpinnerActivity extends Activity {private Spinner mySpinner;private myBaseAdapter adapter;private String[] os = new String[] { "windows", "mac os", "ubuntu", "linux", "android", "ios" };/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mySpinner = (Spinner) findViewById(R.id.myspinner);adapter = new myBaseAdapter();//mySpinner.setBackgroundColor(android.R.color.background_light);mySpinner.setAdapter(adapter);}private final class myBaseAdapter extends BaseAdapter {private LayoutInflater mInflater;public myBaseAdapter() {super();mInflater = getLayoutInflater();}@Overridepublic int getCount() {return os.length;}@Overridepublic Object getItem(int position) {return null;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View view = mInflater.inflate(R.layout.spinner_item, null);ImageView iv_spinner = (ImageView) view.findViewById(R.id.spinner_image);iv_spinner.setImageResource(R.drawable.check);TextView tv_os = (TextView) view.findViewById(R.id.tv_os);tv_os.setText(os[position]);return view;}}}
如上N多废话,只是自己学习的总结。
- Andorid中的Spinner编写实例
- andorid spinner
- andorid spinner 使用
- Andorid开发之使用Spinner控件
- Android Spinner实例
- Spinner使用实例2
- spinner 下拉框实例
- android spinner 简单实例
- Spinner 电影票实例
- 测试给andorid编写驱动
- Andorid中的权限大全
- Andorid中的控件属性
- Andorid Studio 中的.gradle
- Andorid 实例源码:音频录制
- java中的spinner
- Android中的Spinner控件)
- android中的spinner
- Spinner中的文字居中
- TelephonyManager使用
- Android利用ContentResolver查询的三种方式
- iPhone应用程序编程指南(文件和网络)
- 继承BaseAdapter实现自定义Adapter
- Android的Widget编写实例
- Andorid中的Spinner编写实例
- windows如何注册DLL文件
- Android提交数据到服务器的两种方式四种方法
- Midnight Commander
- Mongo 学习笔记--Capped Collection
- ntpdate time.windows.com
- mac os 键值缩写
- SpringMVC+FreeMarker配置详解(验证可用)
- 程序员度量:改善软件团队的分析学