关于下拉框的一些应用

做下拉框可以利用系统自带的方法创建adpter,通过数组传入需要的字符串。

布局文件如下

<?xml version="1.0" encoding="utf-8"?>    
<LinearLayout   
    android:id="@+id/widget28"    
    android:layout_width="fill_parent"    
    android:layout_height="fill_parent"    
    android:orientation="vertical"  
    android:layout_gravity="center_vertical"
	               android:gravity="center_horizontal"  
    xmlns:android="http://schemas.android.com/apk/res/android" >    
    <TextView    
        android:id="@+id/TextView_Show"    
        android:layout_width="fill_parent"    
        android:layout_height="wrap_content"    
        android:text="你选择的是"    
        android:textSize="25sp">    
    </TextView>    
    <Spinner    
        android:id="@+id/spinner_City"    
        android:layout_width="fill_parent"    
        android:layout_height="wrap_content">    
    </Spinner>
    <TextView    
        android:id="@+id/TextView_Show1"    
        android:layout_width="fill_parent"    
        android:layout_height="wrap_content"    
        android:text="你选择的是"    
        android:textSize="25sp">    
    </TextView>    
    <Spinner    
        android:id="@+id/spinner_City1"    
        android:layout_width="fill_parent"    
        android:layout_height="wrap_content">    
    </Spinner> 
    <Button android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:layout_marginBottom="50dp"
        	 android:layout_gravity="center_horizontal"
	         android:gravity="center_horizontal"
        	android:text="OK"
        	android:id="@+id/button"
        	android:visibility="invisible"/>  
</LinearLayout>

java代码如下:

package com.example.mydrawsector;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
        
    public class SpinnerActivity extends Activity {     
        /** Called when the activity is first created. */    
        private List<String> list = new ArrayList<String>();   
        private List<String> list1 = new ArrayList<String>(); 
        private TextView myTextView;    
        private TextView myTextView1;   
        private Spinner mySpinner;    
        private Spinner mySpinner1; 
        private ArrayAdapter<String> adapter;     
        private ArrayAdapter<String> adapter1; 
        private Button button;
        @Override    
        public void onCreate(Bundle savedInstanceState) {     
            super.onCreate(savedInstanceState);     
            setContentView(R.layout.activity_main);     
            //第一步:添加一个下拉列表项的list,这里添加的项就是下拉列表的菜单项     
            list.add("北京");     
            list.add("上海");     
            list.add("深圳");     
            list.add("南京");     
            list.add("重庆");
            list1.add("公交");
            list1.add("地铁");
            list1.add("自驾游");
            button = (Button)findViewById(R.id.button);
            myTextView = (TextView)findViewById(R.id.TextView_Show);     
            mySpinner = (Spinner)findViewById(R.id.spinner_City);
            myTextView1 = (TextView)findViewById(R.id.TextView_Show1);     
            mySpinner1 = (Spinner)findViewById(R.id.spinner_City1); 
            
            //第二步:为下拉列表定义一个适配器,这里就用到里前面定义的list。     
            adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);     
            //第三步:为适配器设置下拉列表下拉时的菜单样式。     
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);     
            //第四步:将适配器添加到下拉列表上     
            adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list1);     
            //第三步:为适配器设置下拉列表下拉时的菜单样式。     
            adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
            mySpinner.setAdapter(adapter);     
            mySpinner1.setAdapter(adapter1);
            //第五步:为下拉列表设置各种事件的响应,这个事响应菜单被选中     
            mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){     
                @SuppressWarnings("unchecked")  
                public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) {     
                    // TODO Auto-generated method stub     
                    /* 将所选mySpinner 的值带入myTextView 中*/    
                    myTextView.setText("您选择的是:"+ adapter.getItem(arg2));     
                    /* 将mySpinner 显示*/    
                    arg0.setVisibility(View.VISIBLE);  
                    isShowOKButton();
                }     
                @SuppressWarnings("unchecked")  
                public void onNothingSelected(AdapterView arg0) {     
//                    // TODO Auto-generated method stub     
//                    myTextView.setText("NONE");     
//                    arg0.setVisibility(View.VISIBLE);     
                }     
            });   
            mySpinner1.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){     
                @SuppressWarnings("unchecked")  
                public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) {     
                    // TODO Auto-generated method stub     
                    /* 将所选mySpinner 的值带入myTextView 中*/    
                    myTextView1.setText("您选择的是:"+ adapter1.getItem(arg2));     
                    /* 将mySpinner 显示*/    
                    arg0.setVisibility(View.VISIBLE);  
                    isShowOKButton();
                }     
                @SuppressWarnings("unchecked")  
                public void onNothingSelected(AdapterView arg0) {     
                    // TODO Auto-generated method stub     
//                    myTextView1.setText("NONE");     
//                    arg0.setVisibility(View.VISIBLE);     
                }     
            });
          
//            /*下拉菜单弹出的内容选项触屏事件处理*/    
//            mySpinner.setOnTouchListener(new Spinner.OnTouchListener(){     
//                public boolean onTouch(View v, MotionEvent event) {     
//                    // TODO Auto-generated method stub     
//                    /* 将mySpinner 隐藏,不隐藏也可以,看自己爱好*/    
//                    v.setVisibility(View.INVISIBLE);     
//                    return false;     
//                }     
//            });     
            /*下拉菜单弹出的内容选项焦点改变事件处理*/    
            mySpinner.setOnFocusChangeListener(new Spinner.OnFocusChangeListener(){     
            public void onFocusChange(View v, boolean hasFocus) {     
            // TODO Auto-generated method stub     
                v.setVisibility(View.VISIBLE);     
            }     
            });     
        } 
        public void isShowOKButton(){
        	if((myTextView.getText()!=null||myTextView.getText()!="")&&(myTextView1.getText()!=null||myTextView1.getText()!="")){
        		button.setVisibility(View.VISIBLE);
        	}
        }
    }

小结

1.Spinner中textview显示的内容为list中第一个元素。

2.继续学习自定义风格的下拉框并且改变spinner的默认值。

相关推荐