一、Android的API方式默认实现的方式
1.layout下编辑main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Spinner
android:id="@+id/spinner01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/citys" /> <Spinner
android:id="@+id/spinner02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/spinner01" /> <Spinner
android:id="@+id/spinner03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/spinner02" /> </RelativeLayout>
2.string.xml代码
<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">Android_015</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string> <string-array name="citys">
<item>北京</item>
<item>上海</item>
<item>广州</item>
<item>深圳</item>
<item>珠海</item>
<item>湖州</item>
</string-array> <string name="tv">TextView</string>
</resources>
3.layout下面的自定义的下拉选项样式 Spinner_layout.xml和me_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/spinnerDropDownItemStyle"
android:ellipsize="marquee"
android:drawableLeft="@drawable/ic_launcher"
android:textColor="#00ff00"
/>
me_layout.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" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_launcher"
android:paddingRight="8dip"
android:paddingTop="8dip"
android:text="@string/tv"
android:textSize="25sp" /> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dip"
android:paddingTop="8dip"
android:text="@string/tv"
android:textSize="25sp" /> </LinearLayout>
4.Java后台代码
package com.example.android_015; import java.util.ArrayList;
import java.util.List; import com.example.android_015.entity.Person; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast; public class MainActivity extends Activity implements OnItemSelectedListener{ private Spinner spinner,spinner03; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); spinner = (Spinner) findViewById(R.id.spinner02);
spinner.setOnItemSelectedListener(this); // 获取string.xml的资源文件的数组
String[] citys = getResources().getStringArray(R.array.citys); // 数组适配器的创建
// 1.实例化的方式创建适配器
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, citys);
// 2.API建议的方式创建adapt
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
this, R.array.citys, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_layout);
//设置下拉试图的样式
//adapter1.setDropDownViewResource(android.R.layout.simple_list_item_checked); spinner.setAdapter(adapter); //自定义的适配器下垃选项
spinner03 = (Spinner) findViewById(R.id.spinner03);
// 建立数据源
List<Person> persons=new ArrayList<Person>();
persons.add(new Person("张三", "上海 "));
persons.add(new Person("李四", "上海 "));
persons.add(new Person("王五", "北京" ));
persons.add(new Person("赵六", "广州 "));
// 建立Adapter绑定数据源
MyAdapter _MyAdapter=new MyAdapter(this, persons);
//绑定Adapter
spinner03.setAdapter(_MyAdapter); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} //选中按钮的实现方法
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
System.out.println("parent:"+parent.getClass()); //父组件 spinner
System.out.println("view:"+view.getClass()); //加载的下拉列表 xml类型 TextView
System.out.println("position="+position);
System.out.println("id="+id);
//spinner.getSelectedItem().toString() 被选中的下拉列表值
//spinner.getSelectedView() 被选中的 下垃组件属性
//spinner.getSelectedItemPosition() 被选中的位置
//spinner.getSelectedItemId() 被选中的id
String str=parent.getItemAtPosition(position).toString(); Toast.makeText(this, "你点击选中的是:"+str, 2000).show();
} //未选中的实现方法
@Override
public void onNothingSelected(AdapterView<?> parent) { } }
5.Person类
package com.example.android_015.entity; /**
* 用户对象
* @author hbj403
*/
public class Person {
private String personName;
private String personAddress; public Person(String personName,String personAddress) {
super();
this.personName=personName;
this.personAddress=personAddress;
} public String getPersonName() {
return personName;
} public void setPersonName(String personName) {
this.personName = personName;
} public String getPersonAddress() {
return personAddress;
} public void setPersonAddress(String personAddress) {
this.personAddress = personAddress;
} }
6、自定义MyAdapter适配器类
package com.example.android_015; import java.util.List; import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView; import com.example.android_015.entity.Person; /**
* 自定义的适配器类
* @author hbj403
*/
public class MyAdapter extends BaseAdapter { private List<Person> mList;
private Context mContext; public MyAdapter(Context pContext,List<Person> pList) {
this.mContext=pContext;
this.mList=pList;
} @Override
public int getCount() {
return mList.size();
} @Override
public Object getItem(int position) {
return mList.get(position);
} @Override
public long getItemId(int position) {
return position;
} //最主要代码
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater _LayoutInflater=LayoutInflater.from(mContext);
convertView=_LayoutInflater.inflate(R.layout.me_layout, null); if(convertView!=null){
TextView _TextView1=(TextView)convertView.findViewById(R.id.textView1);
TextView _TextView2=(TextView)convertView.findViewById(R.id.textView2);
_TextView1.setText(mList.get(position).getPersonName());
_TextView2.setText(mList.get(position).getPersonAddress());
}
return convertView;
}
}
二、运行方式截图
监听事件和第一种方法相同:
部分链接文档来自:转自:http://blog.csdn.net/jiangqq781931404/article/details/7285623