登陆界面综合实例+spinner下拉列表框详解
点击确定:
点击修改:
想到Spinner下拉按钮可以通过两种方法生成
方法一:
1、在array数组里面定义数组
代码如下:
<resources>
<st
<?xml version="1.0" encoding="utf-8"?>
ring-array name="balls">
<item >篮球</item>
<item >足球</item>
<item >网球</item>
<item >排球</item>
</string-array>
<string-array name="citys">
<item >北京</item>
<item >上海</item>
<item >大连</item>
<item >合肥</item>
<item >安徽</item>
</string-array>
</resources>
2、在spinner布局下,将数组应用过来
代码如下:
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/city"
android:entries="@array/citys"
android:prompt="@string/city" />
3、再在.java中定义声明,然后用其本身的方法获得其值
代码如下:
private Spinner citySpinner;
citySpinner = (Spinner)findViewById(R.id.spinner);
//从spinner 下拉框中获取选择的值
protected String getCitys() {
// TODO Auto-generated method stub
return citySpinner.getSelectedItem().toString();
}
方法二:
1、在Spinner的布局文件里面先定义,但不要有值。
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/city"
android:id="@+id/cityItems">
</Spinner>
2、定义数组和Spinner
private static final String[] cities = { "北京", "上海", "广州", "郑州" };
private Spinner cityItems;
3、将定义的Spinne与布局文件中的id挂钩
cityItems = (Spinner) findViewById(R.id.cityItems);
4、创建一个数组型适配器并将cities中的数据与系统默认的spinner布局文件中的元素挂钩
ArrayAdapter<String> adpter = new ArrayAdapter<String>(
LoginActivity.this,android.R.layout.simple_spinner_item,cities);
5、将此适配器adpter设置为setdropdownviewresource的模式
(Sets the layout resource to create the drop down views)
adpter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
6、将文件中定义的Spinner与适配器建立联系
cityItems.setAdapter(adpter);
private String[] cityItems;
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.cityItems);
cityItems = getResources().getStringArray(R.array.citys);
ArrayAdapter< String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,cityItems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Toast.makeText(MainActivity.this, "您选择的是"+ spinner.getSelectedItem().toString(), 2000).show();
废话不多说,直接贴源码
String.xml当中的源码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Login</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="user">用户名:</string>
<string name="secret">密码:</string>
<string name="age">年龄:</string>
<string name="sex">性别:</string>
<string name="hobby">喜好:</string>
<string name="woman">女</string>
<string name="man">男</string>
<string name="city">城市:</string>
<string name="football">足球</string>
<string name="basketball">篮球</string>
<string name="pingpong">乒乓球</string>
<string name="ski">滑冰</string>
<string name="submit">注册</string>
</resources>
Arrays.xml当中的源码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="citys">
<item >北京</item>
<item >上海</item>
<item >大连</item>
<item >合肥</item>
<item >安徽</item>
</string-array>
</resources>
Dimen.xml当中的源码:(主要用于规定字符的大小,文本框的尺寸等)
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fontsize">30px</dimen>
<dimen name="TextViewWidth">120px</dimen>
<dimen name="EditTextWidth">220px</dimen>
</resources>
main.xml当中的布局文件
1: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2: xmlns:tools="http://schemas.android.com/tools"
3: android:layout_width="fill_parent"
4: android:layout_height="fill_parent"
5: android:orientation="vertical"
6: tools:context=".MainActivity" >
7:
8: <RelativeLayout
9: android:layout_width="fill_parent"
10: android:layout_height="wrap_content"
11: >
12: <TextView
13: android:id="@+id/name"
14: android:layout_width="@dimen/TextViewWidth"
15: android:layout_height="wrap_content"
16: android:layout_alignParentLeft="true"
17: android:layout_centerVertical="true"
18: android:text="@string/user"
19: android:textSize="@dimen/fontsize" />
20: <EditText
21: android:layout_width="@dimen/EditTextWidth"
22: android:layout_height="wrap_content"
23: android:layout_alignParentRight="true"
24: android:layout_alignTop="@id/name"
25: android:hint="zhuxuekui"
26: android:id="@+id/nameValue"
27: />
28: </RelativeLayout>
29:
30:
31:
32: <RelativeLayout
33: android:layout_width="fill_parent"
34: android:layout_height="wrap_content"
35: >
36: <TextView
37: android:id="@+id/secret1"
38: android:layout_width="@dimen/TextViewWidth"
39: android:layout_height="wrap_content"
40: android:layout_alignParentLeft="true"
41: android:layout_centerVertical="true"
42: android:text="@string/secret"
43: android:textSize="@dimen/fontsize" />
44:
45: <EditText
46: android:id="@+id/secret"
47: android:layout_width="@dimen/EditTextWidth"
48: android:layout_height="wrap_content"
49: android:layout_alignParentRight="true"
50: android:layout_alignParentTop="true"
51: android:ems="10"
52: android:hint="123456" >
53: </EditText>
54: </RelativeLayout>
55:
56:
57: <RelativeLayout
58: android:layout_width="fill_parent"
59: android:layout_height="wrap_content"
60: >
61: <TextView
62: android:id="@+id/age1"
63: android:layout_width="@dimen/TextViewWidth"
64: android:layout_height="wrap_content"
65: android:layout_alignParentLeft="true"
66: android:layout_centerVertical="true"
67: android:text="@string/age"
68: android:textSize="@dimen/fontsize" />
69:
70: <EditText
71: android:id="@+id/age"
72: android:layout_width="@dimen/EditTextWidth"
73: android:layout_height="wrap_content"
74: android:layout_alignParentRight="true"
75: android:layout_alignParentTop="true"
76: android:ems="10"
77: android:hint="20 ">
78: </EditText>
79: </RelativeLayout>
80:
81: <RelativeLayout
82: android:layout_width="238dp"
83: android:layout_height="wrap_content" >
84: <TextView
85: android:id="@+id/sex"
86: android:layout_width="@dimen/TextViewWidth"
87: android:layout_height="wrap_content"
88: android:layout_alignParentLeft="true"
89: android:layout_alignParentTop="true"
90: android:layout_marginTop="17dp"
91: android:text="@string/sex"
92: android:textSize="@dimen/fontsize" />
93: <RadioGroup
94: android:id="@+id/sex2"
95: android:layout_width="wrap_content"
96: android:layout_height="wrap_content"
97: android:layout_toRightOf="@id/sex"
98: android:checkedButton="@+id/woman"
99: android:orientation="horizontal" >
100:
101: <RadioButton
102: android:text="@string/man"
103: android:id="@+id/man"
104: />
105: <RadioButton
106: android:text="@string/woman"
107: android:id="@+id/woman"
108: />
109: </RadioGroup>
110: </RelativeLayout>
111:
112:
113: <RelativeLayout
114: android:layout_width="fill_parent"
115: android:layout_height="wrap_content"
116: >
117: <TextView
118: android:id="@+id/city"
119: android:layout_width="@dimen/TextViewWidth"
120: android:layout_height="wrap_content"
121: android:layout_alignParentLeft="true"
122: android:layout_centerVertical="true"
123: android:text="@string/city"
124: android:textSize="@dimen/fontsize" />
125:
126: <Spinner
127: android:id="@+id/spinner"
128: android:layout_width="wrap_content"
129: android:layout_height="wrap_content"
130: android:layout_toRightOf="@+id/city"
131: android:entries="@array/citys"
132: android:prompt="@string/city" />
133:
134: </RelativeLayout>
135:
136:
137: <RelativeLayout
138: android:layout_width="fill_parent"
139: android:layout_height="wrap_content"
140: >
141: <TextView
142: android:id="@+id/hobby"
143: android:layout_width="@dimen/TextViewWidth"
144: android:layout_height="wrap_content"
145: android:layout_alignParentLeft="true"
146: android:layout_centerVertical="true"
147: android:text="@string/hobby"
148: android:textSize="@dimen/fontsize" />
149:
150: <CheckBox
151: android:id="@+id/football"
152: android:layout_width="wrap_content"
153: android:layout_height="wrap_content"
154: android:layout_alignParentTop="true"
155: android:layout_toRightOf="@+id/hobby"
156: android:text="@string/football" />
157:
158:
159: <CheckBox
160: android:id="@+id/pingpong"
161: android:layout_width="wrap_content"
162: android:layout_height="wrap_content"
163: android:layout_alignLeft="@+id/football"
164: android:layout_below="@+id/football"
165: android:text="@string/pingpong" />
166:
167:
168:
169: <CheckBox
170: android:id="@+id/basketball"
171: android:layout_width="wrap_content"
172: android:layout_height="wrap_content"
173: android:layout_alignParentTop="true"
174: android:layout_toRightOf="@+id/pingpong"
175: android:text="@string/basketball" />
176:
177: <CheckBox
178: android:id="@+id/ski"
179: android:layout_width="wrap_content"
180: android:layout_height="wrap_content"
181: android:layout_below="@id/basketball"
182: android:layout_toRightOf="@+id/pingpong"
183: android:text="@string/ski" />
184:
185: </RelativeLayout>
186: <Button
187: android:layout_width="wrap_content"
188: android:layout_height="wrap_content"
189: android:id="@+id/submit"
190: android:text="@string/submit"
191: android:textSize="@dimen/fontsize"
192:
193: />
194:
195:
196: </LinearLayout>
MainAcitivity.java当中的源码:
1: package com.example.login;
2:
3: import java.util.ArrayList;
4: import java.util.List;
5:
6: import android.R.string;
7: import android.os.Bundle;
8: import android.app.Activity;
9: import android.app.AlertDialog;
10: import android.app.ProgressDialog;
11: import android.content.DialogInterface;
12: import android.view.Menu;
13: import android.view.View;
14: import android.view.ViewGroup;
15: import android.widget.ArrayAdapter;
16: import android.widget.Button;
17: import android.widget.CheckBox;
18: import android.widget.EditText;
19: import android.widget.RadioButton;
20: import android.widget.RadioGroup;
21: import android.widget.Spinner;
22:
23: public class MainActivity extends Activity {
24:
25: private EditText name,age,pass;
26: private Button regButton;
27: private RadioGroup sexRadioGroup;
28: private CheckBox basketball,pingpong,football,ski;
29: private Spinner citySpinner;
30: private List<CheckBox> myhobbysBoxs;
31: // private String[] cityarray;
32:
33: private boolean flag = true;
34:
35: @Override
36: protected void onCreate(Bundle savedInstanceState) {
37: super.onCreate(savedInstanceState);
38: setContentView(R.layout.activity_main);
39: //定义一个ArrayList( ) 用来存放所有的checkbox
40: myhobbysBoxs = new ArrayList<CheckBox>();
41:
42: //得到相应的显示控件的对象
43: name = (EditText)findViewById(R.id.nameValue);
44: age = (EditText)findViewById(R.id.age);
45: pass = (EditText)findViewById(R.id.secret);
46: regButton = (Button)findViewById(R.id.submit);
47:
48: //获取单选框的控件,注意一小点,静态的文件与输入的文本框的id不可以重复
49: sexRadioGroup = (RadioGroup)findViewById(R.id.sex2);
50:
51: //获取多选框的的id并将他们当中的每一个小项放入List<CheckBox> 这样的checkbox类型的List容器中。
52: basketball = (CheckBox)findViewById(R.id.basketball);
53: myhobbysBoxs.add(basketball);
54: pingpong = (CheckBox)findViewById(R.id.pingpong);
55: myhobbysBoxs.add(pingpong);
56: football = (CheckBox)findViewById(R.id.football);
57: myhobbysBoxs.add(football);
58: ski = (CheckBox)findViewById(R.id.ski);
59: myhobbysBoxs.add(ski);
60:
61: //获取spinner中的id
62: citySpinner = (Spinner)findViewById(R.id.spinner);
63: //这是第二种 在.java中显示spinner布局框中的项方法
64: //cityarray = getResources().getStringArray(R.array.citys);
65:
66: //ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, cityarray);
67:
68: //创建按钮监听事件
69: regButton.setOnClickListener(new View.OnClickListener() {
70: //AlertDialog警告对话框,我们可以设置对话框的
71: @Override
72: public void onClick(View v) {
73: // TODO Auto-generated method stub
74: flag = addUser();
75: if(flag)
76: {
77: //创建一个 AlertDialog 类的内部类 Builder 的对象
78: AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
79: //为 builer 设置标题
80: builder.setTitle("请确认信息")
81: //为 builer 设置消息内容
82: .setMessage("您的信息如下:"+ "\n"
83: + "姓名:"+ name.getText().toString() + "\n" + "年龄:" + age.getText().toString() + "\n" + "性别:" + getSex() + "\n"
84: + "爱好:" + getHobby() + "\n" + "城市:" + getCitys() + "\n")
85: //设置为 false 后,键盘上的后退键失效,默认为ture,也就是说必须按下警告键,后退键无效,所以设置为false
86: .setCancelable(false)
87: //添 加 确 定 按 钮 , 并 添 加 对 该 按 钮 的 处 理 事 件
88: .setPositiveButton("确定", new DialogInterface.OnClickListener() {
89:
90: @Override
91: public void onClick(DialogInterface dialog, int which) {
92: // TODO Auto-generated method stub
93: //进度条,有点像圆形进度条,progressbar。 后面那句说明,现在返回键有效。
94: ProgressDialog.show(MainActivity.this, "用户信息注册中", "请等待.......").setCancelable(true);
95: }
96: }
97: )
98: .setNegativeButton("修改", new DialogInterface.OnClickListener() {
99:
100: @Override
101: public void onClick(DialogInterface dialog, int which) {
102: // TODO Auto-generated method stub
103: dialog.cancel();//删除对话框
104: }
105: });
106: AlertDialog alertDialog = builder.show();//显示对话框
107: }
108:
109:
110: //创建一个AlertDialog对话框显示登录的信息,这个方法中他会有一个确认的过程,点击确定需要干嘛干嘛,点击取消需要干嘛干嘛
111: // new AlertDialog.Builder(MainActivity.this).setTitle("请确认信息").setMessage("您的信息如下:"+ "\n"
112: // + "姓名:"+ name.getText().toString() + "\n" + "年龄:" + age.getText().toString() + "\n" + "性别:" + getSex() + "\n"
113: // + "爱好:" + getHobby() + "\n" + "城市:" + getCitys() + "\n").setCancelable(false).setPositiveButton("确定", new DialogInterface.OnClickListener() {
114: //
115: // @Override
116: // public void onClick(DialogInterface dialog, int which) {
117: // // TODO Auto-generated method stub
118: // //进度条
119: // ProgressDialog.show(MainActivity.this, "用户信息注册中", "请等待.......").setCancelable(true);
120: // }
121: // }
122: // ).setNegativeButton("修改", new DialogInterface.OnClickListener() {
123: //
124: // @Override
125: // public void onClick(DialogInterface dialog, int which) {
126: // // TODO Auto-generated method stub
127: // dialog.cancel();//删除对话框
128: // }
129: // }).show(); //显示对话框
130: }
131:
132: });
133: }
134: //从spinner 下拉列表框中获取选择的值
135: protected String getCitys() {
136: // TODO Auto-generated method stub
137: return citySpinner.getSelectedItem().toString();
138: }
139: //从多选框中获取字符串并返回
140: protected String getHobby() {
141: // TODO Auto-generated method stub
142: //采用一直轮询机制,从1到结束。这种for结构很有趣,然后选择一个加”“。
143: String fasString = "";
144: for (CheckBox cBox : myhobbysBoxs) {
145: if(cBox.isChecked())
146: {
147: fasString += cBox.getText().toString();
148: fasString += ",";
149: }
150: }
151: //substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串。
152: if(fasString != "")
153: {
154: fasString = fasString.substring(0, fasString.length() - 1);
155: }
156: else {
157: fasString = "您没有选择爱好!";
158: }
159: return fasString;
160: }
161: //从单选框中获取字符串并返回
162: protected String getSex() {
163: // TODO Auto-generated method stub
164: RadioButton mButton = (RadioButton)findViewById(sexRadioGroup.getCheckedRadioButtonId());
165: return mButton.getText().toString();
166: }
167:
168: //验证输入是否合法
169: protected boolean addUser() {
170: // TODO Auto-generated method stub
171: if (name.getText().toString().length() == 0) {
172: name.setError("用户名不能为空"); //在view上的效果是,会出现!号。
173: return false;
174: }
175: if (age.getText().toString().length() == 0) {
176: age.setError("年龄不能为空");
177: return false;
178: }
179: if (pass.getText().toString().length() == 0) {
180: pass.setError("密码不能为空");
181: return false;
182: }
183: return true;
184: }
185:
186: @Override
187: public boolean onCreateOptionsMenu(Menu menu) {
188: // Inflate the menu; this adds items to the action bar if it is present.
189: getMenuInflater().inflate(R.menu.main, menu);
190: return true;
191: }
192:
193: }