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 ...
随机推荐
- List、Set、数组之间的转换
数组转Collection 使用Apache Jakarta Commons Collections: import org.apache.commons.collections.Collection ...
- 3名程序员被抓!开发“万能钥匙”APP,撬走3个亿
来自:程序员头条 报道 又有 3 名程序员被抓!开发"万能钥匙"APP,撬走 3 亿! 前几天,据央视新闻报道,上海公安机关接到共享单车企业报案,随后破获了一起共享单车万能解锁 A ...
- 用Python搭建简单的HTTP服务 · Zhangxu's Blog
分享一个快速用Python搭建简单的HTTP服务的方法. 平时我们可能有需要,传输某个文件到手机,或者工作中某台服务器的电脑. 假如这个手机是个测试手机/服务器,并没有微信QQ之类的软件,而且你也不想 ...
- AI超越人类大脑,或许是场“别有用心者”的骗局
谷歌.微软.苹果.特斯拉.百度.腾讯.阿里等互联网巨头企业,以及纳德拉.马斯克.扎克伯格.马云等互联网大佬,近年来一直都对人工智能--AI非常上心.在众多场合对AI给予了或肯定,或恐惧的评价.但无 ...
- py基础之有序列表
L =['adam',95.5,'lisa',85,'bart','bart',59]print (L)#list是一种有序的列表,可以使用索引访问每个list中的值print (L[1])#list ...
- getUserMedia API及HTML5 调用手机摄像头拍照
getUserMedia API简介 HTML5的getUserMedia API为用户提供访问硬件设备媒体(摄像头.视频.音频.地理位置等)的接口,基于该接口,开发者可以在不依赖任何浏览器插件的条件 ...
- js 打开新窗口方式
之前的项目,有个功能是下载文件,这里只要在浏览器输入 url 就会下载那个文件了.当时我只是简单得使用 window.open ,但是却会被浏览器进行拦截,要手动开启才行,然后就搜索研究其他方法,就看 ...
- crypto-js aes加密解密
安装 npm install crypto-js --save unit.js import CryptoJS from "crypto-js"; //秘钥 const CRYPT ...
- 【Geek议题】当年那些风骚的跨域操作
前言 现在cross-origin resource sharing(跨域资源共享,下简称CORS)已经十分普及,算上IE8的不标准兼容(XDomainRequest),各大浏览器基本都已支持,当年为 ...
- Codeforces Round #620 (Div. 2)
Codeforces Round #620 (Div. 2) A. Two Rabbits 题意 两只兔子相向而跳,一只一次跳距离a,另一只一次跳距离b,每次同时跳,问是否可能到同一位置 题解 每次跳 ...