当前位置:   article > 正文

Android-高级控件-下拉列表(Spinner)&列表视图(ListView)_android下拉列表控件

android下拉列表控件

Spinner

语法

 <Spinner

         android:id="@+id/ID号"

         android:layout_width="wrap_content"

          android:layout_height="wrap_content"

          android:entries=“@array/数组名称"

  ......

    />

 android:entries 

选项,用于指定列表项,若在布局文件中不指定该属性,可在Java代码中通过为其指定适配器的方式指定

有两种为其添加列表项的方式

通过XML文件添加

 在values目录下,新建一个数组资源文件array.xml

  1. <resources>
  2. <string-array name="zhuanye">
  3. <item>计算机科学与技术</item>
  4. <item>软件工程</item>
  5. <item>网络工程</item>
  6. <item>物联网工程</item>
  7. </string-array>
  8. </resources>

 引用

  1. <Spinner
  2. android:id="@+id/major"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:entries="@array/zhuanye" />

通过Java代码添加

  1. String[] city=new String[]{"北京", "上海", "天津""重庆""广州" };
  2. Spinner citysp=(Spinner)findViewById(R.id.sp_city);
  3. ArrayAdapter<String> spadapter=new
  4. ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,city);
  5. citysp.SetAdapter(spadapter);

练习题目

 (1)界面构成:

①5个文本框用于显示提示信息“姓名:”、“年龄”、“专业”、“学历”、“爱好”;

②2个编辑框分别用于接收学生的姓名和年龄;

③3个单选按钮用于专业选择(计算机科学与技术、软件工程、网络工程);

④1个Spinner用于学历选择,使用数组资源获取列表项(高中、本科、硕士研究生、博士研究生、其他);

⑤n个复选框用于爱好选择(n>=3);

⑥3个普通按钮,文本分别显示为“录入”、“重置”、“显示”;

⑦1个ListView,用于显示录入的所有学生信息。

(2)程序功能:

单击“录入”按钮,将用户输入和选择的各项信息写入数组;

单击“重置”按钮,清空用户输入的信息;

③单击“显示”按钮,将所有录入的学生信息在ListView中显示。

项目结构

布局

