androidspinner下拉
㈠ android里能为下拉菜单设置监听事件么
Spinner组件的主要功能是用于进行下拉列表显示的功能,当用户选中下拉列表中的某个选项之后可以使用Spinner类中提供的setOnItemClickListener()方法进行监听。
下拉菜单监听范例:
packagecom.richard.onitemselectedlistener;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.view.Menu;
importandroid.view.View;
importandroid.widget.AdapterView;
importandroid.widget.AdapterView.OnItemSelectedListener;
importandroid.widget.ArrayAdapter;
importandroid.widget.Spinner;
{
privateSpinnercity=null;//定义下拉列表框
privateSpinnerarea=null;//定义下拉列表框
privateString[][]areaData=newString[][]{//定义联动菜单项
{"东城","西城","朝阳","大兴","平谷"},//第一级子选项
{"黄浦","杨浦","闵行"},//第二级子选项
{"广州"}};//第三级子选项
privateArrayAdapter<CharSequence>adapterArea=null;//下拉列表内容适配器
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);//父类onCreate()
super.setContentView(R.layout.activity_main);//调用布局管理器
this.city=(Spinner)super.findViewById(R.id.city);//取得组件
this.area=(Spinner)super.findViewById(R.id.area);//取得组件
this.city.setOnItemSelectedListener(
newOnItemSelectedListenerImpl());//设置监听器
}
{
@Override
publicvoidonItemSelected(AdapterView<?>adapterView,Viewview,
intposition,longid){//选项选中时触发
MainActivity.this.adapterArea=newArrayAdapter<CharSequence>(
MainActivity.this,
android.R.layout.simple_spinner_item,
MainActivity.this.areaData[position]);//实例化列表项
MainActivity.this.adapterArea.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);//设置下拉列表显示风格
MainActivity.this.area
.setAdapter(MainActivity.this.adapterArea);//设置数据
}
@Override
publicvoidonNothingSelected(AdapterView<?>adapterView){//没有选项时触发
}
}
@Override
(Menumenu){
//Inflatethemenu;.
getMenuInflater().inflate(R.menu.main,menu);
returntrue;
}
}
㈡ android涓涓嬫媺鍒楄〃椋庢牸鍑轰简闂棰
import java.util.ArrayList;
import java.util.List;
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 SpinnerActivity extends Activity {
//瀹氢箟涓嬫媺鍒楄〃
private Spinner spinner_info = null;
//瀹氢箟涓轰笅𨰾夊垪琛锛坰pinner锛夋坊锷犱竴𨱒′俊鎭镄凚utton鎸夐挳
private Button btn_spinner_add = null;
//瀹氢箟涓轰笅𨰾夊垪琛锛坰pinner锛夊垹闄や竴𨱒′俊鎭镄凚utton鎸夐挳
private Button btn_spinner_del = null;
//瀹氢箟涓涓瀛楃︿覆鏁扮粍
private String[] args = {"jinan","zhonggong","licheng"};
//鍐呭归傞厤鍣ㄧ殑鏁版嵁婧愶纴鐢变簬鐢ㄥ埌浜嗗姩镐佹坊锷犳垨鍒犻櫎涓嬫媺鍒楄〃鏁版嵁锛屽洜姝ゆ暟鎹婧愰噰鐢ㄤ简锷ㄦ乴ist鏁扮粍钥岄潪闱欐丼tring鏁扮粍
private List<String> list = null;
//瀹氢箟涓涓鍐呭归傞厤鍣
private ArrayAdapter<String> arrayAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
//鍒濆嫔寲鎺т欢淇℃伅
initView();
}
/**
* 鍒濆嫔寲鎺т欢淇℃伅
* */
private void initView(){
//鏁版嵁婧愭坊锷犳暟鎹
list = new ArrayList<String>();
for(String arg:args){
list.add(arg);
}
//spinner瀵硅薄
spinner_info = (Spinner)findViewById(R.id.spinner_info);
//瀹氢箟骞跺埯濮嫔寲鍐呭归傞厤鍣锛屽姞杞芥暟鎹鍙婃牱寮忥纴閲囩敤绯荤粺镊甯︾殑镙峰纺
arrayAdapter = new ArrayAdapter<String>(SpinnerActivity.this,android.R.layout.simple_spinner_item,list);
//璁剧疆涓嬫媺鍒楄〃镄勬樉绀烘牱寮忥纸椋庢牸锛夛纴閲囩敤绯荤粺镊甯︾殑镙峰纺锛堥庢牸锛
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//涓轰笅𨰾夊垪琛ㄦ坊锷犻傞厤鍣
spinner_info.setAdapter(arrayAdapter);
/* 涓嬫媺鍒楄〃阃夋嫨浜嬩欢 */
spinner_info.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(SpinnerActivity.this, "鎴戠偣阃変简"+args[arg2].toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
/* 涓嬫媺鍒楄〃锛坰pinner锛夋坊锷犱竴𨱒′俊鎭 */
btn_spinner_add = (Button)findViewById(R.id.btn_spinner_add);
btn_spinner_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {//
String add_info = "鎴戞槸鏂版坊锷犵殑鏁版嵁";
//钖戝唴瀹归傞厤鍣ㄦ坊锷犱竴𨱒′俊鎭
arrayAdapter.add(add_info);
//銮峰彇鏂版坊锷犵殑淇℃伅镄勪綅缃
int position = arrayAdapter.getPosition(add_info);
//璁剧疆鏂版坊锷犵殑淇℃伅涓洪変腑鐘舵
spinner_info.setSelection(position);
}
});
/* 涓嬫媺鍒楄〃锛坰pinner锛夊垹闄や竴𨱒′俊鎭 */
btn_spinner_del = (Button)findViewById(R.id.btn_spinner_delete);
btn_spinner_del.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//绉婚櫎镓阃変俊鎭
arrayAdapter.remove(spinner_info.getSelectedItem().toString());
}
});
}
}
㈢ android spinner怎么用
android中的spinner控件是表示下拉菜单的意思,按照如下步骤使用:
1、首先使用Android studio创建一个项目,如下图:
4、最后去类中绑定控件,并添加数据即可使用。
㈣ androidUI鎺т欢Spinner涓嬫媺凿滃崟鎺т欢涓ょ崭娇鐢ㄦ柟娉
浣跨敤android鍒朵綔琛ㄥ崟涓涓嬫媺凿滃崟镓浣跨敤镄勬带浠禨pinner鍦╝ndroid涓涓烘垜浠瀹氢箟浜嗗崄鍒嗗ソ鐢ㄥソ鐪嬬殑镙峰纺锛屽悓镙风亩鍗曚娇鐢ㄦ柟娉曚篃链変袱绉嶃傚垎鍒鍙浠ユ坠宸ユ瀯寤烘暟鎹鍜屼娇鐢▁ml銮峰彇鏁版嵁锛屽綋铹跺疄闄呴”鐩涓闇瑕佽幏鍙栫绣缁沧暟鎹銆傚彟澶朣pinner鎺т欢闇瑕佸疄鐜疠nItemSelectedListener鎺ュ彛𨱒ュ畬鎴怱pinner涓嬫媺凿滃崟镄勫姛鑳姐傚叿浣扑娇鐢ㄦ柟娉曞备笅銆
涓銆佹坠宸ユ瀯寤篠pinner涓嬫媺妗嗘暟鎹
鎸夌収姝e父娴佺▼锛岄栧厛搴斿厛鎶婂瑰簲镄刅iew鏄剧ず鍑烘潵锛岄渶瑕佺敤鍒瘪extView鍜孲pinner鎺т欢鍗冲彲锛屾墍鍦‵orm Widgets鍗冲彲镓惧埌锛岀浉鍏充唬镰佸拰绀轰緥锲惧备笅銆
锲犱负涓嬫媺凿滃崟闇瑕佹暟鎹锛屾墍浠ユ垜浠鍏堢敤List灏嗘暟鎹婧愭斁杩涘幓锛屽畾涔変负getDataSource()銆备唬镰佺ず渚嫔备笅銆
链钖庤佸垱寤篠pinnerActivity锛屽湪銮峰彇UI鎺т欢涔嫔悗锛岀劧钖庡啀鍒涘缓Spinner鎺т欢阃傞厤鍣ㄥ苟瀹氢箟涓嬫媺妗嗙殑镙峰纺锛岃繖閲屾敞镒廇rrayAdapter瀹氢箟镄勬硾鍨嬫槸String褰㈠纺镄勶纴钥屼笖android宸茬粡涓烘垜浠瀹氢箟濂戒简涓嬫媺妗嗙殑镙峰纺锛屾瘆濡俛ndroid.R.layout.simple_spinner_dropdown_item锛屽洜姝ゆ垜浠鐩存帴璋幂敤鍗冲彲瀹屾垚Spinner涓嬫媺妗嗘带浠剁殑镎崭綔銆傜浉鍏充唬镰佸备笅銆
浜屻佷娇鐢╔ML鏋勫缓Spinner涓嬫媺妗嗘暟鎹
浣跨敤XML鏋勫缓Spinner涓嬫媺妗嗘暟鎹锛岀涓姝ヨ缮鏄瑕佸厛灏哣iew瑙嗗浘琛ㄧ幇鍑烘潵锛屽拰鏂规硶涓浠g爜鐩稿悓銆傜洿鎺ョ収鎼鍗冲彲銆
杩欎釜鏂规硶鐢ㄥ埌XML锛屽洜姝ゅ氨闇瑕佸埌string.xml涓瀹氢箟鏁版嵁婧愶纴绀轰緥浠g爜濡备笅锛
链钖庡氨鏄鍦ㄥ垱寤虹殑SpinnerActivity涓鐩存帴銮峰彇string.xml涓镄勬暟鎹婧愶纴杩欓噷闇瑕佹敞镒忕殑鏄蹇呴’瀹炵幇骞舵敞鍐岀洃钖鍣∣nItemSelectedListener锛屾墠鑳戒娇涔嫔墠镄勬搷浣灭敓鏁堛俿pinner.setOnItemSelectedListener(this);銆傚彟澶栵纴阃傞厤鍣ˋrrayAdapter濡傛灉鐢ㄥ埌createFromResource銮峰彇鏁版嵁婧愶纴灏卞繀椤诲畾涔堿rrayAdapter镄勬硾鍨嬩负CharSequence褰㈠纺锛屽惁鍒欐姤阌欍傚叿浣扑唬镰佸拰瀹炵幇鏁堟灉锲惧备笅銆
㈤ 如何打造Android自定义的下拉列表框控件
一、概述
Android中的有个原生的下拉列表控件Spinner,但是这个控件有时候不符合我们自己的要求,
比如有时候我们需要类似windows 或者web网页中常见的那种下拉列表控件,类似下图这样的:
这个时候只有自己动手写一个了。其实实现起来不算很难,
本文实现的方案是采用TextView +ImageView+PopupWindow的组合方案。
先来看看我们的自己写的控件效果图吧:(源码在文章下面最后给出哈!)
二、自定义下拉列表框控件的实现
1. 自定义控件用到的布局文件和资源:
结果框的布局页面:dropdownlist_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/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="http://schemas.android.com/apk/res/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="http://schemas.android.com/apk/res/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="http://schemas.android.com/apk/res/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 caiming
*
*/
public class XCDropDownListView extends LinearLayout{
private TextView editText;
private ImageView imageView;
private PopupWindow popupWindow = null;
private ArrayList<String> dataList = new ArrayList<String>();
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(ArrayList<String> list){
dataList = list;
editText.setText(list.get(0).toString());
}
/**
* 数据适配器
* @author caiming
*
*/
class XCDropDownListAdapter extends BaseAdapter{
Context mContext;
ArrayList<String> mData;
LayoutInflater inflater;
public XCDropDownListAdapter(Context ctx,ArrayList<String> 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="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/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 caiming
*
*/
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);
ArrayList<String> list = new ArrayList<String>();
for(int i = 0;i< 6;i++){
list.add("下拉列表项"+(i+1));
}
dropDownListView.setItemsData(list);
}
}
对了,这个控件中,我没有实现点击item项回调接口,这个可能对有些写惯了回调的可能觉得少了写什么的感觉,有兴趣的你可以自己添加相关回调操作哈,这个大家应该都会把。
㈥ 怎么改变android spinner下拉列表的宽度
设置spinner下拉菜单的宽度可在布局文件中添加如下语句:
android:dropDownWidth="100dp"
其中,100dp就是下拉菜单的宽度值,单位为像素。
Spinner控件是一种列表类型的控件,它的继承关系如下:
java.lang.Object
↳ android.view.View
↳ android.view.ViewGroup
↳ android.widget.AdapterView<Textends android.widget.Adapter>
↳ android.widget.AbsSpinner
↳ android.widget.Spinner
android.widget.Spinner继承了android.view.ViewGroup类。