当前位置: 代码迷 >> Android >> Spinner 示例
  详细解决方案

Spinner 示例

热度:635   发布时间:2016-04-24 11:51:53.0
阅读《Android 从入门到精通》(11)——下拉列表

下拉列表(Spinner)

和 RadioGroup 类似,但比 RadioGroup 强:一个 Spinner 对象包含多个子项,每个子项只有选中和未选中两种状态。Spinner 继承关系如下:

android.view.ViewGroup
android.widget.AdapterView<T extends android.widget.Adapter>
android.widget.AbsSpinner
android.widget.Spinner

Spinner 类方法


Spinner 示例

完整工程:http://download.csdn.net/detail/sweetloveft/9404957

下述程序中,主要学习 Spinner 的使用方法,需要注意当布局文件中的 Spinner 没有指定 android:entries="@array/language" 时,需要使用代码的加载方法代码加载的方法

1.MainActivity.java

package com.sweetlover.activity;import com.sweetlover.spinnerdemo.R;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.Spinner;import android.widget.Toast;public class MainActivity extends Activity {	@Override	protected void onCreate(Bundle savedInstanceState) {		// TODO Auto-generated method stub		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);				Spinner spinner = (Spinner)findViewById(R.id.spinner1);		/*		// 以下部分是当布局文件中 activity_main.xml 没有指定 android:entries="@array/language" 时,代码加载的方法		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.language, android.R.layout.simple_spinner_item);		// 设置下拉列表风格		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);		spinner.setAdapter(adapter);		*/		spinner.setOnItemSelectedListener(new ItemSelectedListener());	}		class ItemSelectedListener implements OnItemSelectedListener {		@Override		public void onItemSelected(AdapterView<?> parent, View view,				int position, long id) {			// TODO Auto-generated method stub			Toast.makeText(MainActivity.this, "选择的语言:" + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();		}		@Override		public void onNothingSelected(AdapterView<?> parent) {			// TODO Auto-generated method stub			Toast.makeText(MainActivity.this, "未选择任何语言!", Toast.LENGTH_SHORT).show();		}	}}

2.activity_main.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:padding="30dp"    android:orientation="vertical" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:text="@string/tittle"        android:textAppearance="?android:attr/textAppearanceMedium" />    <Spinner        android:id="@+id/spinner1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_marginTop="30dp"        android:entries="@array/language" /></LinearLayout>

3.string.xml

<resources>    <string name="app_name">SpinnerDemo</string>    <string name="tittle">选择你喜欢的语言:</string></resources>

4.arrays.xml

<?xml version="1.0" encoding="utf-8"?><resources>        <string-array name="language" >        <item>汇编语言</item>        <item>C语言</item>        <item>C++语言</item>        <item>Java语言</item>        <item>C#语言</item>        <item>HTML语言</item>        <item>PHP语言</item>        <item>XML语言</item>    </string-array>    </resources>

5.AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.sweetlover.spinnerdemo"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="19" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity android:name="com.sweetlover.activity.MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN"/>                <category android:name="android.intent.category.LAUNCHER"/>            </intent-filter>        </activity>    </application></manifest>

  相关解决方案