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;
}
}
}
Android 下拉控件:Spinner
最新推荐文章于 2016-05-07 12:01:26 发布