zoukankan      html  css  js  c++  java
  • 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
    				
    			}
    			
    		});
    		
    	}
    
    }
    


    运行结果如下:


  • 相关阅读:
    基于SQL脚本将数据库表及字段提取为C#中的类
    libiconv字符集转换库在C#中的使用
    select选中事件
    PHP 字符串处理
    织梦自定义标签
    JQuery 实现 Tab 切换 index
    Juery 实现淡出 淡现效果
    SEO之H1,H2,H3,H4....STRONG使用方法
    Robots.txt 编写
    JS 获取时间
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3194157.html
Copyright © 2011-2022 走看看