重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
一、概述
成都创新互联专注于龙川企业网站建设,自适应网站建设,商城网站定制开发。龙川网站建设公司,为龙川等地区提供建站服务。全流程按需定制设计,专业设计,全程项目跟踪,成都创新互联专业和态度为您提供的服务
Android中的有个原生的下拉列表控件Spinner,但是这个控件有时候不符合我们自己的要求,
比如有时候我们需要类似windows 或者web网页中常见的那种下拉列表控件,类似下图这样的:
这个时候只有自己动手写一个了。其实实现起来不算很难,
本文实现的方案是采用TextView +ImageView+PopupWindow的组合方案。
先来看看我们的自己写的控件效果图吧:(源码在文章下面最后给出哈!)
二、自定义下拉列表框控件的实现
1. 自定义控件用到的布局文件和资源:
结果框的布局页面:dropdownlist_view.xml:
?xml version="1.0" encoding="utf-8"?
RelativeLayout xmlns:android=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/compound"
android:background="@drawable/dropdown_bg_selector"
TextView
android:id="@+id/text"
android:layout_width="250dp"
android:layout_height="40dp"
android:paddingLeft="10dp"
android:text="文本文字"
android:gravity="center_vertical"
android:textSize="14sp"
android:padding="5dp"
android:singleLine="true" /
ImageView
android:id="@+id/btn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_toRightOf="@+id/text"
android:src="@drawable/dropdown"
android:padding="5dp"
android:layout_centerVertical="true"
android:gravity="center"/
/RelativeLayout
下拉弹窗列表布局页面:dropdownlist_popupwindow.xml:
?xml version="1.0" encoding="utf-8"?
LinearLayout xmlns:android=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
ListView
android:id="@+id/listView"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:divider="#666666"
android:dividerHeight="1dp"
/ListView
/LinearLayout
selector资源文件:
dropdown_list_selector.xml:
?xml version="1.0" encoding="utf-8"?
selector xmlns:android=""
item android:state_pressed="true" android:drawable="@color/dropdownlist_item_press"/
item android:drawable="@color/dropdownlist_item"/
/selector
dropdown_bg_selector.xml:
?xml version="1.0" encoding="utf-8"?
selector xmlns:android=""
item android:state_pressed="true" android:drawable="@color/dropdownlist_press"/
item android:drawable="@color/dropdownlist_bg"/
/selector
2. 自定义下拉列表框控件类的实现:
我们采用了TextView+ImageView+PopupWindow的组合方案,所以我的自定义控件需要重写ViewGroup,由于我们已经知道了,布局方向为竖直方向,所以这里,
我直接继承LinearLayout来写这个控件。具体实现代码如下:
package com.czm.xcdropdownlistview;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
@SuppressLint("NewApi")
/**
* 下拉列表框控件
* @author caizhiming
*
*/
public class XCDropDownListView extends LinearLayout{
private TextView editText;
private ImageView imageView;
private PopupWindow popupWindow = null;
private ArrayListString dataList = new ArrayListString();
private View mView;
public XCDropDownListView(Context context) {
this(context,null);
// TODO Auto-generated constructor stub
}
public XCDropDownListView(Context context, AttributeSet attrs) {
this(context, attrs,0);
// TODO Auto-generated constructor stub
}
public XCDropDownListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
initView();
}
public void initView(){
String infServie = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater layoutInflater;
layoutInflater = (LayoutInflater) getContext().getSystemService(infServie);
View view = layoutInflater.inflate(R.layout.dropdownlist_view, this,true);
editText= (TextView)findViewById(R.id.text);
imageView = (ImageView)findViewById(R.id.btn);
this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(popupWindow == null ){
showPopWindow();
}else{
closePopWindow();
}
}
});
}
/**
* 打开下拉列表弹窗
*/
private void showPopWindow() {
// 加载popupWindow的布局文件
String infServie = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater layoutInflater;
layoutInflater = (LayoutInflater) getContext().getSystemService(infServie);
View contentView = layoutInflater.inflate(R.layout.dropdownlist_popupwindow, null,false);
ListView listView = (ListView)contentView.findViewById(R.id.listView);
listView.setAdapter(new XCDropDownListAdapter(getContext(), dataList));
popupWindow = new PopupWindow(contentView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.color.transparent));
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(this);
}
/**
* 关闭下拉列表弹窗
*/
private void closePopWindow(){
popupWindow.dismiss();
popupWindow = null;
}
/**
* 设置数据
* @param list
*/
public void setItemsData(ArrayListString list){
dataList = list;
editText.setText(list.get(0).toString());
}
/**
* 数据适配器
* @author caizhiming
*
*/
class XCDropDownListAdapter extends BaseAdapter{
Context mContext;
ArrayListString mData;
LayoutInflater inflater;
public XCDropDownListAdapter(Context ctx,ArrayListString data){
mContext = ctx;
mData = data;
inflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// 自定义视图
ListItemView listItemView = null;
if (convertView == null) {
// 获取list_item布局文件的视图
convertView = inflater.inflate(R.layout.dropdown_list_item, null);
listItemView = new ListItemView();
// 获取控件对象
listItemView.tv = (TextView) convertView
.findViewById(R.id.tv);
listItemView.layout = (LinearLayout) convertView.findViewById(R.id.layout_container);
// 设置控件集到convertView
convertView.setTag(listItemView);
} else {
listItemView = (ListItemView) convertView.getTag();
}
// 设置数据
listItemView.tv.setText(mData.get(position).toString());
final String text = mData.get(position).toString();
listItemView.layout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
editText.setText(text);
closePopWindow();
}
});
return convertView;
}
}
private static class ListItemView{
TextView tv;
LinearLayout layout;
}
}
三、如何使用该自定义下拉列表框控件
使用该控件和使用普通的自带的控件一样,首先需要在布局文件中引用该控件:
RelativeLayout xmlns:android=""
xmlns:tools=""
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.czm.xcdropdownlistview.MainActivity"
tools:ignore="MergeRootFrame"
com.czm.xcdropdownlistview.XCDropDownListView
android:id="@+id/drop_down_list_view"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" /
/RelativeLayout
其次,就是在代码中使用该控件:
package com.czm.xcdropdownlistview;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
/**
* 使用下拉列表框控件 示例
* @author caizhiming
*
*/
public class MainActivity extends Activity {
XCDropDownListView dropDownListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dropDownListView = (XCDropDownListView)findViewById(R.id.drop_down_list_view);
ArrayListString list = new ArrayListString();
for(int i = 0;i 6;i++){
list.add("下拉列表项"+(i+1));
}
dropDownListView.setItemsData(list);
}
}
整理的下拉列表框示例代码中,主要分为以下部分:静态绑定下拉框数据、动态绑定下拉框数据、选择时触发的事件以及获得所选的值;
1、静态绑定下拉框数据
需要将数据写在xml中,然后设置下拉框的entries属性,则数据自动加载到下拉框中。具体如下:
在value文件夹中新建cityInfo.xml,xml中写入
[html] view plaincopy
?xml version="1.0" encoding="utf-8"?
resources
string-array name="cityArray"
item北京/item
item江苏/item
item浙江/item
item上海/item
/string-array
/resources
设计页面控件代码:
[html] view plaincopy
Spinner android:id="@+id/spinnerCityStatic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/cityArray"/
2、动态绑定下拉框数据
主要三个步骤,1.获得数据列表;2.填充数据适配器;3.设置下拉框的适配器;
[java] view plaincopy
private Spinner spCity = null;
private ArrayAdapterCharSequence adapterCity = null;
private static String[] cityInfo={"北京","江苏","浙江","上海"};
//初始化函数中代码如下
this.spCity = (Spinner) super.findViewById(R.id.spinnerCity);
//将数据cityInfo填充到适配器adapterCity中
this.adapterCity = new ArrayAdapterCharSequence(this,
android.R.layout.simple_spinner_dropdown_item, cityInfo);
//设置下拉框的数据适配器adapterCity
this.spCity.setAdapter(adapterCity);
3、选择时触发的事件
需要实现OnItemSelectedListener接口的onItemSelected方法,代码如下
[java] view plaincopy
//下拉框选择事件
private class OnItemSelectedListenerImpl implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView? parent, View view,
int position, long id) {
String city = parent.getItemAtPosition(position).toString();
Toast.makeText(MainActivity.this, "选择的城市是:" + city,
Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView? parent) {
// TODO Auto-generated method stub
}
}
4、获得下拉框所选的值
[java] view plaincopy
String cityStatic = spCityStatic.getSelectedItem().toString();
项目完整代码:
前台设计页面activity_main.xml如下:
[html] view plaincopy
?xml version="1.0" encoding="utf-8"?
LinearLayout xmlns:android=""
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
Spinner
android:id="@+id/spinnerCityStatic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/cityArray"/
Spinner
android:id="@+id/spinnerCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/
Button
android:id="@+id/btnShowCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看选择的结果"/
/LinearLayout
后台页面MainActivity.java代码:
[java] view plaincopy
package com.example.spinner0803;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btnShowCity = null;
private Spinner spCityStatic = null;
private Spinner spCity = null;
private ArrayAdapterCharSequence adapterCity = null;
private static String[] cityInfo={"北京","江苏","浙江","上海"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//按钮相关
btnShowCity = (Button) super.findViewById(R.id.btnShowCity);
btnShowCity.setOnClickListener(new OnClickListenerImpl());
//静态实现的下拉框,数据写在cityInfo.xml文件中
this.spCityStatic = (Spinner) super
.findViewById(R.id.spinnerCityStatic);
this.spCityStatic
.setOnItemSelectedListener(new OnItemSelectedListenerImpl());
//动态实现的下拉框,数据在程序中获得,实际项目可能来自数据库等
this.spCity = (Spinner) super.findViewById(R.id.spinnerCity);
this.adapterCity = new ArrayAdapterCharSequence(this,
android.R.layout.simple_spinner_dropdown_item, cityInfo);
this.spCity.setAdapter(adapterCity);
this.spCity.setOnItemSelectedListener(new OnItemSelectedListenerImpl());
}
//按钮点击事件
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View v) {
String cityStatic = spCityStatic.getSelectedItem().toString();
String city = spCity.getSelectedItem().toString();
String selectInfo = "第一个选择的城市是:" + city + ",第二个选择的城市是:"
+ cityStatic;
Toast.makeText(MainActivity.this, selectInfo, Toast.LENGTH_LONG)
1、首先在一个布局文件(.XML)中绘画了一个跳转按钮(id为btn1):
Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击跳转" /
2、然后在关联的类中声明一个私有button名称,如:
private Button btn1;
TIPS:在类上会添加:import android.widget.Button;
3、接着在类中onCreate的方法内执行以下操作:
(1)、给btn1赋值,即设置布局文件中的Button按钮id进行关联,如:
btn1 = (Button) findViewById(R.id.btn1);
(2)、给btn1绑定点击事件:
btn1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
}
});
TIPS:在类上会添加:import android.view.View;
(3)、 给bnt1添加点击响应事件:
btn1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件。
//page1为先前已添加的类,并已在AndroidManifest.xml内添加活动事件(activity android:name="page1"/activity),在存放资源代码的文件夹下下,
Intent i = new Intent(MainActivity.this , page1.class);
////启动
startActivity(i);
}
});
TIPS:在类上会添加:import android.content.Intent;
4、最后,就可以就可以跳转到下一个页面了。
android 是可以自定义下拉框的
定义适配器 adapter的setDropDownViewResource 时候 在xml 文件里自定义一个下拉菜单的样式啊