Android 下拉框的实现
2015-08-02 09:46
591 查看
本文主要介绍下拉框Spinner的实现,具体操作看代码。
1.SpinnerActivity.java
2.布局文件spinner.xml
3.在工程目录的values目录下新建arrays.xml
4.注意:需要在AndroidManifest.xml注册相应Activity.
1.SpinnerActivity.java
//下拉框 public class SpinnerActivity extends Activity { private Spinner spinner1; private Spinner spinner2; private Button ok; private ArrayAdapter countiesAdapter; private String[] mCounties={"beijing","guangdong","guangxi","hunan"}; private List<String> allCounties=new ArrayList<String>(); private String result="你选择的是:"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.spinner); spinner1=(Spinner)findViewById(R.id.spinner1); spinner2=(Spinner)findViewById(R.id.spinner2); ok=(Button)findViewById(R.id.ok); for(int i=0;i<mCounties.length;i++){ allCounties.add(mCounties[i]); } countiesAdapter=new ArrayAdapter<String>(SpinnerActivity.this,android.R.layout.simple_spinner_item,allCounties); countiesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner1.setAdapter(countiesAdapter); ArrayAdapter adapter=ArrayAdapter.createFromResource(SpinnerActivity.this,R.array.counties,android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner2.setAdapter(adapter); //单击第一个下拉按钮时,显示选择的值。 spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapter, View view, int position, long id) { // TODO Auto-generated method stub String str=(String)spinner1.getAdapter().getItem((int)id); setTitle(result+str); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); //单击第二个下拉按钮时,显示选择的值。 spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapter, View view, int position, long id) { String str=(String)spinner2.getAdapter().getItem(position); setTitle(result+str); } @Override public void onNothingSelected(AdapterView<?> arg0) { } }); //单击确定按钮,提取选择的值. ok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setTitle(result+spinner1.getSelectedItem()+" - >> "+spinner2.getSelectedItem()); } }); } }
2.布局文件spinner.xml
<?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" > <TextView android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="下拉框1:" /> <Spinner android:id="@+id/spinner1" android:layout_width="150dip" android:layout_height="wrap_content" android:drawSelectorOnTop="false" /> <TextView android:id="@+id/label2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="下拉框2:" /> <Spinner android:id="@+id/spinner2" android:layout_width="150dip" android:layout_height="wrap_content" android:drawSelectorOnTop="false" /> <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/ok" /> </LinearLayout>
3.在工程目录的values目录下新建arrays.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="counties"> <item>AAA</item> <item>BBB</item> <item>CCC</item> <item>DDD</item> <item>EEE</item> </string-array> </resources>
4.注意:需要在AndroidManifest.xml注册相应Activity.
相关文章推荐
- [深入理解Android卷一 全文-第二章]深入理解JNI
- [深入理解Android卷一 全文-第二章]深入理解JNI
- Android_打开DDMS没有文件夹显示
- Android 菜单——上下文菜单、选择菜单以及子菜单
- [深入理解Android卷一全文-第一章]阅读前的准备工作
- Android 属性动画总小结
- 关于Android5.x适配的一点小问题(NDK error: SIGABRT)
- Android AsyncTask usage
- android自定义渐变圆环进度条
- Android差量更新
- Android_开源框架_Afinal
- Android第二夜
- Ubuntu Android Studio NDK开发
- Android Studio not find in root project android()
- Android远程服务三:android AIDL远程服务个人归纳总结
- Android自定义View
- Android SDK 无法更新的解决方法
- Android深入浅出之Binder机制
- Ubuntu 12.04下面设别无法识别android设备的解决方法
- Android NDK之如何避开Java_xxx标准定义