本文主要是介绍联合使用Spinner和setDropDownViewResource,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
layout文件中的main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/title"
/>
<Spinner
android:id="@+id/mySpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
layoutout文件中的myspinner_dropdown.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns: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"
>
</TextView>
res目录下新建的anim文件下的my_anim.xml
<?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"
/>
<!-- 位置转换动画小效果 -->
<alpha
android:fromAlpha="3.0"
android:toAlpha="6.0"
android:duration="300"
/>
<!-- 透明度转换动画效果 -->
</set>
public class SpinnergaojiActivity extends Activity implements OnItemSelectedListener,OnTouchListener,OnFocusChangeListener{
private static final String[] countriesStr = {"美国","英国","日本","韩国"};
private TextView myTextView;
private Spinner mySpinner;
private ArrayAdapter<String> adapter;
private Animation myAnimation; //用于动画的
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTextView = (TextView)this.findViewById(R.id.myTextView);
mySpinner = (Spinner)this.findViewById(R.id.mySpinner);
adapter = new ArrayAdapter<String>(SpinnergaojiActivity.this, android.R.layout.simple_spinner_item,countriesStr);
adapter.setDropDownViewResource(R.layout.myspinner_dropdown);
mySpinner.setAdapter(adapter);
mySpinner.setOnItemSelectedListener(this);
myAnimation = AnimationUtils.loadAnimation(this, R.anim.my_anim);
mySpinner.setOnTouchListener(this);
mySpinner.setOnFocusChangeListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.spinnergaoji, menu);
return true;
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
myTextView.setText("您选择的 是:"+countriesStr[arg2]);
arg0.setVisibility(View.VISIBLE);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
v.startAnimation(myAnimation);
v.setVisibility(View.INVISIBLE);
return false;
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
}
}
这篇关于联合使用Spinner和setDropDownViewResource的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!