Andorid中的Spinner编写实例

 

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. */
	@Override
	public 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 {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
			Toast.makeText(getApplicationContext(), os[position], 1).show();
		}

		@Override
		public 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. */
	@Override
	public 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();
		}

		@Override
		public int getCount() {
			return os.length;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public 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多废话,只是自己学习的总结。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值