Android Spinner学习

在Android开发中,很多时候需要用到Spinner(类似Html中的下拉框)来展示数据,,比如,用户注册界面,用Spinner来展示省份是个不错的选择。 

1、在布局文件中声明 Spinner

 <Spinner 
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/dropitems"
        />

2、在values下面创建 dropitems.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="dropitems">请选择</string>
    <string-array name="drop_items_array">
        <item >湖南</item>
         <item >湖北</item>
          <item >福建</item>
           <item >江西</item>
    </string-array>
</resources>

3、java代码

package com.zhoucj.spinnerdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {

    Spinner spinner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        spinner=(Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource
                (this, R.array.drop_items_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        
    }

    
    
    
    @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;
    }

}

运行效果

如果我们要获取用户选择的值,Spinner添加 OnItemSelectedListener监听,看下面的代码。

package com.zhoucj.spinnerdemo;

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;
import android.widget.Toast;

public class MainActivity extends Activity {

    Spinner spinner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        spinner=(Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource
                (this, R.array.drop_items_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new spinnerItemselectedListener());
        
    }

    class spinnerItemselectedListener implements OnItemSelectedListener
    {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Toast.makeText(MainActivity.this, arg0.getItemAtPosition(arg2).toString(), Toast.LENGTH_LONG).show();
            
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
            
        }
        
    }
    
    
    @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;
    }

}

运行效果如下

好了,Spinner就介绍到这里了。

祝大家 工作顺利   更上一层楼

 

posted @ 2013-06-26 11:06  最後的輕語  阅读(219)  评论(0编辑  收藏  举报