本博文介绍了基本的Spinner的使用方法
首先写一下xml中的代码部分
<Spinner android:id="@+id/myspinner" android:layout_width="fill_parent" android:layout_height="wrap_content" />
再写一下Strings.xml中 的文件部分
<string-array name="tongxue" > <item>侯</item> <item>张</item> <item>赵</item> <item>李</item> <item>李</item> <item>刘</item> </string-array>
最后给大家展示一下activity的代码部分
public class MainActivity extends Activity { private Spinner spin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spin=(Spinner)findViewById(R.id.myspinner); ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.tongxue, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spin.setAdapter(adapter); spin.setPrompt("test"); spin.setOnItemSelectedListener(new SpinnerLis()); } //定义一个监听器 class SpinnerLis implements OnItemSelectedListener{ @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { String selected=parent.getItemAtPosition(position).toString(); Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_LONG).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }
本文出自 “Java大白的战地” 博客,请务必保留此出处http://8023java.blog.51cto.com/10117207/1664415
原文地址:http://8023java.blog.51cto.com/10117207/1664415