布局预览

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content" >
  9. <TextView
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:height="50px"
  13. android:text=" 姓 名 : "
  14. />
  15. <EditText
  16. android:id="@+id/et_name"
  17. android:layout_width="300px"
  18. android:layout_height="wrap_content"
  19. android:singleLine="true"
  20. android:hint="请输入姓名"
  21. />
  22. </LinearLayout>
  23. <LinearLayout
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content" >
  26. <TextView
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:height="50px"
  30. android:text=" 年 龄 : "
  31. />
  32. <EditText
  33. android:id="@+id/et_age"
  34. android:layout_width="300px"
  35. android:layout_height="wrap_content"
  36. android:inputType="text"
  37. android:hint="请输入年龄"
  38. />
  39. </LinearLayout>
  40. <LinearLayout
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content" >
  43. <TextView
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:height="50px"
  47. android:text=" 专 业 : "
  48. />
  49. <RadioGroup
  50. android:id="@+id/radioGroup1"
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:orientation="vertical"
  54. >
  55. <RadioButton
  56. android:id="@+id/rb1"
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:text="计算机科学与技术"
  60. />
  61. <RadioButton
  62. android:id="@+id/rb2"
  63. android:layout_width="wrap_content"
  64. android:layout_height="wrap_content"
  65. android:text="软件工程"
  66. />
  67. <RadioButton
  68. android:id="@+id/rb3"
  69. android:layout_width="wrap_content"
  70. android:layout_height="wrap_content"
  71. android:text="网络工程"
  72. />
  73. </RadioGroup>
  74. </LinearLayout>
  75. <LinearLayout
  76. android:layout_width="wrap_content"
  77. android:layout_height="wrap_content" >
  78. <TextView
  79. android:layout_width="wrap_content"
  80. android:layout_height="wrap_content"
  81. android:height="50px"
  82. android:text=" 学 历 : "
  83. />
  84. <Spinner
  85. android:id="@+id/edu"
  86. android:layout_width="wrap_content"
  87. android:layout_height="wrap_content"
  88. android:entries="@array/xueli" />
  89. </LinearLayout>
  90. <LinearLayout
  91. android:layout_width="wrap_content"
  92. android:layout_height="wrap_content"
  93. >
  94. <TextView
  95. android:layout_width="wrap_content"
  96. android:layout_height="wrap_content"
  97. android:height="50px"
  98. android:text=" 爱 好 : "
  99. />
  100. <GridLayout
  101. android:layout_width="match_parent"
  102. android:layout_height="wrap_content"
  103. android:columnCount="3"
  104. android:id="@+id/g1_hobby"
  105. >
  106. <CheckBox
  107. android:id="@+id/cb1"
  108. android:layout_width="wrap_content"
  109. android:layout_height="wrap_content"
  110. android:text="阅读"
  111. />
  112. <CheckBox
  113. android:id="@+id/cb2"
  114. android:layout_width="wrap_content"
  115. android:layout_height="wrap_content"
  116. android:text="旅游"
  117. />
  118. <CheckBox
  119. android:id="@+id/cb3"
  120. android:layout_width="wrap_content"
  121. android:layout_height="wrap_content"
  122. android:text="发呆"
  123. />
  124. <CheckBox
  125. android:id="@+id/cb4"
  126. android:layout_width="wrap_content"
  127. android:layout_height="wrap_content"
  128. android:text="唱歌"
  129. />
  130. <CheckBox
  131. android:id="@+id/cb5"
  132. android:layout_width="wrap_content"
  133. android:layout_height="wrap_content"
  134. android:text="编程"
  135. />
  136. <CheckBox
  137. android:id="@+id/cb6"
  138. android:layout_width="wrap_content"
  139. android:layout_height="wrap_content"
  140. android:text="运动"
  141. />
  142. </GridLayout>
  143. </LinearLayout>
  144. <LinearLayout
  145. android:layout_width="wrap_content"
  146. android:layout_height="wrap_content"
  147. android:layout_gravity="center">
  148. <Button
  149. android:id="@+id/bt_login"
  150. android:layout_width="wrap_content"
  151. android:layout_height="wrap_content"
  152. android:text="录入"
  153. android:onClick="onClickLogin"
  154. />
  155. <Button
  156. android:id="@+id/bt_reset"
  157. android:layout_width="wrap_content"
  158. android:layout_height="wrap_content"
  159. android:text="重置"
  160. android:onClick="onClickReset"
  161. />
  162. <Button
  163. android:id="@+id/bt_xianshi"
  164. android:layout_width="wrap_content"
  165. android:layout_height="wrap_content"
  166. android:text="显示"
  167. />
  168. </LinearLayout>
  169. <ListView
  170. android:id="@+id/lsv1"
  171. android:layout_width="match_parent"
  172. android:layout_height="wrap_content"
  173. android:divider="#f00"
  174. android:dividerHeight="2dp"
  175. android:layout_gravity="center"
  176. />
  177. </LinearLayout>

list_item.xml

  1. <Te<?xml version="1.0" encoding="utf-8" ?>
  2. <!--2.模板-->
  3. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:textSize="20sp"
  7. android:textColor="#009">
  8. </TextView>

array.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string-array name="xueli">
  4. <item>高中</item>
  5. <item>本科</item>
  6. <item>硕士研究生</item>
  7. <item>博士研究生</item>
  8. <item>其它</item>
  9. </string-array>
  10. </resources>

逻辑实现

