Android之注册界面练习
今天要分享的是一个安卓注册小练习,记录一下自己的学习。
做一个注册页面。
要求填入用户如下信息:
用户名、密码、确认密码、性别(单选)、爱好(多选,包括至少六个选项,如音乐、美术、阅读、篮球等)、email。
当点击取消按钮,所有填过的内容(除了单选框之外)都清空,点击确认按钮,在下面一个文本框内显示上面的信息,如“XXX先生/女士(根据性别),您的密码是123456,您的爱好有音乐、篮球、美术,您的email是abc@163.com”。
1.activity_main.xml布局文件如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.demo3.MainActivity" > <TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_marginLeft="80dp"
android:textSize="25sp"
android:text="注册页面" /> <!--输入框-->
<TextView
android:id="@+id/EditText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginLeft="0dp"
android:textSize="20sp"
android:text="用户名:" />
<TextView
android:id="@+id/EditText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginLeft="0dp"
android:textSize="20sp"
android:text="密 码:" />
<TextView
android:id="@+id/EditText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_marginLeft="0dp"
android:textSize="20sp"
android:text="密 码:" /> <EditText
android:id="@+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginLeft="70dp"
android:inputType="text"
android:hint="请输入您的用户名"
android:selectAllOnFocus="true"
android:singleLine="true"
android:textColorHint="#95A1AA" />
<EditText
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginLeft="70dp"
android:inputType="textPassword"
android:hint="请输入您的密码"
android:selectAllOnFocus="true"
android:textColorHint="#95A1AA" />
<EditText
android:id="@+id/password2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_marginLeft="70dp"
android:inputType="textPassword"
android:hint="请再次输入您的密码"
android:selectAllOnFocus="true"
android:textColorHint="#95A1AA" /> <!-- 性别 -->
<TextView
android:id="@+id/rb_name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:text="性 别:"
android:textSize="20sp" /> <!-- 第一个内嵌布局中的第三个控件为RadioGroup,用于包裹一组单选按钮 -->
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="70dp"
android:layout_marginTop="200dp" android:orientation="horizontal">
<!-- RadioGroup中第一个单选按钮,用于显示单选框 -->
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:textSize="18sp" >
</RadioButton>
<!-- RadioGroup中第二个单选按钮,用于显示单选框 -->
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
android:textSize="18sp" >
</RadioButton>
</RadioGroup>
<!-- 爱好 -->
<!-- 第二个内嵌布局中的第一个控件为TextView,用于显示文本信息 -->
<TextView
android:id="@+id/cb_name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:text="爱 好:"
android:textSize="20sp" /> <!-- 第二个内嵌布局中的第三个控件为CheckBox,用于显示复选框 -->
<CheckBox
android:id="@+id/check_box1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:layout_marginLeft="0dp"
android:layout_toRightOf="@+id/cb_name_tv"
android:text="篮球"
android:textSize="18sp" />
<CheckBox
android:id="@+id/check_box2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/check_box1"
android:layout_marginLeft="0dp"
android:layout_toRightOf="@+id/check_box1"
android:text="音乐"
android:textSize="18sp" /> <CheckBox
android:id="@+id/check_box3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/check_box2"
android:layout_marginLeft="0dp"
android:layout_toRightOf="@+id/check_box2"
android:text="游戏"
android:textSize="18sp" />
<CheckBox
android:id="@+id/check_box4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/check_box1"
android:layout_toRightOf="@+id/cb_name_tv"
android:text="编程"
android:textSize="18sp" />
<CheckBox
android:id="@+id/check_box5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/check_box4"
android:layout_below="@+id/check_box2"
android:layout_toRightOf="@+id/check_box4"
android:layout_marginLeft="0dp"
android:text="看书"
android:textSize="18sp" />
<CheckBox
android:id="@+id/check_box6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/check_box4"
android:layout_below="@+id/check_box2"
android:layout_toRightOf="@+id/check_box5"
android:layout_marginLeft="0dp"
android:text="吃饭"
android:textSize="18sp" /> <TextView
android:id="@+id/EditText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="320dp"
android:layout_marginLeft="0dp"
android:textSize="20sp"
android:text="邮 箱:" />
<EditText
android:id="@+id/Email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="320dp"
android:layout_marginLeft="70dp"
android:inputType="text"
android:hint="如mwf1998@qq.com"
android:selectAllOnFocus="true"
android:singleLine="true"
android:textColorHint="#95A1AA" /> <Button
android:id="@+id/btnOk"
android:layout_width="wrap_content"
android:layout_marginTop="390dp"
android:layout_marginLeft="80dp"
android:layout_height="40dp"
android:padding="5dp"
android:text="OK"
android:onClick="pressOk"/> <Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_marginTop="390dp"
android:layout_marginLeft="200dp"
android:layout_height="40dp"
android:padding="5dp"
android:text="Cancel"
android:onClick="pressCancel"/> <!-- TextView,用于显示文本信息 -->
<TextView
android:id="@+id/tb_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="430dp"
android:layout_marginLeft="10dp"
android:text=""
android:textSize="25sp" /> </RelativeLayout>
2.Mainactivity.java代码如下所示:
package com.example.android_text8; import android.os.Bundle; import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity {
private EditText username,password,password2,Email;
String user = null;
String pwd = null;
String mail = null;
String pwd2 = null;
private Button ok =null;
private Button cancel=null;
private RadioButton rb1;
private RadioButton rb2;
private CheckBox CheckBox1;
private CheckBox CheckBox2;
private CheckBox CheckBox3;
private CheckBox CheckBox4;
private CheckBox CheckBox5;
private CheckBox CheckBox6;
private TextView tb_tv;
private List<RadioButton> radioButtonList = new ArrayList<RadioButton>();
private List<CheckBox> checkBoxList = new ArrayList<CheckBox>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();//初始化变量 } private void initViews() { rb1 = (RadioButton) findViewById(R.id.rb1);
rb2 = (RadioButton) findViewById(R.id.rb2); CheckBox1 = (CheckBox) findViewById(R.id.check_box1);
CheckBox2 = (CheckBox) findViewById(R.id.check_box2);
CheckBox3 = (CheckBox) findViewById(R.id.check_box3);
CheckBox4 = (CheckBox) findViewById(R.id.check_box4);
CheckBox5 = (CheckBox) findViewById(R.id.check_box5);
CheckBox6 = (CheckBox) findViewById(R.id.check_box6); tb_tv = (TextView) findViewById(R.id.tb_tv); radioButtonList.add(rb1);
radioButtonList.add(rb2); checkBoxList.add(CheckBox1);
checkBoxList.add(CheckBox2);
checkBoxList.add(CheckBox3);
checkBoxList.add(CheckBox4);
checkBoxList.add(CheckBox5);
checkBoxList.add(CheckBox6); ok = (Button) findViewById(R.id.btnOk);
cancel = (Button) findViewById(R.id.btnCancel); //OK点击事件监听;
ok.setOnClickListener(new OnClickListener() { public void onClick(View v) {
username = (EditText) findViewById(R.id.username);
user = username.getText().toString();
password = (EditText) findViewById(R.id.password);
pwd = password.getText().toString();
password2 = (EditText) findViewById(R.id.password2);
pwd2 = password2.getText().toString();
Email = (EditText) findViewById(R.id.Email);
mail = Email.getText().toString(); // TODO Auto-generated method stub StringBuffer cb = new StringBuffer();
StringBuffer rb = new StringBuffer(); for (RadioButton radioButton : radioButtonList) {
if (radioButton.isChecked()) {
rb.append(radioButton.getText().toString() + "");
}
}
for (CheckBox checkbox : checkBoxList) {
if (checkbox.isChecked()) {
cb.append(checkbox.getText().toString() + "、");
}
} if ((user == null) && (pwd!= pwd2) && cb.toString() == null ) {
Toast.makeText(getApplicationContext(), "注册失败!输入的密码不一致或者您没有勾选爱好!", 3000).show();
} else { tb_tv.setText(" "+user+"先生/女士"+",您的密码是:"+pwd+ ",您的性别是:"+rb.toString()+",您的爱好是:"+cb.toString()+",您的Email邮箱是:"+mail);
}
Toast.makeText(getApplicationContext(), "注册成功", 3000).show(); }
}); //cancel点击事件监听;
cancel.setOnClickListener(new OnClickListener() { public void onClick(View v) {
username.setText("");
password.setText("");
password2.setText("");
Email.setText(""); Toast.makeText(getApplicationContext(), "清除用户信息", 2000).show(); }
});
} @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;
} }
3.完成效果图

