本文转自:http://hteqc6o.blog.sohu.com/199334086.html

用户注册

1.首先,先画你想要编译出的界面

根据草图,仅仅使用linearLayout的布局是不够的,还需要与RelativeLayout嵌套使用

编写String.Xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="hello">Hello World, UserLoginActivity!</string>

<string name="app_name">用户注册界面</string>

<string name="nameString">用户名</string>

<string name="ageString">年龄</string>

<string name="registerButtonText">注册</string>

<string name="sexString">性别</string>

<string name="favoriteString">喜好</string>

<string name="cityString">城市</string>

<string name="passString">密码</string>

<string name="pingpang">兵乓球</string>

<string name="basketball">篮球</string>

<string name="football">足球</string>

<string name="tennis">网球</string>

</resources>

编写parameters.xml:

这是存放属性信息,如字体、TextView和EditText的宽度。

<?xml version="1.0" encoding="utf-8"?>

<resources>

<dimen name="fontSize">22px</dimen>

<dimen name="TextViewWidth">90px</dimen>

<dimen name="EditTextWidth">160px</dimen>

</resources>

再编写main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<RelativeLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

<TextView

android:layout_width="@dimen/TextViewWidth"

android:layout_height="wrap_content"

android:text="@string/nameString"

android:textSize="@dimen/fontSize"

android:id="@+id/name"/>

<EditText

android:layout_width="@dimen/EditTextWidth"

android:layout_height="wrap_content"

android:layout_toRightOf="@string/nameString"

android:layout_alignTop="@id/name"

android:id="@+id/nameValue"/>/

</RelativeLayout>

<RelativeLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

<TextView

android:layout_width="@dimen/EditTextWidth"

android:layout_height="wrap_content"

android:text="@string/passString"

android:textSize="@dimen/fontSize"

android:id="@+id/pass"/>

<EditText

android:password="true"

android:layout_width="@dimen/EditTextWidth"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/pass"

android:layout_alignTop="@id/pass"

android:id="@+id/passValue"

/>

</RelativeLayout>

<RelativeLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

<TextView android:layout_width="@dimen/TextViewWidth"

android:layout_height="wrap_content"

android:text="@string/ageString"

android:textSize="@dimen/fontSize"

android:id="@+id/age"

/>

<EditText

android:layout_width="@dimen/EditTextWidth"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/age"

android:layout_alignTop="@id/age"

android:id="@+id/ageValue"

/>

</RelativeLayout>

<RelativeLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

<TextView android:layout_width="@dimen/TextViewWidth"

android:layout_height="wrap_content"

android:text="@string/sexString"

android:textSize="@dimen/fontSize"

android:id="@+id/sex"

/>

<RadioGroup

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/sex"

android:checkedButton="@+id/radioMan"

android:orientation="horizontal"

android:id="@+id/sexMenu">

<RadioButton android:text="男" android:id="@id/radioMan"/>

<RadioButton android:text="女" android:id="@+id/radioWomen"/>

</RadioGroup>

</RelativeLayout>

<RelativeLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

<TextView

android:layout_width="@dimen/TextViewWidth"

android:layout_height="wrap_content"

android:text="@string/favoriteString"

android:textSize="@dimen/fontSize"

android:id="@+id/favorite"/>

<CheckBox

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/favorite"

android:text="@string/pingpang"

android:id="@+id/checkboxpingpang"

/>

<CheckBox

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/checkboxpingpang"

android:text="@string/football"

android:id="@+id/checkboxfootball"/>

<CheckBox

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/favorite"

android:layout_below="@id/checkboxfootball"

android:text="@string/basketball"

android:id="@+id/checkboxbasketball"/>

<CheckBox

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/checkboxbasketball"

android:layout_alignTop="@id/checkboxbasketball"

android:text="@string/tennis"

android:id="@+id/checkboxtennis"/>

</RelativeLayout>

<RelativeLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

<TextView

android:layout_width="@dimen/TextViewWidth"

android:layout_height="wrap_content"

android:text="@string/cityString"

android:textSize="@dimen/fontSize"

android:id="@+id/city"/>

<Spinner

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/city"

android:id="@+id/cityItems">

</Spinner>

</RelativeLayout>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/registerButtonText"

android:id="@+id/regidterButton"/>

</LinearLayout>

·参数设置:

<dimen name=”fontSize”>22px</dimen>

存放px(pixels),in(inches),mm(millmeters),pt(points at 72 DPI)类型的数据

·应用:

Android:textSize=”@dimen/fontSize”

·布局的特别属性