MainActivity

  1. package com.example.test;
  2. import android.content.Context;
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.view.View;
  6. import android.widget.AdapterView;
  7. import android.widget.ArrayAdapter;
  8. import android.widget.Button;
  9. import android.widget.CheckBox;
  10. import android.widget.EditText;
  11. import android.widget.GridLayout;
  12. import android.widget.ListView;
  13. import android.widget.RadioButton;
  14. import android.widget.RadioGroup;
  15. import android.widget.Spinner;
  16. import android.view.View. OnClickListener;
  17. import java.util.ArrayList;
  18. public class MainActivity extends Activity {
  19. //声明
  20. private Context context;
  21. Spinner sp;
  22. //声明ListView对象
  23. ListView lsv;
  24. RadioGroup zy;
  25. EditText etname,etage;
  26. CheckBox cb1,cb2,cb3,cb4,cb5,cb6;
  27. CheckBox cb[]=new CheckBox[6];
  28. Button bt1,bt2,btxianshi;
  29. //复选按钮组
  30. GridLayout g1;
  31. String name,age,specialty,shobby,edu;
  32. //
  33. ArrayList<String> list=new ArrayList<String>();;
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.activity_main);
  38. context =this;
  39. //获取控件对象
  40. //登记按钮和重置按钮
  41. bt1 = (Button) findViewById(R.id.bt_login);
  42. bt2 = (Button) findViewById(R.id.bt_reset);
  43. btxianshi = (Button) findViewById(R.id.bt_xianshi);
  44. //复选框
  45. cb[0] = (CheckBox) findViewById(R.id.cb1);
  46. cb[1] = (CheckBox) findViewById(R.id.cb2);
  47. cb[2] = (CheckBox) findViewById(R.id.cb3);
  48. cb[3] = (CheckBox) findViewById(R.id.cb4);
  49. cb[4] = (CheckBox) findViewById(R.id.cb5);
  50. cb[5] = (CheckBox) findViewById(R.id.cb6);
  51. g1 = (GridLayout) findViewById(R.id.g1_hobby);
  52. etname = (EditText) findViewById(R.id.et_name);
  53. etage = (EditText) findViewById(R.id.et_age);
  54. zy = (RadioGroup) findViewById(R.id.radioGroup1);
  55. sp = (Spinner)findViewById(R.id.edu);
  56. lsv=(ListView)findViewById(R.id.lsv1);
  57. //登记信息
  58. bt1.setOnClickListener(new OnClickListener() {
  59. @Override
  60. public void onClick(View v) {
  61. // TODO Auto-generated method stub
  62. edu = sp.getSelectedItem().toString();
  63. shobby="";
  64. //
  65. for(int i=0;i<g1.getChildCount() ;i++) {
  66. CheckBox cbs = (CheckBox) g1.getChildAt(i);
  67. //判断状态
  68. if(cb[i].isChecked()) {
  69. shobby+=cbs.getText().toString()+" ";
  70. }
  71. }
  72. for(int i=0;i<zy.getChildCount();i++) {
  73. RadioButton r = (RadioButton) zy.getChildAt(i);
  74. if(r.isChecked()) {
  75. specialty = r.getText().toString();
  76. }
  77. }
  78. name = etname.getText().toString();
  79. age = etage.getText().toString();
  80. String s="\n姓名:"+name+"\n年龄:"+age+"\n专业:"+specialty+"\n学历:"+edu+"\n爱好:"+shobby;
  81. list.add(s);
  82. }
  83. });
  84. //重置
  85. bt2.setOnClickListener(new OnClickListener() {
  86. @Override
  87. public void onClick(View v) {
  88. // TODO Auto-generated method stub
  89. //清空编辑框
  90. etname.setText("");
  91. etage.setText("");
  92. //消除单选按钮的被选中状态
  93. for(int i=0;i<zy.getChildCount();i++) {
  94. RadioButton r = (RadioButton) zy.getChildAt(i);
  95. if(r.isChecked()) {
  96. r.setChecked(false);
  97. }
  98. }
  99. //清除复选按钮的被选中状态
  100. for(int i=0;i<g1.getChildCount() ;i++) {
  101. CheckBox cbs = (CheckBox) g1.getChildAt(i);
  102. cbs.setChecked(false);
  103. }
  104. lsv.setAdapter(null);
  105. sp.setSelection(0);
  106. }
  107. });
  108. btxianshi.setOnClickListener(new OnClickListener() {
  109. @Override
  110. public void onClick(View view) {
  111. /**
  112. * 从list集合中读取数据显示在文本框中
  113. * 创建适配器对象
  114. */
  115. ArrayAdapter<String> adapter =
  116. new ArrayAdapter<String>(
  117. MainActivity.this,
  118. R.layout.list_item,
  119. list);
  120. //关联
  121. lsv.setAdapter(adapter);
  122. }
  123. });
  124. }
  125. // @Override
  126. // public boolean onCreateOptionsMenu(Menu menu) {
  127. // // Inflate the menu; this adds items to the action bar if it is present.
  128. // getMenuInflater().inflate(R.menu.main, menu);
  129. // return true;
  130. // }
  131. }

运行

登记

显示

 重置

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/111534
推荐阅读
相关标签
  

闽ICP备14008679号