• android学习笔记六——Spinner


    注:参考http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0105/2264.html

    Spinner

    ==>

    Spinner是ViewGroup的间接子类,因此也可作为容器使用。

    常用属性:

      android:prompt——设置该列表框选择框的提示信息

      android:entries——使数组资源设置该下拉列表框的列表项目

    注意:在使用Spinner时,如果可预先指导列表框内容信息,则不需要编写代码,只需要为Spinner指定 android:entries  属性即可实现一个下拉列表框。

    实例一:最简单的使用方式——通过资源文件绑定数据源

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="books">
            <item>大话设计模式</item>
            <item>大话android</item>
            <item>java 高级编程</item>
        </string-array>
    </resources>
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".IndexActivity" >
    
        <Spinner
            android:id="@+id/spcontent"
            android:entries="@array/books"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </RelativeLayout>
    

    注意:需要为values文件夹下添加arrays.xml文件,如下图:

    注意:可通过处理setOnItemSelectedListener添加Spinner选择事件...

    选择事件处理

    package com.example.myspinner;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.Spinner;
    import android.widget.Toast;
    
    public class IndexActivity extends Activity
    {
    	@Override
    	protected void onCreate(Bundle savedInstanceState)
    	{
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.index);
    
    		Spinner spinner = (Spinner) this.findViewById(R.id.spcontent);
    		spinner.setOnItemSelectedListener(new SpinnerSelectLintener());
    
    	}
    
    	private class SpinnerSelectLintener implements OnItemSelectedListener
    	{
    		@Override
    		public void onItemSelected(AdapterView<?> parent, View view,
    				int position, long id)
    		{
    			String[] books = getResources().getStringArray(R.array.books);
    			String value = "选择了:" + books[position];
    			Toast.makeText(IndexActivity.this, value, 3000).show();
    		}
    
    		@Override
    		public void onNothingSelected(AdapterView<?> parent)
    		{
    		}
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu)
    	{
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.index, menu);
    		return true;
    	}
    
    }
    

    实例二 ——

    通过ArrayAdapter绑定数据源

    package com.example.myspinner2;
    
    import android.os.Bundle;
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Toast;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.Spinner;
    
    @SuppressLint("ShowToast")
    public class MainActivity extends Activity
    {
    	@Override
    	protected void onCreate(Bundle savedInstanceState)
    	{
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		Spinner spinner = (Spinner) this.findViewById(R.id.spcontent);
    		// 建立数据源
    		String[] mItems = getResources().getStringArray(R.array.books);
    		// 建立Adapter并且绑定数据源
    		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    				android.R.layout.simple_spinner_item, mItems);
    		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    		// 绑定 Adapter到控件
    		spinner.setAdapter(adapter);
    
    		spinner.setOnItemSelectedListener(new OnItemSelectedListener()
    		{
    			@Override
    			public void onItemSelected(AdapterView<?> parent, View view,
    					int position, long id)
    			{
    				String[] books = getResources().getStringArray(R.array.books);
    				String value = "选择了:" + books[position];
    				Toast.makeText(MainActivity.this, value, 3000).show();
    			}
    
    			@Override
    			public void onNothingSelected(AdapterView<?> parent)
    			{
    			}
    		});
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu)
    	{
    		// Inflate the menu; this adds items to the action bar if it is present.
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
    
    }
    

    运行效果如下:

    注意:

    1.第二个参数是Spinner未展开菜单时Spinner的默认样式,android.R.layout.simple_spinner_item是系统自带的内置布局

    2.通过如下设置可以更改Spinner展开以及显示样式

      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, mItems);

      adapter.setDropDownViewResource(android.R.layout.simple_list_item_checked);

    3.如果不设置adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)会怎样呢?

      会造成未展开的sipnner和展开的菜单都是一种布局样式

    4.其实simple_spinner_item和simple_spinner_dropdown_item两者的名字正好反映了他们的区别。一个应用于下拉一个应用于Spinner本身。

    5.Spinner的菜单显示方式

    它有两种显示形式,一种是下拉菜单,一种是弹出框,菜单显示形式是spinnerMode属性决定的:

    1
    2
    android:spinnerMode="dropdown"
    android:spinnerMode="dialog"

     在android2.3上没有这个属性,系统默认将Spinner弹出菜单显示成dialog。

    Theme.Light和Theme.Holo.Light下Spinner在不同模式下的效果对比图如下所示:

    Spinner支持的XML属性:

      1. entries: 直接在xml布局文件中绑定数据源(可以不设置,即可以在Activity中动态绑定)

      2.spinnerMode: Spinner的显示形式

      3.android:prompt="@string/app_name"——在Spinner弹出选择对话框的时候对话框的标题

    实例三——使用自定义Adapter

    注意:使用自定义Adapter需要添加子布局文件;需要添加Adapter泛型

    子布局文件==》
    <?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:drawableLeft="@drawable/tjcl_1"
            android:paddingRight="8dip"
            android:paddingTop="8dip"
            android:textSize="20sp" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="8dip"
            android:paddingTop="8dip"
            android:textSize="20sp" />
    
    </LinearLayout>
    
    自定义Adapter以及实现==>
    package com.example.myspinner3;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Spinner;
    import android.widget.TextView;
    
    public class MainActivity extends Activity
    {
    	@Override
    	protected void onCreate(Bundle savedInstanceState)
    	{
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		// 初始化控件
    		Spinner spinner = (Spinner) findViewById(R.id.spcontent);
    		// 建立数据源
    		List<Person> persons = new ArrayList<Person>();
    		persons.add(new Person("猪八戒", "上海 "));
    		persons.add(new Person("王麻子", "上海 "));
    		persons.add(new Person("姜子牙", "北京"));
    		persons.add(new Person("王婆子", "广州 "));
    		// 建立Adapter绑定数据源
    		MyAdapter adapter = new MyAdapter(this, persons);
    		// 绑定Adapter
    		spinner.setAdapter(adapter);
    	}
    
    	public class Person
    	{
    		private String personName;
    		private String personAddress;
    
    		public Person(String personName, String personAddress)
    		{
    			super();
    			this.personName = personName;
    			this.personAddress = personAddress;
    		}
    
    		public String getPersonName()
    		{
    			return personName;
    		}
    
    		public void setPersonName(String personName)
    		{
    			this.personName = personName;
    		}
    
    		public String getPersonAddress()
    		{
    			return personAddress;
    		}
    
    		public void setPersonAddress(String personAddress)
    		{
    			this.personAddress = personAddress;
    		}
    
    	}
    
    	public class MyAdapter extends BaseAdapter
    	{
    		private List<Person> personList;
    		private Context mContext;
    
    		public MyAdapter(Context pContext, List<Person> pList)
    		{
    			this.mContext = pContext;
    			this.personList = pList;
    		}
    
    		@Override
    		public int getCount()
    		{
    			return personList.size();
    		}
    
    		@Override
    		public Object getItem(int position)
    		{
    			return personList.get(position);
    		}
    
    		@Override
    		public long getItemId(int position)
    		{
    			return position;
    		}
    
    		/**
    		 * 下面是重要代码
    		 */
    		@Override
    		public View getView(int position, View view, ViewGroup parent)
    		{
    			LayoutInflater layoutinflater = LayoutInflater.from(mContext);
    			view = layoutinflater.inflate(R.layout.itemspinner, null);
    			if (view != null)
    			{
    				TextView tv1 = (TextView) view.findViewById(R.id.textView1);
    				TextView tv2 = (TextView) view.findViewById(R.id.textView2);
    
    				tv1.setText(personList.get(position).getPersonName());
    				tv2.setText(personList.get(position).getPersonAddress());
    			}
    			return view;
    		}
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu)
    	{
    		getMenuInflater().inflate(R.menu.main, menu);
    		return true;
    	}
    
    }
    

      

    实现效果如下图所示:

     LayoutInflater——参考http://www.cnblogs.com/top5/archive/2012/05/04/2482328.html

    ==>

    LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。

    具体作用:

    1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;
    2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。
    注意:
      inflate 方法与 findViewById 方法不同;
      inflater 是用来找 res/layout 下的 xml 布局文件,并且实例化;
      findViewById() 是找具体 xml 布局文件中的具体 widget 控件(如:Button、TextView 等)。
     
  • 相关阅读:
    180602-nginx多域名配置
    180601-MySql性能监控工具MyTop
    180530-反射获取泛型类的实际参数
    180531-Spring中JavaConfig知识小结
    RabbitMQ基础教程之Spring&JavaConfig使用篇
    RabbitMQ基础教程之使用进阶篇
    RabbitMQ基础教程之基本使用篇
    jquery控制文字内容溢出用点点点(…)省略号表示
    用CSS设置Table的细边框的最好用的方法
    web app 自适应方案总结 弹性布局之rem
  • 原文地址:https://www.cnblogs.com/YYkun/p/5750119.html
Copyright © 2020-2023  润新知