关闭

Android 下拉控件:Spinner

200人阅读 评论(0) 收藏 举报
分类:
package com.example.spinner;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
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_start;
	private Spinner spinner_target;
	private String[] start;
	private String[] target;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		init();

		spinner_start.setAdapter(new MyAdapter(this, -1, start));
		spinner_target.setAdapter(new MyAdapter(this, -1, target));

		spinner_start.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {

			}
		});

		spinner_target.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

				/*
				 *  监听事件 
				 */
				switch (position) {
				case 0:
					
					break;

				case 1:
					
					
					break;

				default:
					break;
				}

			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {

			}
		});

	}

	private void init() {

		spinner_start = (Spinner) findViewById(R.id.spinner_start);
		spinner_target = (Spinner) findViewById(R.id.spinner_target);

		start = this.getResources().getStringArray(R.array.start);
		target = this.getResources().getStringArray(R.array.target);
	}

	private class MyAdapter extends ArrayAdapter {

		private Context mContext;
		private LayoutInflater inflater;
		private Object[] object;
		private boolean temp = true;

		public MyAdapter(Context context, int textViewResourceId, Object[] objects) {
			super(context, textViewResourceId, objects);

			this.mContext = context;
			this.object = (String[]) objects;

			inflater = LayoutInflater.from(context);

		}

		@Override
		public int getCount() {

			return object.length;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			if (convertView == null)

				convertView = inflater.inflate(R.layout.list_item, null);

			TextView text = (TextView) convertView.findViewById(R.id.text);

			text.setTextSize(18);
			if (temp) {
				text.setText(null);
				temp = false;
			} else {
				text.setText(object[position].toString());
			}
			return convertView;

		}
		/*
		 * 
		 * getDropDownView 下拉框显示的数据
		 */
		@Override
		public View getDropDownView(int position, View convertView, ViewGroup parent) {

			if (convertView == null)

				convertView = inflater.inflate(R.layout.list_item, null);

			TextView text = (TextView) convertView.findViewById(R.id.text);

			text.setTextSize(18);
			text.setText(object[position].toString());

			return convertView;
		}
	}

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:46868次
    • 积分:1465
    • 等级:
    • 排名:千里之外
    • 原创:103篇
    • 转载:6篇
    • 译文:0篇
    • 评论:6条
    文章分类
    最新评论