标签:
———————————spinner
第一步: 添加一个下拉列表项,添加
private List<String > list = enew aRRAYlIST<String>();
list.add("北京");《数据源》
list.add("北京");
list.add("北京");
list.add("北京");
list.add("北京");
第二步:为下拉列表定义一个数据适配器,(ArrayAdapter),
adapter = new ArrayAdapter<String>(this,adnroid,R.layout.simple_spinner_item,list);
《定时适配器数据源》
第三步:为适配器设置下拉列表时的菜单样式
adapter.setDropDwonViewResource(android.R.layout.simple_spinner_dropdown_item);
第四步:将适配器添加到下拉列表上;
myDpinner.setAdapter(adapter);`这里写代码片`
第五步: 为下拉列表设置各种时间的响应,这个事件响应菜单被选中
mySpinner.setOnItenSelectedListener(new Spinner.OnItemSelectedListener(){....});
public class MainActivity extends Activity {
private TextView myTextView;
private Spinner mySpinner;
private SimpleAdapter adapter;
private List<Map<String, Object>> dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 第一步:添加一个下拉列表项的list,这里添加的项就是下拉列表的菜单项
dataList = new ArrayList<Map<String, Object>>();
getData();
myTextView = (TextView) findViewById(R.id.textView);
mySpinner = (Spinner) findViewById(R.id.spinner);
// 第二步:为下拉列表定义一个适配器,这里就用到里前面定义的list。
adapter=new SimpleAdapter(this, dataList, R.layout.item, new String[]{"image","text"}, new int[]{R.id.image,R.id.text});
// 第三步:为适配器设置下拉列表下拉时的菜单样式。
adapter.setDropDownViewResource(R.layout.item);
// 第四步:将适配器添加到下拉列表上
mySpinner.setAdapter(adapter);
// 第五步:为下拉列表设置各种事件的响应,这个事响应菜单被选中
mySpinner
.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
/* 将所选mySpinner 的值带入myTextView 中 */
myTextView.setText("您选择的是:" + adapter.getItem(arg2));
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
myTextView.setText("NONE");
}
});
}
private void getData() {
// TODO Auto-generated method stub
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", R.drawable.ic_launcher);
map.put("text", "北京");
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("image", R.drawable.ic_launcher);
map2.put("text", "上海");
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("image", R.drawable.ic_launcher);
map3.put("text", "广州");
Map<String, Object> map4 = new HashMap<String, Object>();
map4.put("image", R.drawable.ic_launcher);
map4.put("text", "深圳");
dataList.add(map);
dataList.add(map2);
dataList.add(map3);
dataList.add(map4);
}
}
<Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="wrap_content"></Spinner>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp" android:src="@drawable/ic_launcher"/>
<TextView android:id="@+id/text" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/image"
android:text="None"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity {
private EditText input;
private ImageView downArrow;
private List<String> msgList;
private PopupWindow popWin;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.input);
downArrow = (ImageView) findViewById(R.id.down_arrow);
msgList = new ArrayList<String>();
for (int i = 0; i < 20; i++) {
msgList.add("1000000000"+i);
}
initListView();
downArrow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//定义 popupWindow
popWin = new PopupWindow(MainActivity.this);
popWin.setWidth(input.getWidth()); //设置宽度
popWin.setHeight(200); //设置popWin 高度
popWin.setContentView(listView); //为popWindow填充内容
popWin.setOutsideTouchable(true); // 点击popWin 以处的区域,自动关闭 popWin
popWin.showAsDropDown(input, 0, 0); //设置 弹出窗口,显示的位置
}
});
}
private void initListView() {
listView = new ListView(this);
listView.setBackgroundResource(R.drawable.listview_background); //设置listView 背景
listView.setDivider(null); //设置条目之间的分隔线为null
listView.setVerticalScrollBarEnabled(false); // 关闭水平横线
listView.setAdapter(new MyListAdapter());
}
private class MyListAdapter extends BaseAdapter{
@Override
public int getCount() {
return msgList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = View.inflate(getApplicationContext(), R.layout.list_item, null);
holder = new ViewHolder();
holder.delete = (ImageView) convertView.findViewById(R.id.delete);
holder.tv_msg =(TextView) convertView.findViewById(R.id.tv_list_item);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.tv_msg.setText(msgList.get(position));
holder.delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//删除对应的条目
msgList.remove(position);
//刷新listView
MyListAdapter.this.notifyDataSetChanged();
}
});
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//设置输入框
input.setText(msgList.get(position));
popWin.dismiss();
}
});
return convertView;
}
}
private class ViewHolder{
TextView tv_msg;
ImageView delete;
}
}
activity_main.xml
<EditText
android:id="@+id/input"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="@string/hello_world" />
<ImageView
android:id="@+id/down_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/down_arrow"
android:layout_alignRight="@id/input"
android:clickable="true"
android:layout_marginTop="20dp"
/>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
tools:context=".MainActivity" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/user" />
<TextView
android:id="@+id/tv_list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="@string/hello_world" />
<ImageView
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/delete" />
</LinearLayout>
标签:
原文地址:http://blog.csdn.net/xs15073722800/article/details/51333459