Android中Spinner控件的使用(十)

来源:转载

package com.itarchy.spinner.demo;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;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.AutoCompleteTextView;import android.widget.Button;import android.widget.SimpleAdapter;import android.widget.Spinner;import android.widget.Toast;import com.itatchy.spinner.demo.R;public class MainActivity extends Activity implements OnClickListener { private Spinner mSpinner; private ArrayList<String> dataSource; private ArrayAdapter<String> arrayAdapter; private Spinner spPhone; private ArrayList<Map<String, Object>> phoneDataSource; private SimpleAdapter simpleAdapter; private AutoCompleteTextView autoTextView; private ArrayList<String> autoTvSource; private ArrayAdapter<String> autoTvAdapter; private Button tvSearch; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initSpinner(); initPhone(); initAutoTv(); } private void initSpinner() { // 获取控件 mSpinner = (Spinner) this.findViewById(R.id.spinner); // 1、初始化数据源 dataSource = new ArrayList<String>(); dataSource.add("A型"); dataSource.add("B型"); dataSource.add("AB型"); dataSource.add("O型"); // 2、将数据源添加到数组适配器 arrayAdapter = new ArrayAdapter<String>(getApplication(), android.R.layout.simple_spinner_dropdown_item, dataSource); arrayAdapter = new ArrayAdapter<String>(getApplication(), R.layout.custom_spinner, dataSource); // 3、将适配器添加到拉列表控件上 mSpinner.setAdapter(arrayAdapter); mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // 获取点击的item String result = arrayAdapter.getItem(position); // 显示 Toast.makeText(getApplication(), "您的血型是:" + result, 1000) .show(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } private void initPhone() { // 获取控件 spPhone = (Spinner) this.findViewById(R.id.sp_phone); // 1、初始化数据源 phoneDataSource = new ArrayList<Map<String, Object>>(); // 对象1 Map<String, Object> mapOb1 = new HashMap<String, Object>(); mapOb1.put("txt", "iphone"); mapOb1.put("img", R.drawable.ic_launcher); phoneDataSource.add(mapOb1); // 对象2 Map<String, Object> mapOb2 = new HashMap<String, Object>(); mapOb2.put("txt", "Vivo"); mapOb2.put("img", R.drawable.ic_launcher); phoneDataSource.add(mapOb2); // 对象3 Map<String, Object> mapOb3 = new HashMap<String, Object>(); mapOb3.put("txt", "小米"); mapOb3.put("img", R.drawable.ic_launcher); phoneDataSource.add(mapOb3); String[] from = { "txt", "img" }; int[] to = { R.id.phone_name, R.id.phone_img }; // 2、将数据源添加到适配器 // SimpleAdapter:可以放些简单的对象,布局比较单一 simpleAdapter = new SimpleAdapter(getApplication(), phoneDataSource, R.layout.custom_phone_spinner, from, to); // 3、将适配器添加到拉列表控件上 spPhone.setAdapter(simpleAdapter); // 点击Item时执行 spPhone.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // 先获取一个items对象 Map<String, Object> resultMap = (Map<String, Object>) simpleAdapter .getItem(position); // 从点击的item里获取某一个值 String result = (String) resultMap.get("txt"); // 显示 Toast.makeText(getApplication(), "您的喜欢的手机是:" + result, 1000) .show(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } private void initAutoTv() { autoTextView = (AutoCompleteTextView) this.findViewById(R.id.autotv); tvSearch = (Button) this.findViewById(R.id.tv_search); // 初始化数据源 autoTvSource = new ArrayList<String>(); autoTvSource.add("qeqr"); autoTvSource.add("qqr"); autoTvSource.add("qeqrfasf"); autoTvSource.add("qqrafaf"); autoTvSource.add("qeqrzvva"); autoTvSource.add("qqrafsafa"); autoTvSource.add("af"); autoTvSource.add("14314"); autoTvSource.add("iyoiy"); autoTvSource.add("afda"); autoTvSource.add("adafsdf"); autoTvSource.add("13414124"); autoTvSource.add("adafsdf"); autoTvSource.add("qerqr"); autoTvSource.add("3425432"); autoTvSource.add("adafsdf"); autoTvSource.add("qewqr"); autoTvSource.add("adfafa"); autoTvSource.add("zadfaf"); autoTvSource.add("qerq"); autoTvSource.add("adafsdf"); autoTvSource.add("qrewq"); autoTvSource.add("adafsdf"); autoTvSource.add("adwerwqafsdf"); autoTvSource.add("14144"); autoTvSource.add("adafsdf"); autoTvSource.add("adfaf"); autoTvSource.add("qewqr"); // 2、将数据源添加到适配器上 autoTvAdapter = new ArrayAdapter<String>(getApplication(), android.R.layout.simple_spinner_dropdown_item, autoTvSource); // 3、 autoTextView.setAdapter(autoTvAdapter); tvSearch.setOnClickListener(this); } @Override public void onClick(View v) { String searchResult = autoTextView.getText().toString(); // 显示 Toast.makeText(getApplication(), searchResult, 1000).show(); }}
<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=".MainActivity" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="Spinner控件的使用" /> <TextView android:id="@+id/tv_value" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/title" android:text="您的血型:" /> <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_value" /> <TextView android:id="@+id/tv_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/spinner" android:text="您喜欢的手机类型:" /> <Spinner android:id="@+id/sp_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_phone" /> <AutoCompleteTextView android:id="@+id/autotv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/sp_phone" android:layout_toLeftOf="@+id/tv_search" android:completionThreshold="1" /> <Button android:id="@+id/tv_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/sp_phone" android:text="搜索" /></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itatchy.spinner.demo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itarchy.spinner.demo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>




分享给朋友:
您可能感兴趣的文章:
随机阅读: