Android UI, spinner 下拉选框
来源:互联网 发布:郑大数据库程序设计 编辑:程序博客网 时间:2024/04/30 21:16
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;@Overrideprotected 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() {@Overridepublic void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {Toast.makeText(MyActivity2.this, ((TextView)arg1).getText(), Toast.LENGTH_SHORT).show();}@Overridepublic 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;}@Overridepublic int getCount() {return userList.size();}@Overridepublic Object getItem(int arg0) {return userList.get(arg0);}@Overridepublic long getItemId(int arg0) {return 0;}@Overridepublic 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;}}
0 0
- Android UI, spinner 下拉选框
- Android UI自定义Spinner下拉框(用popuwindow实现)
- Android UI自定义Spinner下拉框(用popuwindow实现)
- android 下拉框spinner
- android:Spinner下拉框
- Android 下拉框Spinner
- android spinner下拉框
- Android UI控件之Spinner(下拉列表)
- Android UI控件详解-Spinner(下拉列表)
- Android UI控件-Spinner(下拉列表)
- Android 自定义spinner下拉选框
- Android--下拉列表框(Spinner)
- android 下拉选择框Spinner
- android:自定义spinner下拉框
- Android—Spinner(下拉框)
- android Spinner自定义下拉框
- android 下拉选择框Spinner
- jQuery UI基础----14jQuery UI Widgets-spinner(下拉框
- 在OLTP中,索引访问的几种常规操作
- lower_bound()&&upper_bound()得用法
- Java基础Java中static,this与final
- NVME Driver 解析之Create I/O Completion Queue command
- http://blog.csdn.net/wddpxqq/article/details/7341809
- Android UI, spinner 下拉选框
- sql数据库连接代码
- 使用RSA访问目标机
- HDU ACM 1081 最大子矩阵问题
- Android深入浅出系列之Socket—Socket编程(一)
- Codeforces Round #275 (Div. 1)D(树形DP)
- 天气寒冷,许多人想以食补来御寒
- http与https的区别
- 常用RGB颜色表