下拉框

原创 2015年11月18日 14:33:34


package com.example.spinner;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private String[] start=null;
	private String[] target=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		start = this.getResources().getStringArray(R.array.citys_start);
		target = this.getResources().getStringArray(R.array.citys_target);
				

		Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
		//ArrayAdapter adapter1 = new ArrayAdapter(this,android.R.layout.simple_list_item_1, android.R.id.text1, start);			
		spinner1.setAdapter(new MyAdapter1(this,-1));
		spinner1.setSelection(2);

		Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
		ArrayAdapter adapter2 = new ArrayAdapter(this,android.R.layout.simple_list_item_1, android.R.id.text1, target);		
		spinner2.setAdapter(adapter2);
		spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,long id) {		
				Toast.makeText(getApplicationContext(), target[pos],Toast.LENGTH_SHORT).show();				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
	}

	private class MyAdapter1 extends ArrayAdapter {
    
		private  LayoutInflater infalter;
		
		public MyAdapter1(Context context, int resource) {
			super(context, resource);
			infalter=LayoutInflater.from(context);
		}

		@Override
		public int getCount() {

			return start.length;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			View view = infalter.inflate(android.R.layout.simple_list_item_1,null);
			TextView text= (TextView) view.findViewById(android.R.id.text1);
			text.setText("始发地:"+start[position]);
			
			return view;
		}

		@Override
		public View getDropDownView(int position, View convertView,ViewGroup parent) {
			View view = infalter.inflate(android.R.layout.simple_list_item_1,null);
			TextView text= (TextView) view.findViewById(android.R.id.text1);
			text.setText("始发地:"+start[position]);
			
			return view;
		}
	}
}

<LinearLayout 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:orientation="horizontal"
    tools:context="com.example.spinner.MainActivity" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<resources>
    
    <string-array name = "citys_start">
        <item>北京</item>
        <item>成都</item>
        <item>广州</item>
        <item>上海</item>
        <item>西安</item>
        <item>新疆</item>
        <item>深圳</item>
        <item>美国</item>
        <item>韩国</item>
        <item>大千世界</item>
		<item>三千小世界</item>
	</string-array>
	
    <string-array name="citys_target">
        <item>北京</item>
        <item>上海</item>
        <item>成都</item>
        <item>广州</item>
        <item>北京</item>
        <item>上海</item>
        <item>成都</item>
        <item>广州</item>
        <item>北京</item>
        <item>上海</item>
        <item>成都</item>
        <item>广州</item>
        <item>北京</item>
        <item>上海</item>
        <item>成都</item>
        <item>广州</item>
    </string-array>
    
</resources>




版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【Unity】I2本地化教程——下拉框选项

如何获得Popup List 的 Label的值 1.获得PopupList下拉框当前值使得调用其值 创建Sprite文件以及Label Sprite设置如下 Label设置如...

下拉框三级联动

RepositoryItemComboBox获得焦点弹出下拉框

在dev中的gridcontrol中添加了一列  RepositoryItemComboBox下拉框,想要实现当  RepositoryItemComboBox列获得焦点的时候,自动弹出下拉框...

可以输入的下拉框

  • 2014-10-13 10:22
  • 113KB
  • 下载

解决自定义标签select级联下拉框异步加载显示值不正确

解决自定义标签select级联下拉框异步加载显示值不正确

带搜索功能的下拉框

  • 2017-08-21 14:50
  • 51KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)