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

做一个注册页面。

要求填入用户如下信息:

  用户名、密码、确认密码、性别(单选)、爱好(多选,包括至少六个选项,如音乐、美术、阅读、篮球等)、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. connect() failed (111: Connection refused) while connecting to upstream报错处理

    新lnmp环境调试项目时,nginx报错如下: 解决: 发现php-fpm.conf是以套接字方式通信,而nginx是以端口方式通信,见下图: 将nginx.conf修改为如下,重新reload即可

  2. sycPHPCMS v1.6 cookie sqlinjection

    ./user/index.php include "../include/conn.php"; include "../include/function.php" ...

  3. 设计模式详解及PHP实现:代理模式

    [目录] 代理模式(Proxy pattern) 代理模式是一种结构型模式,它可以为其他对象提供一种代理以控制对这个对象的访问. 主要角色 抽象主题角色(Subject):它的作用是统一接口.此角色定 ...

  4. SQL中 decode()函数简介

    SQL中 decode()函数简介 今天看别人的SQL时看这里面还有decode()函数,以前从来没接触到,上网查了一下,还挺好用的一个函数,写下来希望对朋友们有帮助哈! decode()函数简介: ...

  5. Ftp Centos · GitBook

    これよくない pyftpdlibをつかおう sudo easy_install pyftpdlib nohup python -m pyftpdlib > pyftpdlib.log 2> ...

  6. 苹果为何要一定要去印度生产iPhone

    ​ 现在,关于苹果手机有几种流行的猜想和期待,今年恰逢iPhone问世十周年,新产品估计会有颠覆性创新,消费者正望穿秋水,翘首企盼,但只需待到金秋便可知晓,何况iPhone8或许也就是一小撮发烧友的选 ...

  7. C++走向远洋——66(十五周阅读程序)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  8. Implementing 5G NR Features in FPGA

    目录 论文来源 论文简介 基本原理 论文创新点 借鉴之处 论文来源 2018 European Conference on Networks and Communications (EuCNC),Ja ...

  9. Swift--struct与class的区别(汇编角度底层分析)

    概述 相对Objective-C, Swift使用结构体Struct的比例大大增加了,其中Int, Bool,以及String,Array等底层全部使用Struct来定义!在Swift中结构体不仅可以 ...

  10. php-fpm启动失败处理

    报错信息: No pool defined. at least one pool section must be specified in config file [11-Mar-2019 23:57 ...