当前位置 : 主页 > 网络编程 > 其它编程 >

AndroidUI(Spinner)详解

来源:互联网 收集:自由互联 发布时间:2023-07-02
目录1.应用场景2.常用属性3.xml数据源布局实现Spinner4.ArrayadapterArrayList布局实现Spinner5.自定义adapter数据源布局实现Spin 目录     1.应用场景     2.常用属性     3.xml数据源布局实现Spin
目录1.应用场景2.常用属性3.xml数据源布局实现Spinner4.ArrayadapterArrayList布局实现Spinner5.自定义adapter数据源布局实现Spin 目录     1.应用场景     2.常用属性     3.xml数据源布局实现Spinner     4.ArrayadapterArrayList布局实现Spinner     5.自定义adapter数据源布局实现Spinner     6.多级连联动Spinner实现(市县区三级联动)      1.应用场景     Spinner即列表选项框多应用于城市选择或者一些搜索分类选取一种的情况      2.常用属性

android:entries"values/"android:popupBackground""android:dropDownSelector""android:layout_width"wrap_content"android:layout_height"wrap_content"android:dropDownHorizontalOffset"12dp"android:dropDownVerticalOffset"1dp"android:dropDownWidth"40dp"android:spinnerMode"dropdown|dialog"android:scrollbars"vertical"android:prompt""android:scrollbarThumbHorizontal"mipmap/ic_launcher" android:scrollbarTrackHorizontal"" 3.xml数据源布局实现Spinner     1)数据源myarraylist.xml

上海北京广州深圳杭州成都      2)主布局xml

        3)Java类文件

package com.example.spinnerui;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.Spinner;import android.widget.Toast;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {private Spinner dropdownSp;//Spinner默认选择第一项所以刚进入是会触发onItemSelected事件为了解决这个问题定义类firstSelectprivate boolean firstSelecttrue;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//初始化dropdownSp (Spinner) findViewById(R.id.dropdownSp);//设置监听dropdownSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {/** parent:指示spinner* view显示item的空间这里指TextView* position被选中的item的位置* id选中项的id* */Overridepublic void onItemSelected(AdapterView parent, View view, int position, long id) {if (!firstSelect){//打印提示parent.getItemAtPosition(position)获取选中位置的值Toast.makeText(MainActivity.this,"你选择了"parent.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();Log.d("id",id"");Log.d("position",position"");Log.d("view",view.toString());Log.d("parent",parent.toString());}else {firstSelectfalse;}}Overridepublic void onNothingSelected(AdapterView parent) {}});}}     4)效果截图

4.ArrayadapterArrayList布局实现Spinner     1)自定义的已选取item的布局drop_item.xml

    2)主布局文件

    3)Java文件代码 

package com.example.spinnerui;import android.content.Context;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.Toast;import java.util.ArrayList;public class SecondActivity extends AppCompatActivity {private Spinner dialogSp;private boolean firstSelecttrue;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);//初始化dialogSp (Spinner) findViewById(R.id.dialogSp);//初始化数据源ArrayList arraynew ArrayList();for (int i0;i<5;i){array.add(i,"数据"i);}/** ArrayAdapter(Context context, int resource, List objects)* context表示上下文* int resource选取的item的布局* objects数据* *///系统内置的选取的Item的TextView布局android.R.layout.simple_spinner_item//自定义的选取的Item的TextView布局R.layout.drop_itemArrayAdapter myArrayAdapter new ArrayAdapter(this,R.layout.drop_item,array);//设置列表框item的布局myArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//设置适配器dialogSp.setAdapter(myArrayAdapter);//设置监听dialogSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {/** parent:指示spinner* view显示item的空间这里指TextView* position被选中的item的位置* id选中项的id* */Overridepublic void onItemSelected(AdapterView parent, View view, int position, long id) {if (!firstSelect){//打印提示parent.getItemAtPosition(position)获取选中位置的值Toast.makeText(SecondActivity.this,"你选择了"parent.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();Log.d("id",id"");Log.d("position",position"");Log.d("view",view.toString());Log.d("parent",parent.toString());}else {firstSelectfalse;}}Overridepublic void onNothingSelected(AdapterView parent) {}});}}     4)从xml文件加载数据适配

ArrayAdapter myArrayAdapter ArrayAdapter.createFromResource(this, R.array.myarraydata,android.R.layout.simple_spinner_item);     5)效果截图 5.自定义adapter数据源布局实现Spinner     1)自定义adapterMyCustomAdapter.java  

