Android之注册界面练习

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android之注册界面练习

侠客小飞   2020-03-28 我要评论

今天要分享的是一个安卓注册小练习,记录一下自己的学习。

做一个注册页面。

要求填入用户如下信息:

  用户名、密码、确认密码、性别(单选)、爱好(多选,包括至少六个选项,如音乐、美术、阅读、篮球等)、email。

  当点击取消按钮,所有填过的内容(除了单选框之外)都清空,点击确认按钮,在下面一个文本框内显示上面的信息,如“XXX先生/女士(根据性别),您的密码是123456,您的爱好有音乐、篮球、美术,您的email是abc@163.com”。

1.activity_main.xml布局文件如下所示:

  1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2     xmlns:tools="http://schemas.android.com/tools"
  3     android:layout_width="match_parent"
  4     android:layout_height="match_parent"
  5     android:paddingBottom="@dimen/activity_vertical_margin"
  6     android:paddingLeft="@dimen/activity_horizontal_margin"
  7     android:paddingRight="@dimen/activity_horizontal_margin"
  8     android:paddingTop="@dimen/activity_vertical_margin"
  9     tools:context="com.example.demo3.MainActivity" >
 10     
 11     <TextView
 12        
 13         android:layout_width="wrap_content"
 14         android:layout_height="wrap_content"
 15         android:layout_marginTop="6dp"
 16         android:layout_marginLeft="80dp"
 17         android:textSize="25sp"
 18         android:text="注册页面" />
 19     
 20     <!--输入框-->
 21     <TextView
 22            android:id="@+id/EditText1"
 23         android:layout_width="wrap_content"
 24         android:layout_height="wrap_content"
 25         android:layout_marginTop="50dp"
 26         android:layout_marginLeft="0dp"
 27         android:textSize="20sp"
 28         android:text="用户名:" />
 29     <TextView
 30            android:id="@+id/EditText2"
 31         android:layout_width="wrap_content"
 32         android:layout_height="wrap_content"
 33         android:layout_marginTop="100dp"
 34         android:layout_marginLeft="0dp"
 35         android:textSize="20sp"
 36         android:text="密    码:" />
 37     <TextView
 38            android:id="@+id/EditText3"
 39         android:layout_width="wrap_content"
 40         android:layout_height="wrap_content"
 41         android:layout_marginTop="150dp"
 42         android:layout_marginLeft="0dp"
 43         android:textSize="20sp"
 44         android:text="密    码:" />
 45     
 46     <EditText
 47         android:id="@+id/username"
 48         android:layout_width="fill_parent"
 49         android:layout_height="wrap_content"
 50         android:layout_marginTop="50dp"
 51         android:layout_marginLeft="70dp"
 52         android:inputType="text"
 53         android:hint="请输入您的用户名"
 54         android:selectAllOnFocus="true"
 55         android:singleLine="true"
 56         android:textColorHint="#95A1AA" />
 57     <EditText
 58         android:id="@+id/password"
 59         android:layout_width="fill_parent"
 60         android:layout_height="wrap_content"
 61         android:layout_marginTop="100dp"
 62         android:layout_marginLeft="70dp"
 63         android:inputType="textPassword" 
 64         android:hint="请输入您的密码"
 65         android:selectAllOnFocus="true"
 66         android:textColorHint="#95A1AA" />
 67     <EditText
 68         android:id="@+id/password2"
 69         android:layout_width="fill_parent"
 70         android:layout_height="wrap_content"
 71         android:layout_marginTop="150dp"
 72         android:layout_marginLeft="70dp"
 73         android:inputType="textPassword" 
 74         android:hint="请再次输入您的密码"
 75         android:selectAllOnFocus="true"
 76         android:textColorHint="#95A1AA" />
 77    
 78       <!-- 性别 -->
 79       <TextView
 80             android:id="@+id/rb_name_tv"
 81             android:layout_width="wrap_content"
 82             android:layout_height="wrap_content"
 83             android:layout_marginTop="200dp"
 84             android:text="性    别:"
 85             android:textSize="20sp" />
 86         
 87         <!-- 第一个内嵌布局中的第三个控件为RadioGroup,用于包裹一组单选按钮 -->
 88         <RadioGroup
 89             android:id="@+id/radio_group"
 90             android:layout_width="fill_parent"
 91             android:layout_height="fill_parent"
 92             android:layout_marginLeft="70dp"
 93             android:layout_marginTop="200dp"
 94            
 95             android:orientation="horizontal">
 96             <!-- RadioGroup中第一个单选按钮,用于显示单选框 -->
 97             <RadioButton
 98                 android:id="@+id/rb1"
 99                 android:layout_width="wrap_content"
