android > Spinner ( 下拉列表 )
先是在 layout 里面 建议个 Spinner 控件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Spinner android:id="@+id/spnner" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
然后在 主activity 里
package so.com; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; public class SoActivity extends Activity { /** Called when the activity is first created. */ private List<String> list = new ArrayList<String>(); private Spinner spinner; private ArrayAdapter<String> adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); spinner = (Spinner)findViewById(R.id.spnner); list.add("北京"); list.add("上海"); list.add("天津"); list.add("南京"); adapter = new ArrayAdapter<String>(SoActivity.this, android.R.layout.simple_spinner_item, list); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); ///增加监听事件,其实在程序启动时就调用了该监听 spinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Toast.makeText(SoActivity.this, adapter.getItem(arg2), Toast.LENGTH_LONG).show(); } public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub Toast.makeText(SoActivity.this, "Selected None", Toast.LENGTH_LONG).show(); } }); } }
相关推荐
绿豆饼 2020-04-10
狼窝 2020-03-20
Mexican 2011-01-20
满城风絮 2011-09-06
yxx0 2015-04-15
ruanjianxiong 2016-09-20
fanleiym 2017-02-09
learningITwell 2016-08-18
virusplayer 2015-04-15
Ifree团队 2013-07-08
emptoney 2013-03-19
toperfect 2013-03-19
yxx0 2013-01-22
tufeizhang 2012-11-30
najiutan 2012-02-26
Mexican 2012-02-15