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

做一个注册页面。

要求填入用户如下信息:

  用户名、密码、确认密码、性别(单选)、爱好(多选,包括至少六个选项,如音乐、美术、阅读、篮球等)、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之注册界面练习的更多相关文章

  1. Android的配置界面PreferenceActivity

    我想大家对于android的系统配置界面应该不会陌生吧,即便陌生,那么下面的界面应该似曾相识吧,假若还是不认识,那么也没有关系,我们这一节主要就是介绍并讲解android 中系统配置界面的使用,相信大 ...

  2. 家庭记账本app实现登录注册界面以及仿微信操作界面(共4个实现一个)遇到了麻烦

    今天学习了数据的创建,以及关于数据库的相关操作. 今天主要是实现了对于数据库的增加和查找. 具体的代码如下: 首先是数据库的创建: DBOpenMessage.java package com.exa ...

  3. 解决Android Graphical Layout 界面效果不显示

    解决Android Graphical Layout 界面效果不显示 qq463431476

  4. HTML登录注册界面怎么制作?

    在没有学习CSS样式的前提下,是如何做一个简单的注册界面的. 一.表单标签(form) 首先我们先写一个<form></form>的标签,form标签属于表单标签,通常我们的登 ...

  5. Android之拨号界面图片风格,无信息默认显示界面修改

    Android之拨号界面图片风格,无信息默认显示界面修改 点开Dialer app,出现拨号,联系人,收藏三个选项卡,也就是三个Fragment,在三个界面都没有信息的时候会显示一个时钟,联系人,收藏 ...

  6. iOS开发——UI进阶篇(八)pickerView简单使用,通过storyboard加载控制器,注册界面,通过xib创建控制器,控制器的view创建,导航控制器的基本使用

    一.pickerView简单使用 1.UIPickerViewDataSource 这两个方法必须实现 // 返回有多少列 - (NSInteger)numberOfComponentsInPicke ...

  7. HTML练习----注册界面

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. Android实现入门界面布局

    Android实现入门界面布局 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 代码实现 首先是常量的定义,安卓中固定字符串应该定义在常量中. stri ...

  9. tkinter 创建登陆注册界面

    import tkinter as tk from tkinter import messagebox #设置窗口居中 def window_info(): ws = window.winfo_scr ...

随机推荐

  1. angular知识点(2)

    angular知识点(2) 1.为了代码规范,对于需要自动加载的依赖,需要在前面加上注释,注释为://@ngInject 或者是/*@ngInject*/ 2.ngSwitch的应用 在需要用到选择出 ...

  2. MAC使用nginx分发80至8080端口

    由于项目必须要启动80端口,但是mac系统中非root用户无法直接使用1024以下的端口 2.释放apache的80端口 由于Mac OS是自带Apache服务的,它本身占用了80端口,首先你需要将A ...

  3. 查看网卡信息 - ethtool

    查看网卡是千兆还是万兆网卡,使用ethtool 网络接口名 ethtool eth0

  4. Think 框架漏洞利用

    下午有点闲,又没有女朋友陪,该干嘛呢??? 对了,做安全的是不需要女朋友的,哈哈哈哈哈 废话不多说,本机搭建环境: 首先nmap扫描一下,哦哈,有点东西. 开的端口有点多,这个时候有点窃喜,开的端口太 ...

  5. NS域名工作原理及解析

    DNS域名工作原理及解析   0x00 定义 DNS( Domain Name System)是“域名系统”的英文缩写,它作为将域名和IP地址相互映射的一个分布式数据库,能够使人更方便地访问互联网.D ...

  6. 7-43 jmu-python-字符串异常处理 (20 分)

    输入一行字符串及下标,能取出相应字符.程序能对不合法数据做相应异常处理. 输入格式: 行1:输入一字符串 行2:输入字符下标 输出格式: 下标非数值异常,输出下标要整数 下标越界,输出下标越界 数据正 ...

  7. SQL Server 最小日志记录

    SQL Server之所以记录事务日志,首要目的是为了把失败或取消的操作还原到最原始的状态,但是,并不是所有的操作都需要完全记录事务日志,比如,在一个空表上放置排他锁,把大量的数据插入到该空表中.即使 ...

  8. 【每日一包0018】fecha

    [github地址:https://github.com/ABCDdouyae...] fecha 比moment.js更加轻量级的时间解析和格式化包 format 用法:format(<Dat ...

  9. vue+express+mysql项目总结(node项目部署阿里云通用)

    原文发布于我的个人博客上:原文点这里   前面经历千辛万苦,终于把博客的所有东西都准备好了,现在就只等部署了.下面我介绍下我的部署过程: 一.购买服务器和域名   如果需要域名(不用域名通过ip也可以 ...

  10. ts文件的编译和运行

    hello.ts代码 function sayHello(person: string): string { return 'Hello, ' + person; } let user = 'Tom' ...