- 论坛徽章:
- 0
|
Android控件之Spinner探究
以下模拟下拉列表的用法
布局文件- 01.<?xml version="1.0" encoding="utf-8"?>
- 02.<LinearLayout android:id="@+id/LinearLayout01"
- 03. android:layout_width="fill_parent" android:layout_height="fill_parent"
- 04. android:orientation="vertical"
- 05. xmlns:android="http://schemas.android.com/apk/res/android">
- 06. <TextView android:text="@string/ys"
- 07. android:id="@+id/TextView01"
- 08. android:layout_width="fill_parent"
- 09. android:layout_height="wrap_content"
- 10. android:textSize="28dip" />
- 11. <Spinner android:id="@+id/Spinner01"
- 12. android:layout_width="fill_parent"
- 13. android:layout_height="wrap_content" />
- 14.</LinearLayout>
复制代码 复制代码
SpinnerActivity类- 01.package com.ljq.sp;
- 02.
- 03.import android.app.Activity;
- 04.import android.os.Bundle;
- 05.import android.util.Log;
- 06.import android.view.View;
- 07.import android.view.ViewGroup;
- 08.import android.widget.AdapterView;
- 09.import android.widget.BaseAdapter;
- 10.import android.widget.ImageView;
- 11.import android.widget.LinearLayout;
- 12.import android.widget.Spinner;
- 13.import android.widget.TextView;
- 14.import android.widget.AdapterView.OnItemSelectedListener;
- 15.
- 16.public class SpinnerActivity extends Activity {
- 17. private Spinner sp = null;//下拉列表
- 18. private TextView tv = null;
- 19. // 所有资源图片的数组
- 20. private int[] drawableIds={R.drawable.football,R.drawable.basketball,R.drawable.volleyball};
- 21. // 所有字符串的数组
- 22. private int[] msgIds={R.string.zq,R.string.lq,R.string.pq};
- 23.
- 24. @Override
- 25. public void onCreate(Bundle savedInstanceState) {
- 26. super.onCreate(savedInstanceState);
- 27. setContentView(R.layout.main);
- 28.
- 29. tv = (TextView) findViewById(R.id.TextView01);
- 30. sp=(Spinner)this.findViewById(R.id.Spinner01);//初始化Spinner
- 31. sp.setAdapter(adapter);
- 32. sp.setOnItemSelectedListener(new OnItemSelectedListener() {
- 33. public void onItemSelected(AdapterView<?> parent, View view, int positon, long id) {
- 34. LinearLayout ll = (LinearLayout) view;
- 35. View v=ll.getChildAt(0);//获取第一个控件ImageView
- 36. Log.i("ljq", v.getClass().getName());
- 37. TextView tvn = (TextView) ll.getChildAt(1);//获取第二个控件TextView
- 38. StringBuilder sb = new StringBuilder();
- 39. sb.append(getResources().getText(R.string.ys)).append(":").append(tvn.getText());
- 40. tv.setText(sb.toString());
- 41. }
- 42.
- 43. public void onNothingSelected(AdapterView<?> parent) {
- 44.
- 45. }
- 46. });
- 47. }
- 48.
- 49. private BaseAdapter adapter = new BaseAdapter(){
- 50. public int getCount() {
- 51. return drawableIds.length;
- 52. }
- 53.
- 54. public Object getItem(int position) {
- 55. return drawableIds[position];
- 56. }
- 57.
- 58. public long getItemId(int position) {
- 59. return position;
- 60. }
- 61.
- 62. public View getView(int position, View convertView, ViewGroup parent) {
- 63. LinearLayout ll = new LinearLayout(SpinnerActivity.this);
- 64. ll.setOrientation(LinearLayout.HORIZONTAL);
- 65.
- 66. ImageView iv = new ImageView(SpinnerActivity.this);
- 67. iv.setImageResource(drawableIds[position]);
- 68. ll.addView(iv);
- 69.
- 70. TextView tv=new TextView(SpinnerActivity.this);
- 71. tv.setText(msgIds[position]);//设置内容
- 72. tv.setTextSize(24);
- 73. tv.setTextColor(R.color.black);
- 74. ll.addView(tv);
- 75. return ll;
- 76. }
- 77. };
- 78.}
复制代码 复制代码
运行结果
分类: android常用控件
标签: android Spinner, android 下拉列表 |
|