亲宝软件园·资讯

展开

Android spinner下垃菜单 Android spinner下垃菜单用法实例详解

dreamzml 人气:0
想了解Android spinner下垃菜单用法实例详解的相关内容吗,dreamzml在本文为您仔细讲解Android spinner下垃菜单的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,spinner,下垃菜单,下面大家一起来学习吧。

本文实例讲述了Android spinner下垃菜单用法。分享给大家供大家参考,具体如下:

一、使用数组作为数据源

1. 在layout布局文件:声明一个TextView控件和一个Spinner控件,代码如下

<Spinner android:id="@+id/Spinner01"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"></Spinner>

2. 在Activity文件加入以下代码

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 SpinnerActivity extends Activity {
  private static final String[] m={"A型","B型","O型","AB型","其他"};
  private TextView view ;
  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);
    view = (TextView) findViewById(R.id.spinnerText);
    spinner = (Spinner) findViewById(R.id.Spinner01);
    //将可选内容与ArrayAdapter连接起来
    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,m);
    //设置下拉列表的风格
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //将adapter 添加到spinner中
    spinner.setAdapter(adapter);
    //添加事件Spinner事件监听
    spinner.setOnItemSelectedListener(new SpinnerSelectedListener());
    //设置默认值
    spinner.setVisibility(View.VISIBLE);
  }
  //使用数组形式操作
  class SpinnerSelectedListener implements OnItemSelectedListener{
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
      view.setText("你的血型是:"+m[arg2]);
    }
    public void onNothingSelected(AdapterView<?> arg0) {
    }
  }
}

二、使用XML作为数据源

1. 在values文件夹下新建一个arryas.xml文件:代码如下

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string-array name="plantes">
    <item>NOKIA</item>
    <item>MOTO</item>
    <item>HTC</item>
    <item>LG</item>
    <item>其他</item>
  </string-array>
</resources>

2. 在Activity文件加入以下代码

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 SpinnerActivity extends Activity {
  private TextView view2;
  private Spinner spinner2;
  private ArrayAdapter adapter2;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.spinner);
    spinner2 = (Spinner) findViewById(R.id.spinner02);
    view2 = (TextView) findViewById(R.id.spinnerText02);
    //将可选内容与ArrayAdapter连接起来
    adapter2 = ArrayAdapter.createFromResource(this, R.array.plantes, android.R.layout.simple_spinner_item);
    //设置下拉列表的风格
 adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //将adapter2 添加到spinner中
    spinner2.setAdapter(adapter2);
    //添加事件Spinner事件监听
    spinner2.setOnItemSelectedListener(new SpinnerXMLSelectedListener());
    //设置默认值
    spinner2.setVisibility(View.VISIBLE);
  }
  //使用XML形式操作
  class SpinnerXMLSelectedListener implements OnItemSelectedListener{
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
      view2.setText("你使用什么样的手机:"+adapter2.getItem(arg2));
    }
    public void onNothingSelected(AdapterView<?> arg0) {
    }
  }
}

希望本文所述对大家Android程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论