一、概述
成都創(chuàng)新互聯(lián)專(zhuān)注于龍川企業(yè)網(wǎng)站建設(shè),自適應(yīng)網(wǎng)站建設(shè),商城網(wǎng)站定制開(kāi)發(fā)。龍川網(wǎng)站建設(shè)公司,為龍川等地區(qū)提供建站服務(wù)。全流程按需定制設(shè)計(jì),專(zhuān)業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專(zhuān)業(yè)和態(tài)度為您提供的服務(wù)
Android中的有個(gè)原生的下拉列表控件Spinner,但是這個(gè)控件有時(shí)候不符合我們自己的要求,
比如有時(shí)候我們需要類(lèi)似windows 或者web網(wǎng)頁(yè)中常見(jiàn)的那種下拉列表控件,類(lèi)似下圖這樣的:
這個(gè)時(shí)候只有自己動(dòng)手寫(xiě)一個(gè)了。其實(shí)實(shí)現(xiàn)起來(lái)不算很難,
本文實(shí)現(xiàn)的方案是采用TextView +ImageView+PopupWindow的組合方案。
先來(lái)看看我們的自己寫(xiě)的控件效果圖吧:(源碼在文章下面最后給出哈?。?/p>
二、自定義下拉列表框控件的實(shí)現(xiàn)
1. 自定義控件用到的布局文件和資源:
結(jié)果框的布局頁(yè)面: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
下拉彈窗列表布局頁(yè)面: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. 自定義下拉列表框控件類(lèi)的實(shí)現(xiàn):
我們采用了TextView+ImageView+PopupWindow的組合方案,所以我的自定義控件需要重寫(xiě)ViewGroup,由于我們已經(jīng)知道了,布局方向?yàn)樨Q直方向,所以這里,
我直接繼承LinearLayout來(lái)寫(xiě)這個(gè)控件。具體實(shí)現(xiàn)代碼如下:
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();
}
}
});
}
/**
* 打開(kāi)下拉列表彈窗
*/
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);
}
/**
* 關(guān)閉下拉列表彈窗
*/
private void closePopWindow(){
popupWindow.dismiss();
popupWindow = null;
}
/**
* 設(shè)置數(shù)據(jù)
* @param list
*/
public void setItemsData(ArrayListString list){
dataList = list;
editText.setText(list.get(0).toString());
}
/**
* 數(shù)據(jù)適配器
* @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();
// 獲取控件對(duì)象
listItemView.tv = (TextView) convertView
.findViewById(R.id.tv);
listItemView.layout = (LinearLayout) convertView.findViewById(R.id.layout_container);
// 設(shè)置控件集到convertView
convertView.setTag(listItemView);
} else {
listItemView = (ListItemView) convertView.getTag();
}
// 設(shè)置數(shù)據(jù)
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("下拉列表項(xiàng)"+(i+1));
}
dropDownListView.setItemsData(list);
}
}
整理的下拉列表框示例代碼中,主要分為以下部分:靜態(tài)綁定下拉框數(shù)據(jù)、動(dòng)態(tài)綁定下拉框數(shù)據(jù)、選擇時(shí)觸發(fā)的事件以及獲得所選的值;
1、靜態(tài)綁定下拉框數(shù)據(jù)
需要將數(shù)據(jù)寫(xiě)在xml中,然后設(shè)置下拉框的entries屬性,則數(shù)據(jù)自動(dòng)加載到下拉框中。具體如下:
在value文件夾中新建cityInfo.xml,xml中寫(xiě)入
[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
設(shè)計(jì)頁(yè)面控件代碼:
[html] view plaincopy
Spinner android:id="@+id/spinnerCityStatic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/cityArray"/
2、動(dòng)態(tài)綁定下拉框數(shù)據(jù)
主要三個(gè)步驟,1.獲得數(shù)據(jù)列表;2.填充數(shù)據(jù)適配器;3.設(shè)置下拉框的適配器;
[java] view plaincopy
private Spinner spCity = null;
private ArrayAdapterCharSequence adapterCity = null;
private static String[] cityInfo={"北京","江蘇","浙江","上海"};
//初始化函數(shù)中代碼如下
this.spCity = (Spinner) super.findViewById(R.id.spinnerCity);
//將數(shù)據(jù)cityInfo填充到適配器adapterCity中
this.adapterCity = new ArrayAdapterCharSequence(this,
android.R.layout.simple_spinner_dropdown_item, cityInfo);
//設(shè)置下拉框的數(shù)據(jù)適配器adapterCity
this.spCity.setAdapter(adapterCity);
3、選擇時(shí)觸發(fā)的事件
需要實(shí)現(xiàn)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();
項(xiàng)目完整代碼:
前臺(tái)設(shè)計(jì)頁(yè)面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="查看選擇的結(jié)果"/
/LinearLayout
后臺(tái)頁(yè)面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);
//按鈕相關(guān)
btnShowCity = (Button) super.findViewById(R.id.btnShowCity);
btnShowCity.setOnClickListener(new OnClickListenerImpl());
//靜態(tài)實(shí)現(xiàn)的下拉框,數(shù)據(jù)寫(xiě)在cityInfo.xml文件中
this.spCityStatic = (Spinner) super
.findViewById(R.id.spinnerCityStatic);
this.spCityStatic
.setOnItemSelectedListener(new OnItemSelectedListenerImpl());
//動(dòng)態(tài)實(shí)現(xiàn)的下拉框,數(shù)據(jù)在程序中獲得,實(shí)際項(xiàng)目可能來(lái)自數(shù)據(jù)庫(kù)等
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());
}
//按鈕點(diǎn)擊事件
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View v) {
String cityStatic = spCityStatic.getSelectedItem().toString();
String city = spCity.getSelectedItem().toString();
String selectInfo = "第一個(gè)選擇的城市是:" + city + ",第二個(gè)選擇的城市是:"
+ cityStatic;
Toast.makeText(MainActivity.this, selectInfo, Toast.LENGTH_LONG)
1、首先在一個(gè)布局文件(.XML)中繪畫(huà)了一個(gè)跳轉(zhuǎn)按鈕(id為btn1):
Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="點(diǎn)擊跳轉(zhuǎn)" /
2、然后在關(guān)聯(lián)的類(lèi)中聲明一個(gè)私有button名稱(chēng),如:
private Button btn1;
TIPS:在類(lèi)上會(huì)添加:import android.widget.Button;
3、接著在類(lèi)中onCreate的方法內(nèi)執(zhí)行以下操作:
(1)、給btn1賦值,即設(shè)置布局文件中的Button按鈕id進(jìn)行關(guān)聯(lián),如:
btn1 = (Button) findViewById(R.id.btn1);
(2)、給btn1綁定點(diǎn)擊事件:
btn1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
}
});
TIPS:在類(lèi)上會(huì)添加:import android.view.View;
(3)、 給bnt1添加點(diǎn)擊響應(yīng)事件:
btn1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//Intent是一種運(yùn)行時(shí)綁定(run-time binding)機(jī)制,它能在程序運(yùn)行過(guò)程中連接兩個(gè)不同的組件。
//page1為先前已添加的類(lèi),并已在AndroidManifest.xml內(nèi)添加活動(dòng)事件(activity android:name="page1"/activity),在存放資源代碼的文件夾下下,
Intent i = new Intent(MainActivity.this , page1.class);
////啟動(dòng)
startActivity(i);
}
});
TIPS:在類(lèi)上會(huì)添加:import android.content.Intent;
4、最后,就可以就可以跳轉(zhuǎn)到下一個(gè)頁(yè)面了。
android 是可以自定義下拉框的
定義適配器 adapter的setDropDownViewResource 時(shí)候 在xml 文件里自定義一個(gè)下拉菜單的樣式啊