使用Spinner控件实现选项选择

动画样式文件:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <translate 
  android:fromXDelta="0" 
  android:toXDelta="-100%p" 
  android:duration="300"
  >
  </translate>
  <alpha 
  android:fromAlpha="1.0" 
  android:toAlpha="0.0" 
  android:duration="300">
  </alpha>
</set>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/white"
  >
  <TextView  
  android:id="@+id/myTextView"
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="@string/title"
  android:textColor="@drawable/black"
  >
  </TextView>
  <Spinner 
  android:id="@+id/mySpinner"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" 
  >
  </Spinner>
</LinearLayout>


package com.EX026;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.ListView;
import android.widget.Spinner;

public class EX026 extends Activity
{
  private static final String[] countriesStr =
  { "怜怜", "莉莉", "妾妾", "娜娜" };
  private TextView myTextView;
  private Spinner mySpinner;
  private ArrayAdapter<String> adapter;
  Animation myAnimation;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    /* 载入main.xml Layout */
    setContentView(R.layout.main);

    /* 以findViewById()取得对象 */
    myTextView = (TextView) findViewById(R.id.myTextView);
    mySpinner = (Spinner) findViewById(R.id.mySpinner);

    adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, countriesStr);
    /* myspinner_dropdown为自定义下拉菜单模式定义在res/layout目录下 */
    adapter.setDropDownViewResource(R.layout.myspinner);

    /* 将ArrayAdapter添加Spinner对象中 */
    mySpinner.setAdapter(adapter);

    /* 将mySpinner添加OnItemSelectedListener */
    mySpinner.setOnItemSelectedListener
     (new Spinner.OnItemSelectedListener()
    {
      @Override
      public void onItemSelected
       (AdapterView<?> arg0, View arg1, int arg2,
          long arg3)
      {
        /* 将所选mySpinner的值带入myTextView中 */
        myTextView.setText("您选择的是" + countriesStr[arg2]);
        /* 将mySpinner显示 */
        arg0.setVisibility(View.VISIBLE);
      }

      @Override
      public void onNothingSelected(AdapterView<?> arg0)
      {
        // TODO Auto-generated method stub
      }
    });

    /* 取得Animation定义在res/anim目录下 */
    myAnimation = AnimationUtils.loadAnimation(this, R.anim.my_anim);

    /* 将mySpinner添加OnTouchListener */
    mySpinner.setOnTouchListener(new Spinner.OnTouchListener()
    {

      @Override
      public boolean onTouch(View v, MotionEvent event)
      {
        /* 将mySpinner运行Animation */
        v.startAnimation(myAnimation);
        /* 将mySpinner隐藏 */
        v.setVisibility(View.INVISIBLE);
        return false;
      }

    });

    mySpinner.setOnFocusChangeListener(new Spinner.OnFocusChangeListener()
    {
      @Override
      public void onFocusChange(View v, boolean hasFocus)
      {
        // TODO Auto-generated method stub
      }
    });
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值