100                 android:layout_height="wrap_content"
101                 android:text="男"
102                 android:textSize="18sp" >
103             </RadioButton>
104             <!-- RadioGroup中第二个单选按钮,用于显示单选框 -->
105             <RadioButton
106                 android:id="@+id/rb2"
107                 android:layout_width="wrap_content"
108                 android:layout_height="wrap_content"
109                 android:text="女"
110                 android:textSize="18sp" >
111             </RadioButton>
112         </RadioGroup>
113         <!-- 爱好 -->
114         <!-- 第二个内嵌布局中的第一个控件为TextView,用于显示文本信息 -->
115         <TextView
116             android:id="@+id/cb_name_tv"
117             android:layout_width="wrap_content"
118             android:layout_height="wrap_content"
119             android:layout_marginTop="250dp"
120             android:text="爱    好:"
121             android:textSize="20sp" />
122         
123     
124     <!-- 第二个内嵌布局中的第三个控件为CheckBox,用于显示复选框 -->
125         <CheckBox
126             android:id="@+id/check_box1"
127             android:layout_width="wrap_content"
128             android:layout_height="wrap_content"
129             android:layout_marginTop="250dp"
130             android:layout_marginLeft="0dp"
131             android:layout_toRightOf="@+id/cb_name_tv"
132             android:text="篮球"
133             android:textSize="18sp" />
134         <CheckBox
135             android:id="@+id/check_box2"
136             android:layout_width="wrap_content"
137             android:layout_height="wrap_content"
138             android:layout_alignBaseline="@+id/check_box1"
139             android:layout_marginLeft="0dp"
140             android:layout_toRightOf="@+id/check_box1"
141             android:text="音乐"
142             android:textSize="18sp" />
143         
144         <CheckBox
145             android:id="@+id/check_box3"
146             android:layout_width="wrap_content"
147             android:layout_height="wrap_content"
148             android:layout_alignBaseline="@+id/check_box2"
149             android:layout_marginLeft="0dp"
150             android:layout_toRightOf="@+id/check_box2"
151             android:text="游戏"
152             android:textSize="18sp" />
153         <CheckBox
154             android:id="@+id/check_box4"
155             android:layout_width="wrap_content"
156             android:layout_height="wrap_content"
157             android:layout_below="@+id/check_box1"
158             android:layout_toRightOf="@+id/cb_name_tv"     
159             android:text="编程"
160             android:textSize="18sp" />
161        <CheckBox
162             android:id="@+id/check_box5"
163             android:layout_width="wrap_content"
164             android:layout_height="wrap_content"
165             android:layout_alignBaseline="@+id/check_box4"
166             android:layout_below="@+id/check_box2"
167             android:layout_toRightOf="@+id/check_box4"  
168             android:layout_marginLeft="0dp"
169             android:text="看书"
170             android:textSize="18sp" />
171        <CheckBox
172             android:id="@+id/check_box6"
173             android:layout_width="wrap_content"
174             android:layout_height="wrap_content"
175             android:layout_alignBaseline="@+id/check_box4"
176             android:layout_below="@+id/check_box2"
177             android:layout_toRightOf="@+id/check_box5"  
178             android:layout_marginLeft="0dp"
179             android:text="吃饭"
180             android:textSize="18sp" />
181        
182        <TextView
183            android:id="@+id/EditText3"
184         android:layout_width="wrap_content"
185         android:layout_height="wrap_content"
186         android:layout_marginTop="320dp"
187         android:layout_marginLeft="0dp"
188         android:textSize="20sp"
189         android:text="邮    箱:" />
190        <EditText
191         android:id="@+id/Email"
192         android:layout_width="fill_parent"
193         android:layout_height="wrap_content"
194         android:layout_marginTop="320dp"
195         android:layout_marginLeft="70dp"
196         android:inputType="text"
197         android:hint="如mwf1998@qq.com"
198         android:selectAllOnFocus="true"
199         android:singleLine="true"
200         android:textColorHint="#95A1AA" />
201        
202        
203        <Button
204         android:id="@+id/btnOk"
205         android:layout_width="wrap_content"
206         android:layout_marginTop="390dp"
207         android:layout_marginLeft="80dp"
208         android:layout_height="40dp"
209         android:padding="5dp"
210         android:text="OK"
211         android:onClick="pressOk"/>
212           
213       <Button
214         android:id="@+id/btnCancel"
215         android:layout_width="wrap_content"
216         android:layout_marginTop="390dp"
217         android:layout_marginLeft="200dp"
218         android:layout_height="40dp"
219         android:padding="5dp"
220         android:text="Cancel"
221         android:onClick="pressCancel"/>
222       
223       <!-- TextView,用于显示文本信息 -->
224         <TextView
225             android:id="@+id/tb_tv"
226             android:layout_width="wrap_content"
227             android:layout_height="wrap_content"
228             android:layout_marginTop="430dp"
229             android:layout_marginLeft="10dp"
230             android:text=""
231             android:textSize="25sp" />
232     
233  
234    
235 </RelativeLayout>
View Code