package com.example.spinnerui;import android.content.Context;import android.graphics.drawable.Drawable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;import java.util.List;import java.util.Map;import java.util.Objects;/*** Created by elimy on 2016-08-10.*/public class MyCustomAdapter extends BaseAdapter {private Context context;private List list;private LayoutInflater inflater;/**带参构造方法*/public MyCustomAdapter(List list, Context context) {this.list list;this.context context;inflater LayoutInflater.from(context);}Overridepublic int getCount() {return list.size();}Overridepublic Object getItem(int position) {return position;}Overridepublic long getItemId(int position) {return position;}Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder null;if (holder null){holdernew ViewHolder();if(convertView null){convertViewinflater.inflate(R.layout.custom_drop_layout,null);holder.imageView (ImageView) convertView.findViewById(R.id.app_icon);holder.appText (TextView) convertView.findViewById(R.id.list_item);holder.descText (TextView) convertView.findViewById(R.id.list_desc);convertView.setTag(holder);}else {//根据Tag获取已经存在的ViewHolderholder (ViewHolder) convertView.getTag();}//设置显示内容holder.appText.setText(list.get(position).get("name").toString());holder.descText.setText(list.get(position).get("desc").toString());holder.imageView.setBackgroundResource((Integer)list.get(position).get("image"));}return convertView;}/**实现ViewHolder类*/class ViewHolder{public ImageView imageView;public TextView appText;public TextView descText;}}            2)每个下拉列表item布局custom_drop_layout.xml    

        3)主布局xml

      4)主布局Java文件ThreeActivity.java

package com.example.spinnerui;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.Spinner;import android.widget.Toast;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;public class ThreeActivity extends AppCompatActivity {private Spinner dropdownSp;private ArrayList list new ArrayList();private Map map;private boolean firstSelecttrue;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_three);//初始化dropdownSp (Spinner) findViewById(R.id.dropdownSp);//初始化自定义adapterlistgetData();MyCustomAdapter myCustomAdapter new MyCustomAdapter(list,ThreeActivity.this);dropdownSp.setAdapter(myCustomAdapter);//设置监听dropdownSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {/** parent:指示spinner* view显示item的空间这里指TextView* position被选中的item的位置* id选中项的id* */Overridepublic void onItemSelected(AdapterView parent, View view, int position, long id) {if (!firstSelect){//打印提示parent.getItemAtPosition(position)获取选中位置的值Toast.makeText(ThreeActivity.this,"你选择了"list.get(position).get("name").toString(),Toast.LENGTH_SHORT).show();Log.d("id",id"");Log.d("position",position"");Log.d("view",view.toString());Log.d("parent",parent.toString());}else {firstSelectfalse;}}Overridepublic void onNothingSelected(AdapterView parent) {}});}/*初始化数据源*/private ArrayList getData() {mapnew HashMap();map.put("image",R.drawable.calendar);map.put("name","日历");map.put("desc","这是一款超级时尚的日历APP");list.add(map);mapnew HashMap();map.put("image",R.drawable.camera);map.put("name","相机");map.put("desc","完美美颜美美哒");list.add(map);mapnew HashMap();map.put("image",R.drawable.clock);map.put("name","闹钟");map.put("desc","妈妈再也不担心你睡懒觉啦");list.add(map);mapnew HashMap();map.put("image",R.drawable.games_control);map.put("name","游戏控制");map.put("desc","游戏尽在掌握");list.add(map);return list;}}      5)效果截图      6.多级连联动Spinner实现(市县区三级联动)     1)数据spinner_array_data.xml

上海湖南浙江四川黄浦区长宁区虹口区浦东新区南京东路街道外滩街道半淞园路街道华阳路街道江苏路街道新华路街道欧阳路街道曲阳路街道广中路街道潍坊新村街道陆家嘴街道周家渡街道长沙市湘潭市衡阳市岳阳市雨湖区岳塘区湘潭县天心区芙蓉区长沙县珠晖区雁峰区石鼓区岳阳楼区云溪区君山区成都市雅安市乐山市资阳市高新区金牛区青羊区宝兴县小金县芦山县市中区沙湾区五通桥区雁江区安岳县乐至县杭州市温州市宁波市嘉兴市下城区江干区西湖区鹿城区龙湾区瓯海区海曙区江东区江北区南湖区秀洲区嘉善县  

    2)主布局activity_four.xml

    3)主布局java文件FourActivity.java

