- 注册时间
- 2011-3-22
- 最后登录
- 2013-6-27
- 在线时间
- 11644 小时
- 阅读权限
- 150
- 积分
- 62779
- 帖子
- 28923
- 精华
- 1
- UID
- 6
|
那啥。。小学没毕业 表达能力不行。直接上代码吧。
layout 代码
spinner_layout.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">
- <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content"
- android:drawingCacheQuality="auto" android:drawSelectorOnTop="false"></Spinner>
- </LinearLayout>
复制代码 strings- <string-array name="spinnerTest" id="spinnerTest">
- <item>漠漠 </item>
- <item>混混</item>
- <item>YY</item>
- <item>宝宝</item>
- </string-array>
复制代码 java代码- package com.qwj.MyAndroid.bll;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView.OnItemSelectedListener;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Spinner;
- import android.widget.Toast;
- public class SpinnerActivity extends Activity {
-
- private Spinner sp;
- private String[] stringArray;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.spinner_layout);
-
-
- //用于ArrayAdapter的Sources
- ArrayList<String> dataSources=new ArrayList<String>();
- //获取values文件夹下面的Strings.xml的string-array 资源文件
- stringArray=getResources().getStringArray(R.array.spinnerTest);
- for(int i=0;i<stringArray.length;i++)
- {
- dataSources.add(stringArray[i]);
- }
-
- //获取控件
- sp=(Spinner)findViewById(R.id.spinner);
- //创建ArrayAdapter
- ArrayAdapter<String> spinnerAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,dataSources);
- //设置选择框样式
- spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
-
- //设置适配器
- sp.setAdapter(spinnerAdapter);
- //设置标题
- sp.setPrompt("选择坏人");
-
- //设置选择事件
- sp.setOnItemSelectedListener(new OnItemSelectedListener() {
-
- @Override
- public void onItemSelected(AdapterView<?> arg0, View arg1,
- int arg2, long arg3) {
- // TODO Auto-generated method stub
- try{
- System.out.println(stringArray[(int)sp.getSelectedItemId()]);
- }catch(Exception ex)
- {
- System.out.println("select Error: "+ex.getMessage());
- }
- //显示选中消息
- Toast.makeText(SpinnerActivity.this,"你选择了:"+sp.getSelectedItem().toString()+" 坏人。", Toast.LENGTH_LONG).show();
- }
-
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
-
- }
- });
- }
- }
复制代码 效果。。
|
-
1
查看全部评分
-
|