Android中的下拉菜单,用起来也挺舒服的。
package com.mnkjxy;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class MyActivity extends Activity {
/** Called when the activity is first created. */
private static final String[] players = {"科比", "姚明", "枷锁", "德隆", "其他"};
private TextView textView = null;
private Spinner spinner = null;
private ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.view);
spinner = (Spinner) findViewById(R.id.spinner);
//将可选内容放入到ArrayAdapter中
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, players);
//设置下拉风格
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
//将adapter放在spinner中
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
textView.setText("你喜欢的NBA明星:" + players[arg2]);
//设置显示当前选择的项
arg0.setVisibility(View.VISIBLE);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
main.xml
<?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"
>
<TextView
android:id="@+id/view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>