1. Spinner 下拉选框
使用系统默认的spinner 可以通过系统提供的 adapter 将 数据和UI联系起来, 但数据一般为一个List<String>, UI 也是固定的。
如果需要自定义内容怎 需要自己 重写adapter , 并且定义自己的UI
MyActivity2.java , 两个spinner , 一个使用默认UI, 一个为自定义UI和自定义Adapter
package com.example.uitest;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MyActivity2 extends Activity {
private final String TAG = "myLog";
private Spinner spinner;
private Spinner spinner2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_2);
showSpinner1();
showSpinner2();
}
private void showSpinner2() {
spinner2 = (Spinner) findViewById(R.id.spinner2);
ArrayList<User> userList = new ArrayList<User>();
userList.add(new User("kevin","zhongshan"));
userList.add(new User("xiang","guangzhou"));
userList.add(new User("jie","changsha"));
UserAdapter userAdapter = new UserAdapter(MyActivity2.this, userList);
spinner2.setAdapter(userAdapter);
}
//使用一般的适配器
private void showSpinner1() {
spinner = (Spinner) findViewById(R.id.spinner1);
//建立数据源
String[] strList = getResources().getStringArray(R.array.strList);
//建立适配器,连接数据源
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, strList);
//绑定适配器到UI组件
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(MyActivity2.this, ((TextView)arg1).getText(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
res/layout/layout2.xml 布局文件, 放入两个 spinner
<?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="vertical" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="@string/spinnerName" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
res/value/string.xml; 可以配置一个 array 给spinner使用
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">UITest</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="spinnerName">My Spinner</string>
<string-array name="strList">
<item>Red</item>
<item>Green</item>
<item>Blue</item>
</string-array>
</resources>
UserAdapter.java 自定义的适配器
package com.example.uitest;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class UserAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<User> userList;
public UserAdapter(Context context, ArrayList<User> list){
mContext = context;
userList = list;
}
@Override
public int getCount() {
return userList.size();
}
@Override
public Object getItem(int arg0) {
return userList.get(arg0);
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
arg1 = LayoutInflater.from(mContext).inflate(R.layout.spinner_view, null);
TextView tv1 = (TextView) arg1.findViewById(R.id.textView1);
TextView tv2 = (TextView) arg1.findViewById(R.id.textView2);
tv1.setText(userList.get(arg0).getUsename());
tv2.setText(userList.get(arg0).getAddress());
return arg1;
}
}
res/layout/spinner_view.xml 自定义的spinner 控件
<?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:text="TextView"
android:drawableLeft="@drawable/ic_launcher"
android:padding="3dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:padding="3dp" />
</LinearLayout>
User对象定义
package com.example.uitest;
public class User {
private String usename;
private String address;
public User(String usename, String address) {
super();
this.usename = usename;
this.address = address;
}
public String getUsename() {
return usename;
}
public void setUsename(String usename) {
this.usename = usename;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}