2.Mainactivity.java代码如下所示:

  1 package com.example.android_text8;
  2 
  3 import android.os.Bundle;
  4 
  5 import java.util.ArrayList;
  6 import java.util.List;
  7 
  8 
  9 import android.app.Activity;
 10 import android.view.Menu;
 11 import android.view.View;
 12 import android.view.View.OnClickListener;
 13 import android.widget.Button;
 14 import android.widget.CheckBox;
 15 import android.widget.CompoundButton;
 16 import android.widget.EditText;
 17 import android.widget.RadioButton;
 18 import android.widget.TextView;
 19 import android.widget.Toast;
 20 
 21 public class MainActivity extends Activity {
 22     private EditText username,password,password2,Email;
 23     String user = null;
 24     String pwd = null;
 25     String mail = null;
 26     String pwd2 = null;
 27     private Button ok =null;
 28     private Button cancel=null;
 29     private RadioButton rb1;
 30     private RadioButton rb2;
 31     private CheckBox CheckBox1;
 32     private CheckBox CheckBox2;
 33     private CheckBox CheckBox3;
 34     private CheckBox CheckBox4;
 35     private CheckBox CheckBox5;
 36     private CheckBox CheckBox6;
 37     private TextView tb_tv;
 38     private List<RadioButton> radioButtonList = new ArrayList<RadioButton>();
 39     private List<CheckBox> checkBoxList = new ArrayList<CheckBox>();
 40     @Override
 41     protected void onCreate(Bundle savedInstanceState) {
 42         super.onCreate(savedInstanceState);
 43         setContentView(R.layout.activity_main);
 44         initViews();//初始化变量
 45         
 46     }
 47 
 48     private void initViews() {
 49     
 50         rb1 = (RadioButton) findViewById(R.id.rb1);
 51         rb2 = (RadioButton) findViewById(R.id.rb2);
 52         
 53         
 54         CheckBox1 = (CheckBox) findViewById(R.id.check_box1);
 55         CheckBox2 = (CheckBox) findViewById(R.id.check_box2);
 56         CheckBox3 = (CheckBox) findViewById(R.id.check_box3);
 57         CheckBox4 = (CheckBox) findViewById(R.id.check_box4);
 58         CheckBox5 = (CheckBox) findViewById(R.id.check_box5);
 59         CheckBox6 = (CheckBox) findViewById(R.id.check_box6);
 60         
 61         tb_tv = (TextView) findViewById(R.id.tb_tv);
 62         
 63         radioButtonList.add(rb1);
 64         radioButtonList.add(rb2);
 65         
 66         checkBoxList.add(CheckBox1);
 67         checkBoxList.add(CheckBox2);
 68         checkBoxList.add(CheckBox3);
 69         checkBoxList.add(CheckBox4);
 70         checkBoxList.add(CheckBox5);
 71         checkBoxList.add(CheckBox6);
 72         
 73         ok = (Button) findViewById(R.id.btnOk);
 74         cancel = (Button) findViewById(R.id.btnCancel);
 75     
 76     
 77         
 78         //OK点击事件监听;
 79         ok.setOnClickListener(new OnClickListener() {
 80     
 81             public void onClick(View v) {
 82                 username = (EditText) findViewById(R.id.username);
 83                 user = username.getText().toString();
 84                 password = (EditText) findViewById(R.id.password);
 85                 pwd  = password.getText().toString();
 86                 password2 = (EditText) findViewById(R.id.password2);
 87                 pwd2  = password2.getText().toString();
 88                 Email = (EditText) findViewById(R.id.Email);
 89                 mail  = Email.getText().toString();
 90                 
 91             
 92                 // TODO Auto-generated method stub
 93                 
 94                 
 95                 StringBuffer cb = new StringBuffer();
 96                 StringBuffer rb = new StringBuffer();
 97                 
 98                 for (RadioButton radioButton : radioButtonList) {
 99                     if (radioButton.isChecked()) {
100                         rb.append(radioButton.getText().toString() + "");
101                     }
102                 }
103                 for (CheckBox checkbox : checkBoxList) {   
104                     if (checkbox.isChecked()) {
105                         cb.append(checkbox.getText().toString() + "、");
106                     }
107                 }
108                 
109                 
110                 
111                 if ((user == null) && (pwd!= pwd2) && cb.toString() == null ) {
112                     Toast.makeText(getApplicationContext(), "注册失败!输入的密码不一致或者您没有勾选爱好!", 3000).show();
113                 } else {
114                       
115                     tb_tv.setText("        "+user+"先生/女士"+",您的密码是:"+pwd+ ",您的性别是:"+rb.toString()+",您的爱好是:"+cb.toString()+",您的Email邮箱是:"+mail);
116                 }
117                 Toast.makeText(getApplicationContext(), "注册成功", 3000).show();
118 
119                 
120             }
121         });
122         
123         //cancel点击事件监听;
124         cancel.setOnClickListener(new OnClickListener() {
125             
126             public void onClick(View v) {
127                     username.setText("");
128                     password.setText("");
129                     password2.setText("");
130                     Email.setText("");
131                     
132                     
133                             
134                        
135 
136                     Toast.makeText(getApplicationContext(), "清除用户信息", 2000).show();
137                     
138         
139             }
140         });
141     }
142 
143     
144 
145     @Override
146     public boolean onCreateOptionsMenu(Menu menu) {
147         // Inflate the menu; this adds items to the action bar if it is present.
148         getMenuInflater().inflate(R.menu.main, menu);
149         return true;
150     }
151     
152 }    
View Code

3.完成效果图

PS:如果有做的不好的地方,欢迎交流指正。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们