赞
踩
<Spinner
android:id="@+id/ID号"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries=“@array/数组名称"
......
/>
可选项,用于指定列表项,若在布局文件中不指定该属性,可在Java代码中通过为其指定适配器的方式指定
有两种为其添加列表项的方式
在values目录下,新建一个数组资源文件array.xml
- <resources>
- <string-array name="zhuanye">
- <item>计算机科学与技术</item>
- <item>软件工程</item>
- <item>网络工程</item>
- <item>物联网工程</item>
- </string-array>
- </resources>
引用
- <Spinner
- android:id="@+id/major"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:entries="@array/zhuanye" />
- String[] city=new String[]{"北京", "上海", "天津" , "重庆" , "广州" };
- Spinner citysp=(Spinner)findViewById(R.id.sp_city);
- ArrayAdapter<String> spadapter=new
- ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,city);
- citysp.SetAdapter(spadapter);
(1)界面构成:
①5个文本框用于显示提示信息“姓名:”、“年龄”、“专业”、“学历”、“爱好”;
②2个编辑框分别用于接收学生的姓名和年龄;
③3个单选按钮用于专业选择(计算机科学与技术、软件工程、网络工程);
④1个Spinner用于学历选择,使用数组资源获取列表项(高中、本科、硕士研究生、博士研究生、其他);
⑤n个复选框用于爱好选择(n>=3);
⑥3个普通按钮,文本分别显示为“录入”、“重置”、“显示”;
⑦1个ListView,用于显示录入的所有学生信息。
(2)程序功能:
①单击“录入”按钮,将用户输入和选择的各项信息写入数组;
②单击“重置”按钮,清空用户输入的信息;
③单击“显示”按钮,将所有录入的学生信息在ListView中显示。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
-
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:height="50px"
- android:text=" 姓 名 : "
- />
- <EditText
- android:id="@+id/et_name"
- android:layout_width="300px"
- android:layout_height="wrap_content"
- android:singleLine="true"
- android:hint="请输入姓名"
- />
-
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:height="50px"
- android:text=" 年 龄 : "
- />
- <EditText
- android:id="@+id/et_age"
- android:layout_width="300px"
- android:layout_height="wrap_content"
- android:inputType="text"
- android:hint="请输入年龄"
- />
-
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:height="50px"
- android:text=" 专 业 : "
- />
- <RadioGroup
- android:id="@+id/radioGroup1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- >
- <RadioButton
- android:id="@+id/rb1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="计算机科学与技术"
- />
- <RadioButton
- android:id="@+id/rb2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="软件工程"
- />
- <RadioButton
- android:id="@+id/rb3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="网络工程"
- />
-
-
- </RadioGroup>
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:height="50px"
- android:text=" 学 历 : "
- />
- <Spinner
- android:id="@+id/edu"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:entries="@array/xueli" />
-
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:height="50px"
- android:text=" 爱 好 : "
- />
- <GridLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:columnCount="3"
- android:id="@+id/g1_hobby"
- >
- <CheckBox
- android:id="@+id/cb1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="阅读"
- />
- <CheckBox
- android:id="@+id/cb2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="旅游"
- />
- <CheckBox
- android:id="@+id/cb3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="发呆"
- />
- <CheckBox
- android:id="@+id/cb4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="唱歌"
- />
- <CheckBox
- android:id="@+id/cb5"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="编程"
- />
- <CheckBox
- android:id="@+id/cb6"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="运动"
- />
-
-
- </GridLayout>
-
- </LinearLayout>
-
-
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center">
- <Button
- android:id="@+id/bt_login"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="录入"
- android:onClick="onClickLogin"
- />
- <Button
- android:id="@+id/bt_reset"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="重置"
- android:onClick="onClickReset"
- />
- <Button
- android:id="@+id/bt_xianshi"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="显示"
-
- />
-
- </LinearLayout>
-
- <ListView
- android:id="@+id/lsv1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:divider="#f00"
- android:dividerHeight="2dp"
- android:layout_gravity="center"
-
- />
-
- </LinearLayout>

