android下拉菜单制作

Spinner1.java

package com.example.spinner1; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; public class spinner1 extends Activity { /** Called when the activity is first created. */ private Spinner spinner=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); spinner = (Spinner)findViewById(R.id.Spinnered); ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.plants_array, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); /*//方法2 List list = new ArrayList(); list.add("test1"); list.add("test2"); ArrayAdapter adapter = new ArrayAdapter(this,R.layout.item,R.id.TextViewId,list); 如果按上面这样做:则需要新建一个item.xml */ spinner.setAdapter(adapter); spinner.setPrompt("下拉菜单"); spinner.setOnItemSelectedListener(new SpinnerOnItemSelectListener()); } } class SpinnerOnItemSelectListener implements OnItemSelectedListener{ @Override public void onItemSelected(AdapterView AdapterView, View view, int position, long arg3) { // TODO Auto-generated method stub String selected = AdapterView.getItemAtPosition(position).toString(); System.out.println(selected); } @Override public void onNothingSelected(AdapterView arg0) { // TODO Auto-generated method stub System.out.println("NothingSelected"); } }

//按方法二的item.xml,方法一则不需要

 

main.xml

string.xml

Hello World, spinner1! spinner1 金银花 菊花 金花 银花 红花

 更多技术博客,欢迎关注楼主个人博客 : 小杰博客

你可能感兴趣的:(android下拉菜单制作)