在这里我们需要用到Relativelayout 的个别属性,例如:

Android:layout_toRightOf=”@id/age”

这是与控件age向右对齐

·输入框的输入限制

Android:numeric=”integer

编写java代码:

package com.sharpandroid.UserLogin;

import java.util.ArrayList;

import java.util.List;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.ProgressDialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.EditText;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.Spinner;

public class LoginActivity extends Activity {

private static final String[] cities = {"北京","上海","石家庄","保定","米兰","慕尼黑","巴黎"};

private EditText name,age,pass;

private Button regButton;

private RadioGroup sexRadioGroup;

private CheckBox basketball,football,pingpang,tennis;

private Spinner cityItems;

private boolean flag = true;

private List<CheckBox> favorities;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//定义一个ArrayList,用来存放所有的checkBox

favorities = new ArrayList<CheckBox>();

//得到相应的对对象

name = (EditText) findViewById(R.id.nameValue);

age = (EditText) findViewById(R.id.ageValue);

pass = (EditText) findViewById(R.id.passValue);

regButton = (Button) findViewById(R.id.regidterButton);

cityItems = (Spinner) findViewById(R.id.cityItems);

sexRadioGroup = (RadioGroup) findViewById(R.id.sexMenu);

basketball = (CheckBox) findViewById(R.id.checkboxbasketball);

//将basketball对象添加到favorities中

favorities.add(basketball);

football = (CheckBox) findViewById(R.id.checkboxfootball);

favorities.add(football);

pingpang = (CheckBox) findViewById(R.id.checkboxpingpang);

favorities.add(pingpang);

tennis = (CheckBox) findViewById(R.id.checkboxtennis);

favorities.add(tennis);

//创建一个数组型适配器,并将cities中的数据

ArrayAdapter<String>adpter = new ArrayAdapter<String>(

LoginActivity.this,android.R.layout.simple_spinner_item,cities);

adpter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

cityItems.setAdapter(adpter);

//为regButton注册一个事件监听器

regButton.setOnClickListener(new View.OnClickListener() {

//当按钮被点击的时候调用

@Override

public void onClick(View v) {

flag = addUser();

if(flag)

//创建Alertdialog对话框的显示登录信息。

new

AlertDialog.Builder(LoginActivity.this).setTitle("请确认信息")

.setMessage("您的信息如下:"+"\n" + "姓名:"

+name.getText().toString()+"/n"+" 年龄:"

+age.getText().toString()+"/n"+"性别:"

+getSex()+"\n"+"爱好:"+getFavorite()

+"\n"+"城市:"+getCity()+"\n")

.setCancelable(false).setPositiveButton("确定",

new DialogInterface.OnClickListener() {

public void onClick(

DialogInterface dialog, int id) {

// TODO Auto-generated method stub

ProgressDialog.show(

LoginActivity.this,"用户信息注册中","请等待……")

.setCancelable(true);

}

}).setNegativeButton("修改",

new DialogInterface.OnClickListener() {

public void onClick(

DialogInterface dialog, int id) {

dialog.cancel();//删除对话框

}

}).show();

}

});

}

//获取Spinner中的值

private String getCity(){

return cities[cityItems.getSelectedItemPosition()];

}

//获取checkBox中的值

private String getFavorite(){

String favString = "";

for(CheckBox cd : favorities){

if (cd.isChecked()){

favString +=cd.getText().toString();

favString +=",";

}

}

if(favString != ""){

favString = favString.substring(0,favString.length() -1);

}else{

favString="您还没有选择!";

}return favString;

}

private String getSex(){

RadioButton mRadio = (RadioButton)findViewById

(sexRadioGroup.getCheckedRadioButtonId());

return mRadio.getText().toString();

}

public boolean addUser(){

if(name.getText().toString().length()==0){

name.setError("用户名不嫩为空");

return false;

}

if (age.getText().toString().length()==0){

age.setError("年龄不能为空");

return false;

}

if(pass.getText().toString().length()==0){

pass.setError("密码不能为空");

return false;

}

return true;

}

}

当你没有输入用户名就点击注册会出现提示:如下:

点击注册会出现:

[转]Android的userlogin登录的更多相关文章

  1. android 注册、登录实现程序

    注册页面: user_register.xml: <?xml version="1.0" encoding="utf-8"?> <Linear ...

  2. Android 微信第三方登录(个人笔记)

    今天在写微信登录,花了半天时间搞定.然后写下自己的笔记,希望帮助更多的人...欢迎各位指教. 微信授权登录,官方说的不是很清楚.所以导致有一部分的坑. 微信注册应用平台的应用签名,下载 微信签名生成工 ...

  3. Android UmengShareSDK第三方登录

    Android UmengShareSDK 第三方登录- 今天就不废话了,集成平台第三方登录.市面上集成平台有shareSDK 和 Ument两种,shareSDK的ipa和服务好些,如果自己研究会很 ...

  4. android 案例二 登录界面

    效果图: 运行图:   总结: 编写这个简单的用户登录界面,主要用到了以下的知识:   java基础中的IO流的操作 用以读取.显示用户的信息 Android布局 线性布局和相对布局 数据的存储选在包 ...

  5. android实现第三方登录之QQ登录

    首先,当然是在腾讯开放平台(http://open.qq.com/)注册成为开发者,然后获取APP ID 1.下载SDK http://wiki.open.qq.com/wiki/mobile/SDK ...

  6. Android之QQ登录界面

    首先过程中碰到的几个问题: 1.对 EditText 进行自定义背景 2.运行时自动 EditText 自动获得焦点 3.在获得焦点时即清空 hint ,而不是输入后清空 4.清空按钮的出现时机(在得 ...

  7. Android开发之登录验证

    最近在做一个小项目,项目开发中需要实现一个登录验证功能,具体的要求就是,在Android端输入用户名和密码,在服务器端验证MySQL数据库中是否有此用户,实现之前当然首要的是,如何使Android端的 ...

  8. Android --LoginActivity模板登录

    Android Studio使用自带LoginActivity模板,制作登录界面 登录界面功能: 1.记住表单账户密码,并自动登录 //获得sp实例对象 sp = this.getSharedPref ...

  9. Java基础知识强化之网络编程笔记21:Android网络通信之 Android常用OAuth登录(获取令牌信息)

    1. 首先我们去下载开发相关SDK(Android): 下载百度使用OAuth的SDK(Android),如下: 下载链接为:http://developer.baidu.com/wiki/index ...

随机推荐

  1. F - Shooter

    UVA___10535 The shooter is in a great problem. He is trapped in a “2D” maze with a laser gun and can ...

  2. SpringSecurity 获取认证信息 和 认证实现

    JdbcDaoImpl 实现获取认证信息 PasswordEncoder 实现具体认证过程

  3. Thinkphp 批量更新方法 saveALL

    批量更新只适用于一个字段的更新,原理是用自定义函数拼接sql语句,然后再执行sql语句. //数据 $data[] = array('id'=>1,'value'=>value1); $d ...

  4. hrbust oj 1536 Leonardo's Notebook 置换群问题

    题目大意: 给出一个A~Z的置换G,问能否找到一个A~Z的置换G' 能够用来表示为 G = G'*G' 由定理: 任意一个长为 L 的置换的k次幂,都会把自己的每一个循环节分裂成gcd(L, K)份, ...

  5. AI小记-K近邻算法

    K近邻算法和其他机器学习模型比,有个特点:即非参数化的局部模型. 其他机器学习模型一般都是基于训练数据,得出一般性知识,这些知识的表现是一个全局性模型的结构和参数.模型你和好了后,不再依赖训练数据,直 ...

  6. Ubuntu安装deb时错误:“dpkg:错误:另外一个进程已经为 dpkg 状态数据库 加锁”解决

    以下方式任选一个即可: 1.重启系统 2.执行(这种方式不要尝试,系统很容易挂) sudo rm /var/lib/dpkg/lock 然后执行修复 sudo dpkg --configure -a

  7. laravel5.5更新到laravel5.7

    为什么要更新呢?因为项目用的第三方后台扩展包,有很些bug,不够完美.想要一个漂亮的后台,那个后台只支持5.7. 然后,我就开始更新框架了. 修改后:"php": "&g ...

  8. Spring注解配置定时任务<task:annotation-driven/>

    http://m.blog.csdn.net/article/details?id=50945311 首先在配置文件头部的必须要有: xmlns:task="http://www.sprin ...

  9. 关于OleDB连接Excel的Extended Properties(扩展属性)HDR=YES; IMEX=2个人理解心得

    近期在用C#写一个创建Excel并将数据导出到Excel的WinForm程序, 让我对OleDB连接Excel的Extended Properties(扩展属性)HDR=YES; IMEX=2有了深刻 ...

  10. 5分钟APIG实战: 使用Rust语言快速构建API能力开放

    序言:Rust语言简介 参与过C/C++大型项目的同学可能都经历过因为Null Pointer.Memory Leak等问题“被” 加班了不知道多少个晚上.别沮丧,你不是一个人,Mozilla Fir ...