package com.example.spinnerui;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.TextView;/*** 注意在初始化int[]数组的时候注意city数组中的数据index要与province array.xml中的顺序要一致county数组也是* 同样的道理这样才可以实现省份和城市数据对应城市和县区数据对应。* ps:提供另一种思路就是将county数据设置为二维数组对应分别对应省份和城市* */public class FourActivity extends AppCompatActivity {private Spinner provinceSpinner, citySpinner, countySpinner;private ArrayAdapter provinceAdapter, cityAdapter, countyAdapter;private TextView result;private String provinceName, cityName, countyName;private boolean firstSelect true;//省份对应的城市数组初始化将城市数组和省份的positon关联起来int[] cityofprovince {R.array.shanghai_city_data, R.array.hunan_city_data,R.array.zhejiang_city_data, R.array.sichuan_city_data};//城市对应的区县数组初始化将县区和城市positon关联起来int[] shanghaicityofcounty {R.array.huangpu_county_data, R.array.changning_county_data,R.array.hongkou_county_data, R.array.pudong_county_data};int[] hunancityofcounty {R.array.changsha_county_data, R.array.xiangtan_county_data,R.array.hengyang_county_data, R.array.yueyang_county_data};int[] zhejiangcityofcounty {R.array.hangzhou_county_data, R.array.wenzhou_county_data,R.array.ningbo_county_data, R.array.jiaxing_county_data};int[] sichuancityofcounty {R.array.chengdu_county_data, R.array.yaan_county_data,R.array.leshang_county_data, R.array.ziyang_county_data};Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_four);//实例化spinnerprovinceSpinner (Spinner) findViewById(R.id.provinceSpinner);citySpinner (Spinner) findViewById(R.id.citySpinner);countySpinner (Spinner) findViewById(R.id.countySpinner);result (TextView) findViewById(R.id.result);//从xml文件资源中实例化provinceAdapterprovinceAdapter ArrayAdapter.createFromResource(FourActivity.this, R.array.province_data, android.R.layout.simple_spinner_item);//设置下拉列表布局android.R.layout.simple_spinner_dropdown_item android内置的布局provinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//设置适配器provinceSpinner.setAdapter(provinceAdapter);//省级spinner添加监听事件provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {Overridepublic void onItemSelected(AdapterView parent, View view, int position, long id) {provinceName provinceSpinner.getSelectedItem().toString();final int provinceId provinceSpinner.getSelectedItemPosition();Log.d("debug:", provinceName "" provinceId);//根据选取province的position从一开始的int数组中动态适配相应的city数据if(true){cityAdapter ArrayAdapter.createFromResource(FourActivity.this, cityofprovince[position], android.R.layout.simple_spinner_item);cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);citySpinner.setAdapter(cityAdapter);//设置select事件监听citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {Overridepublic void onItemSelected(AdapterView parent, View view, int position, long id) {//获取选中item的文本cityName citySpinner.getSelectedItem().toString();//获取选中positionint cityId citySpinner.getSelectedItemPosition();Log.d("debug:", cityName "" cityId);switch (provinceId) {case 0:setAdapterFun(shanghaicityofcounty[cityId]);break;case 1:setAdapterFun(hunancityofcounty[cityId]);break;case 2:setAdapterFun(zhejiangcityofcounty[cityId]);break;case 3:setAdapterFun(sichuancityofcounty[cityId]);break;default:break;}//为countySpinner设置监听器countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {Overridepublic void onItemSelected(AdapterView parent, View view, int position, long id) {//根据位置获取文本内容countyName countySpinner.getSelectedItem().toString();Log.d("debug:", countyName "" countyName);result.setText("你的选择是"provinceName"—>"cityName"—>"countyName);}Overridepublic void onNothingSelected(AdapterView parent) {}});}Overridepublic void onNothingSelected(AdapterView parent) {}});}}Overridepublic void onNothingSelected(AdapterView parent) {}});}/*function:功能主要是采取通用的方式为Spinner设置不同的数据* param: i 表示对应数据数组的id值* */private void setAdapterFun(int i) {//通过xml文件初始化ArrayAdaptercountyAdapter ArrayAdapter.createFromResource(FourActivity.this, i, android.R.layout.simple_spinner_item);//设置下拉列表框布局countyAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//为countySpinner设置适配器countySpinner.setAdapter(countyAdapter);}}  

    4)效果截图

【感谢龙石为本站提供api网关 http://www.longshidata.com/pages/apigateway.html】
上一篇:phonegap安卓视频播放解决方案
下一篇:没有了
网友评论