- <Te<?xml version="1.0" encoding="utf-8" ?>
- <!--2.模板-->
- <TextView xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:textSize="20sp"
- android:textColor="#009">
-
- </TextView>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string-array name="xueli">
- <item>高中</item>
- <item>本科</item>
- <item>硕士研究生</item>
- <item>博士研究生</item>
- <item>其它</item>
- </string-array>
-
- </resources>
- package com.example.test;
-
- import android.content.Context;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.GridLayout;
- import android.widget.ListView;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.Spinner;
- import android.view.View. OnClickListener;
-
-
- import java.util.ArrayList;
-
- public class MainActivity extends Activity {
-
- //声明
- private Context context;
-
- Spinner sp;
- //声明ListView对象
- ListView lsv;
-
- RadioGroup zy;
-
- EditText etname,etage;
-
- CheckBox cb1,cb2,cb3,cb4,cb5,cb6;
- CheckBox cb[]=new CheckBox[6];
-
- Button bt1,bt2,btxianshi;
- //复选按钮组
- GridLayout g1;
-
- String name,age,specialty,shobby,edu;
- //
- ArrayList<String> list=new ArrayList<String>();;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- context =this;
-
- //获取控件对象
- //登记按钮和重置按钮
- bt1 = (Button) findViewById(R.id.bt_login);
- bt2 = (Button) findViewById(R.id.bt_reset);
- btxianshi = (Button) findViewById(R.id.bt_xianshi);
- //复选框
- cb[0] = (CheckBox) findViewById(R.id.cb1);
- cb[1] = (CheckBox) findViewById(R.id.cb2);
- cb[2] = (CheckBox) findViewById(R.id.cb3);
- cb[3] = (CheckBox) findViewById(R.id.cb4);
- cb[4] = (CheckBox) findViewById(R.id.cb5);
- cb[5] = (CheckBox) findViewById(R.id.cb6);
-
- g1 = (GridLayout) findViewById(R.id.g1_hobby);
-
- etname = (EditText) findViewById(R.id.et_name);
- etage = (EditText) findViewById(R.id.et_age);
-
-
- zy = (RadioGroup) findViewById(R.id.radioGroup1);
-
- sp = (Spinner)findViewById(R.id.edu);
-
- lsv=(ListView)findViewById(R.id.lsv1);
-
-
-
- //登记信息
- bt1.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
-
- edu = sp.getSelectedItem().toString();
-
- shobby="";
- //
- for(int i=0;i<g1.getChildCount() ;i++) {
- CheckBox cbs = (CheckBox) g1.getChildAt(i);
- //判断状态
- if(cb[i].isChecked()) {
- shobby+=cbs.getText().toString()+" ";
- }
- }
-
- for(int i=0;i<zy.getChildCount();i++) {
- RadioButton r = (RadioButton) zy.getChildAt(i);
- if(r.isChecked()) {
- specialty = r.getText().toString();
- }
- }
-
-
- name = etname.getText().toString();
- age = etage.getText().toString();
-
- String s="\n姓名:"+name+"\n年龄:"+age+"\n专业:"+specialty+"\n学历:"+edu+"\n爱好:"+shobby;
-
- list.add(s);
-
-
-
-
-
- }
- });
-
-
-
-
- //重置
- bt2.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- //清空编辑框
- etname.setText("");
- etage.setText("");
- //消除单选按钮的被选中状态
- for(int i=0;i<zy.getChildCount();i++) {
- RadioButton r = (RadioButton) zy.getChildAt(i);
- if(r.isChecked()) {
- r.setChecked(false);
- }
- }
- //清除复选按钮的被选中状态
- for(int i=0;i<g1.getChildCount() ;i++) {
- CheckBox cbs = (CheckBox) g1.getChildAt(i);
- cbs.setChecked(false);
- }
-
- lsv.setAdapter(null);
- sp.setSelection(0);
-
- }
- });
-
- btxianshi.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- /**
- * 从list集合中读取数据显示在文本框中
- * 创建适配器对象
- */
- ArrayAdapter<String> adapter =
- new ArrayAdapter<String>(
- MainActivity.this,
- R.layout.list_item,
- list);
- //关联
- lsv.setAdapter(adapter);
-
- }
- });
-
-
-
- }
-
-
-
- // @Override
- // public boolean onCreateOptionsMenu(Menu menu) {
- // // Inflate the menu; this adds items to the action bar if it is present.
- // getMenuInflater().inflate(R.menu.main, menu);
- // return true;
- // }
-
- }

登记
显示
重置
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。