package com.turboradio.googlesdk;
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.Spinner;
import android.widget.TextView;
public class Ex_04_08_spinner extends Activity{
private static final String [] countriesStr = {
"北京市","上海市","天津市","重庆市"
};
private TextView myTextView;
private Spinner mySpinner;
private Animation myAnimation;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ex_04_08);
myTextView = (TextView)findViewById(R.id.myTextView);
mySpinner = (Spinner)findViewById(R.id.mySpinner);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,countriesStr);
adapter.setDropDownViewResource(R.layout.myspinnerdropdown);
mySpinner.setAdapter(adapter);
mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
myTextView.setText("选择的是:" + countriesStr[position]);
//view.setVisibility(View.VISIBLE);
}
public void onNothingSelected(AdapterView<?> arg0) {
}});
/**取得Animation定义的res/anim目录下**/
myAnimation = AnimationUtils.loadAnimation(this, R.anim.my_anim);
mySpinner.setOnTouchListener(new Spinner.OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
/**将mySpinner运行Animation**/
mySpinner.startAnimation(myAnimation);
mySpinner.setVisibility(View.INVISIBLE);
returnfalse;
}});
mySpinner.setOnFocusChangeListener(new Spinner.OnFocusChangeListener(){
public void onFocusChange(View v, boolean hasFocus) {
}});
}
}
layout.ex_04_08.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Spinner
android:id="@+id/mySpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
myspinnerdropdown.xml
<TextViewxmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="24sp"
android:singleLine="true"
style="?android:attr/spinnerDropDownItemStyle"
/>
res/anim/
my_anim.xml
<?xmlversion="1.0"encoding="utf-8"?>
<setxmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="-100%p"
android:duration="300"
/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="300"
/>
</set>