• Spinner的用法实现


    界面上只有一个textview和一个spinner,实现下拉列表框。

    spinner.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:orientation="vertical" >
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dip"
            android:layout_marginRight="15dip"
            android:textSize="25dip"
            />
    
        <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    


    SpinnerTest.java:

    package com.example.wenandroid;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.ArrayAdapter;
    import android.widget.Spinner;
    import android.widget.TextView;
    
    public class SpinnerTest extends Activity {
    private static final String m[]={"A型","B型","O型","AB型","其他"};
    private TextView textview;
    private Spinner spinner;
    private ArrayAdapter<String> adapter; 
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.spinner);
    		textview=(TextView)findViewById(R.id.textView);
    		spinner=(Spinner)findViewById(R.id.spinner);
    		adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m);
    		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    		spinner.setAdapter(adapter);
    		spinner.setOnItemSelectedListener(new OnItemSelectedListener()
    		{
    
    			@Override
    			public void onItemSelected(AdapterView<?> arg0, View arg1,
    					int arg2, long arg3) {
    				// TODO Auto-generated method stub
    				textview.setText("您的血型是:"+m[arg2]);
    			}
    
    			@Override
    			public void onNothingSelected(AdapterView<?> arg0) {
    				// TODO Auto-generated method stub
    				
    			}
    			
    		});
    		
    	}
    
    }
    


    运行结果如下:


  • 相关阅读:
    739. Daily Temperatures
    535. Encode and Decode TinyURL
    811. Subdomain Visit Count
    706. Design HashMap
    C++-static作用(转)
    大学四年应当如何渡过(转)
    计算机导论第八章-总结
    计算机导论第四章习题
    计算机导论-第一章习题
    20世纪最伟大的十大算法
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3194157.html
Copyright © 2020-2023  润新知