PS:如果有做的不好的地方,欢迎交流指正。
Android之注册界面练习的更多相关文章
- Android的配置界面PreferenceActivity
我想大家对于android的系统配置界面应该不会陌生吧,即便陌生,那么下面的界面应该似曾相识吧,假若还是不认识,那么也没有关系,我们这一节主要就是介绍并讲解android 中系统配置界面的使用,相信大 ...
- 家庭记账本app实现登录注册界面以及仿微信操作界面(共4个实现一个)遇到了麻烦
今天学习了数据的创建,以及关于数据库的相关操作. 今天主要是实现了对于数据库的增加和查找. 具体的代码如下: 首先是数据库的创建: DBOpenMessage.java package com.exa ...
- 解决Android Graphical Layout 界面效果不显示
解决Android Graphical Layout 界面效果不显示 qq463431476
- HTML登录注册界面怎么制作?
在没有学习CSS样式的前提下,是如何做一个简单的注册界面的. 一.表单标签(form) 首先我们先写一个<form></form>的标签,form标签属于表单标签,通常我们的登 ...
- Android之拨号界面图片风格,无信息默认显示界面修改
Android之拨号界面图片风格,无信息默认显示界面修改 点开Dialer app,出现拨号,联系人,收藏三个选项卡,也就是三个Fragment,在三个界面都没有信息的时候会显示一个时钟,联系人,收藏 ...
- iOS开发——UI进阶篇(八)pickerView简单使用,通过storyboard加载控制器,注册界面,通过xib创建控制器,控制器的view创建,导航控制器的基本使用
一.pickerView简单使用 1.UIPickerViewDataSource 这两个方法必须实现 // 返回有多少列 - (NSInteger)numberOfComponentsInPicke ...
- HTML练习----注册界面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Android实现入门界面布局
Android实现入门界面布局 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 代码实现 首先是常量的定义,安卓中固定字符串应该定义在常量中. stri ...
- tkinter 创建登陆注册界面
import tkinter as tk from tkinter import messagebox #设置窗口居中 def window_info(): ws = window.winfo_scr ...
随机推荐
- 手术Robot能取代医生吗?
现在,机械自动化已经成为各领域为之神往的大趋势,从工业组装,到智能物流,再到餐饮.银行服务,以及娱乐等等,管理者无不处心积虑地降低成本.提高效率,其中,一个非常重要的手段就是利用机器取代人工.医院,作 ...
- 二十一世纪计算 | John Hopcroft:AI革命
编者按:信息革命的浪潮浩浩汤汤,越来越多的人将注意力转向人工智能,想探索它对人类生产生活所产生的可能影响.人工智能的下一步发展将主要来自深度学习,在这个领域中,更多令人兴奋的话题在等待我们探讨:神经网 ...
- docker mysql5.7.16 中文乱码
有部分同学会遇到,在centos上Docker-MySQL没乱码,但是在fedora系统上的docker-mysql会有乱码问题,这兴许是docker-mysql的问题,这里的bug我们不去追究,这里 ...
- B站实战第三天
B站实战第三天 用了两天多的时间才把B站页面的头部写完,今天来写头部下面的导航栏部分和轮播图一些模块. 因为还没学js,轮播图部分用swiper来实现. 今天首先复习的知识点是弹性盒模型. 弹性盒模型 ...
- Linux下多线程复制文件(C)
Linux下实现多线程文件复制,使用<pthread.h>提供的函数: int pthread_create(pthread_t *thread,const pthread_attr_t ...
- 一个很粗糙的XXXX
改dnsrecon的代码改来改去都获取不到想要的结果,也不知道是不是py中的正则和PHP的有神马不一样的地方,但是用RegexBuddy测的时候是正确的,想不通啊想不通.果断不改了,自己动手PHP ...
- ThinkPHP判断更新是否成功的正确方法
如何判断一个更新操作是否成功 $Model = D('Blog'); $data['id'] = 10; $data['name'] = 'update name'; $result = $Model ...
- 使用BIND搭建内部DNS服务
...
- c语言之单向链表
0x00 什么是链表 链表可以说是一种最为基础的数据结构了,而单向链表更是基础中的基础.链表是由一组元素以特定的顺序组合或链接在一起的,不同元素之间在逻辑上相邻,但是在物理上并不一定相邻.在维护一组数 ...
- direction和writing-mode的介绍
direction介绍 属性值和兼容都很好 CSSdirection属性简单好记,属性值少,兼容性好,关键时候省心省力,是时候给大家宣传宣传,不要埋没了人家的特殊技能. Chrome Safari F ...