zoukankan      html  css  js  c++  java
  • android实战教程--控件篇(Spinner下拉框)

    Spinner是android系统提供的一个弹出式下拉框,以下是使用Spinner控件的一个实例:

    修改activity_main.xml文件:

    <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" >
    
        <Spinner 
            android:id="@+id/vocation"
            android:prompt="@string/vocation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <Spinner 
            android:id="@+id/sex"
            android:prompt="@string/sex"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/vocation"/>
    
    </RelativeLayout>
    


    修改string.xml文件:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <string name="app_name">Demo</string>
        <string name="action_settings">Settings</string>
        <string name="hello_world">Hello world!</string>
    	<string name="vocation">您的职业</string>
    	<string name="sex">您的性别</string>
    	<string-array name="vacationList">
    	    <item>学生</item>
    	    <item>技工</item>
    	    <item>前台</item>
    		<item >工程师</item>	    
    	</string-array>
    	<string-array name="sexList">
    	    <item >男</item>
    	    <item>女</item>
    	</string-array>
    </resources>
    

    修改MainActivity.java文件:

    package com.example.demo;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.ArrayAdapter;
    import android.widget.Spinner;
    
    public class MainActivity extends Activity {
    	private Spinner sp1;
    	private Spinner sp2;
    	
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		sp1=(Spinner) this.findViewById(R.id.vocation);
    		sp2=(Spinner) this.findViewById(R.id.sex);
    		
    		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.vacationList, android.R.layout.simple_spinner_dropdown_item);
    		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    		sp1.setAdapter(adapter);
    		
    		sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
    
    			@Override
    			public void onItemSelected(AdapterView<?> parent, View v,
    					int position, long id) {
    				// TODO Auto-generated method stub
    				setTitle("选择了"+sp1.getSelectedItem().toString());
    			}
    
    			@Override
    			public void onNothingSelected(AdapterView<?> arg0) {
    				// TODO Auto-generated method stub
    				setTitle("还没选中");
    			}
    		});
    		
    		adapter=ArrayAdapter.createFromResource(this, R.array.sexList, android.R.layout.simple_spinner_dropdown_item);
    		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    		sp2.setAdapter(adapter);
    		sp2.setOnItemSelectedListener(new OnItemSelectedListener() {
    
    			@Override
    			public void onItemSelected(AdapterView<?> parent, View v,
    					int position, long id) {
    				// TODO Auto-generated method stub
    				setTitle("选择了"+sp2.getSelectedItem().toString());
    			}
    
    			@Override
    			public void onNothingSelected(AdapterView<?> arg0) {
    				// TODO Auto-generated method stub
    				setTitle("还没选中");
    			}
    		});
    		
    	}
    
    	@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;
    	}
    
    }
    

    效果如下:



  • 相关阅读:
    Linux Windows平台添加pip源
    Python redis交互
    Redis redis-trib集群配置
    Linux 加阿里yum源
    阿里云yum源镜像
    Android实战——GreenDao3.2的使用,爱不释手
    一个Demo带你彻底掌握View的滑动冲突
    观察者模式简单理解
    Android Studio插件之MVPHelper,一键生成MVP代码
    城市导航列表
  • 原文地址:https://www.cnblogs.com/IntelligentBrain/p/5111293.html
Copyright © 2011-2022 走看看