package yzy.cxt.com; 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; /** * 自定义下拉菜单 * @author 青衣修罗 * */ public class MainActivity extends Activity { private TextView textView; private Spinner spinner; private ArrayAdapter<String> adapter; private static final String[] etoile= { "刘德华", "毛泽东", "刘翔", "盖茨" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textView = (TextView)findViewById(R.id.textView); spinner = (Spinner)findViewById(R.id.spinner); // 初始化适配器 adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,etoile); //设置自定义下拉菜单样式 adapter.setDropDownViewResource(R.layout.menulayout); //adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //将适配器绑定到下拉菜单 spinner.setAdapter(adapter); // 为下拉菜单添加监听事件 spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // 设定选中的内容 textView.setText(etoile[arg2]); // 让下拉菜单的内容显示 arg0.setVisibility(View.VISIBLE); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); } }
Android开发基础:自定义下拉列表样式(spinner的用
内容版权声明:除非注明,否则皆为本站原创文章。