Android实现简单用户注册案例

本文实例为大家分享了Android实现简单用户注册的具体代码,供大家参考,具体内容如下

目标: 设计一个用户注册案例。在主界面中对输入的手机号、密码、性别、爱好和城市后,可以在界面二中进行显示。

提示:

1、页面布局的元素用到TextView、EditText、Button、RadioButton、CheckBox、Spinner;
2、通过intent实现主界面跳转到界面二
3、涉及传递多个的数据时,使用Bundle对象作为容器,通过调用Bundle的putString先将数据存储到Bundle中,然后调用Intent的putExtras()方法将Bundle存入Intent中,然后获得Intent后, 调用getExtras()获得Bundle容器,然后调用其getString获取对应的数据!

Bundle bundle=new Bundle();
bundle.putString("phone",phone_str);
bundle.putString("paswd",paswd_str);
bundle.putString("sex",sex_str);
bundle.putString("hobby",hobby_str);
bundle.putString("city",city_str);
//为意图追加额外的数据,意图原来已经具有的数据不会丢失,但key同名的数据会被替换
intent.putExtras(bundle);
//取得启动该Activity的Intent对象
 Intent intent=this.getIntent();
 Bundle bundle=intent.getExtras();
 /*取出Intent中附加的数据*/
 String phone=bundle.getString("phone");
 String paswd=bundle.getString("paswd");
 String sex=bundle.getString("sex");
 String hobby=bundle.getString("hobby");
 String city=bundle.getString("city");

界面显示如下:

实现如下:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@drawable/img03"
 tools:context="com.example.jinjin.applicationtest4.MainActivity">

 <!--手机号-->
 <LinearLayout
 android:layout_width="368dp"
 android:layout_height="wrap_content"
 tools:layout_editor_absoluteY="0dp"
 android:orientation="horizontal"
 tools:layout_editor_absoluteX="8dp">
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="40dp"
  android:textSize="18sp"
  android:textColor="@android:color/background_dark"
  android:text="手机号:"/>
 <EditText
  android:id="@+id/phone"
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:inputType="phone"
  android:hint="请输入手机号"/>
 </LinearLayout>
 <!--密码-->
 <LinearLayout
 android:layout_width="368dp"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="40dp"
  android:textSize="18sp"
  android:textColor="@android:color/background_dark"
  android:text="密 码:"/>
 <EditText
  android:id="@+id/paswd"
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:inputType="numberPassword"
  android:hint="请输入密码"/>
 </LinearLayout>
 <!--性别选择-->
 <RadioGroup
 android:id="@+id/sex"
 android:layout_width="match_parent"
 android:layout_height="40dp"
 android:orientation="horizontal">
 <RadioButton
  android:id="@+id/nan"
  android:layout_width="50dp"
  android:layout_height="wrap_content"
  android:checked="true"
  android:text="男"/>
 <RadioButton
  android:id="@+id/nv"
  android:layout_width="50dp"
  android:layout_height="wrap_content"
  android:text="女"/>
 </RadioGroup>
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
 <CheckBox
  android:id="@+id/read_book"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="读书" />

 <CheckBox
  android:id="@+id/play_ball"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="打球" />
 <CheckBox
  android:id="@+id/music"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="听音乐" />
 </LinearLayout>
 <Spinner
 android:id="@+id/spinner"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" />
 <Button
 android:id="@+id/register"
 android:layout_width="fill_parent"
 android:background="#3F51B5"
 android:textColor="#FFFFFF"
 android:textSize="18sp"
 android:text="注 册"
 android:layout_height="40dp" />
</LinearLayout>

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:background="@drawable/img03"
 android:layout_height="match_parent">
 <TextView
 android:id="@+id/show_content"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:textSize="17sp"
 android:layout_marginLeft="50dp"
 android:textColor="@android:color/black"
 android:text="TextView" />
</LinearLayout>

MainActivity.java

package com.example.jinjin.applicationtest4;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
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 MainActivity extends AppCompatActivity implements View.OnClickListener,RadioGroup.OnCheckedChangeListener{
 //定义字符串用来保存各个信息
 private String phone_str="";
 private String paswd_str="";
 private String sex_str="男";
 private String hobby_str="1";
 private String city_str="";
 //组件定义
 EditText phone_edit,paswd_edit;
 RadioGroup sex_group;
 RadioButton nan_but,nv_but;
 CheckBox play,read,music;
 Button register;
 Spinner spinner;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 //初始化组件
 phone_edit = (EditText) findViewById(R.id.phone);
 paswd_edit=(EditText)findViewById(R.id.paswd);
 sex_group=(RadioGroup)findViewById(R.id.sex);
 //添加监听事件
 nan_but=(RadioButton)findViewById(R.id.nan);
 sex_group.setOnCheckedChangeListener(this);
 read=(CheckBox)findViewById(R.id.read_book);
 play=(CheckBox)findViewById(R.id.play_ball);
 music=(CheckBox)findViewById(R.id.music);
 register=(Button)findViewById(R.id.register);
 //添加监听事件
 register.setOnClickListener(this);
 spinner=(Spinner)findViewById(R.id.spinner);
  // 创建ArrayAdapter对象
 final String[] city=new String[]{"南宁","桂林","百色","柳州","玉林","河池"};
 ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,city);
 spinner.setAdapter(adapter);
 //城市下拉单列表添加监听事件
 spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

  @Override
  public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
  city_str=city[i];
  }
  @Override
  public void onNothingSelected(AdapterView<?> adapterView) {
  //未选中状态
  }
 });
 }

 @Override
 public void onClick(View view) {
 switch (view.getId()){
  case R.id.register:
  //获取手机号和密码
  phone_str=phone_edit.getText().toString();
  paswd_str=paswd_edit.getText().toString();
  //获取兴趣爱好即复选框的值
  hobby_str="";//清除上一次已经选中的选项
  if (read.isChecked()){
   hobby_str+=read.getText().toString();
  }if(play.isChecked()){
  hobby_str+=play.getText().toString();
  }if(music.isChecked()){
  hobby_str+=music.getText().toString();
  }
  Intent intent=new Intent(this,SecondActivity.class);
  Bundle bundle=new Bundle();
  bundle.putString("phone",phone_str);
  bundle.putString("paswd",paswd_str);
  bundle.putString("sex",sex_str);
  bundle.putString("hobby",hobby_str);
  bundle.putString("city",city_str);
  intent.putExtras(bundle);
  startActivity(intent);
  break;
 }
 }

 @Override
 public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
 //根据用户选择来改变sex_str的值
 sex_str=i==R.id.nan?"男性":"女性";
 }
}

SecondActivity.java

package com.example.jinjin.applicationtest4;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

/**
 * Created by jinjin on 2020/5/23.
 */

public class SecondActivity extends AppCompatActivity {
 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_second);
 Intent intent=this.getIntent();
 Bundle bundle=intent.getExtras();
 String phone=bundle.getString("phone");
 String paswd=bundle.getString("paswd");
 String sex=bundle.getString("sex");
 String hobby=bundle.getString("hobby");
 String city=bundle.getString("city");
 TextView show_text=(TextView)findViewById(R.id.show_content);
 show_text.setText("手机号为:"+phone+"\n"+"密码为:"+paswd+"\n"+"性别是:"+sex+"\n"+"爱好是:"+hobby+"\n"+"城市是:"+city);
 }
}

巩固监听事件:如果要对register和spinne的监听事件改造方法,如何重新实现监听?

  • register可使用内部类,并重写onClick()方法 。
  • spinner可使用实现接口的监听事件。

实现如下

package com.example.jinjin.applicationtest4;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
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 MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener,AdapterView.OnItemSelectedListener{
 //定义字符串用来保存各个信息
 private String phone_str = "";
 private String paswd_str = "";
 private String sex_str = "男";
 private String hobby_str = "1";
 private String city_str = "";
 //组件定义
 EditText phone_edit, paswd_edit;
 RadioGroup sex_group;
 RadioButton nan_but, nv_but;
 CheckBox play, read, music;
 Button register;
 Spinner spinner;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 //初始化组件
 phone_edit = (EditText) findViewById(R.id.phone);
 paswd_edit = (EditText) findViewById(R.id.paswd);
 sex_group = (RadioGroup) findViewById(R.id.sex);
 //添加监听事件
 nan_but = (RadioButton) findViewById(R.id.nan);
 sex_group.setOnCheckedChangeListener(this);
 read = (CheckBox) findViewById(R.id.read_book);
 play = (CheckBox) findViewById(R.id.play_ball);
 music = (CheckBox) findViewById(R.id.music);
 register = (Button) findViewById(R.id.register);
 //添加监听事件
// register.setOnClickListener(this);
 spinner = (Spinner) findViewById(R.id.spinner);
  //直接new一个内部类对象作为参数
 register.setOnClickListener(new mclick());

// final String[] city=new String[]{"南宁","桂林","百色","柳州","玉林","河池"};
// ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,city);
// spinner.setAdapter(adapter);
// //城市下拉单列表添加监听事件
// spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
//  @Override
//  public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//  city_str=city[i];
//  }
//  @Override
//  public void onNothingSelected(AdapterView<?> adapterView) {
//  }
// });

 //Spinner加载监听事件
 spinner.setOnItemSelectedListener(this);
 }

 @Override
 public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
 city_str=MainActivity.this.getResources().getStringArray(R.array.city)[i];
 }

 @Override
 public void onNothingSelected(AdapterView<?> adapterView) {

 }

 //定义一个内部类,实现View.OnClickListener接口,并重写onClick()方法
 class mclick implements View.OnClickListener {

 @Override
 public void onClick(View view) {

  switch (view.getId()) {
  case R.id.register:
   //获取手机号和密码
   phone_str = phone_edit.getText().toString();
   paswd_str = paswd_edit.getText().toString();
   //获取兴趣爱好即复选框的值
   hobby_str = "";//清除上一次已经选中的选项
   if (read.isChecked()) {
   hobby_str += read.getText().toString();
   }
   if (play.isChecked()) {
   hobby_str += play.getText().toString();
   }
   if (music.isChecked()) {
   hobby_str += music.getText().toString();
   }
   Intent intent = new Intent(MainActivity.this, SecondActivity.class);
   Bundle bundle = new Bundle();
   bundle.putString("phone", phone_str);
   bundle.putString("paswd", paswd_str);
   bundle.putString("sex", sex_str);
   bundle.putString("hobby", hobby_str);
   bundle.putString("city", city_str);
   intent.putExtras(bundle);
   startActivity(intent);
   break;
  }
 }
 }

 @Override
 public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
 //根据用户选择来改变sex_str的值
 sex_str = i == R.id.nan ? "男性" : "女性";
 }
}

在res/values下编写一个:array.xml文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string-array name="city">
 <item>南宁</item>
 <item>桂林</item>
 <item>柳州</item>
 <item>百色</item>
 <item>河池</item>
 <item>玉林</item>
 </string-array>
</resources>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android QQ新用户注册界面绘制

    先看看效果图: 问题: 1.下拉列表(因为还没看到这里...) 2.标题栏显示问题 3.按钮的 Enable 设置 .......... 以下是代码: 布局 fragment_main(问题1) <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layo

  • Android设计登录界面、找回密码、注册功能

    本文实例为大家分享了Android 登录.找回密码.注册功能的实现代码,供大家参考,具体内容如下 1.数据库的设计 我在数据库中添加了两张表,一张表用来存储用户信息,诸如用户名,密码,手机号等,可任意添加.另一张表用来存储上一个登录用户的账户信息,我是为了方便才另外创建了一张表去存储,而且这张表我设计了它只能存储一条信息,每次的存储都是对上一条记录的覆盖.事实上,我尝试过在存储用户信息的那张表内添加一个标识,用来标记上一次登录的是哪一个帐号,但是这样做的话,每次改变标识都需要遍历整张表,十分的麻

  • Android客户端实现注册、登录详解(1)

    我们在开发安卓App时难免要与服务器打交道,尤其是对于用户账号信息的注册与登录更是每个Android开发人员必须掌握的技能,本文将对客户端的注册/登录功能的实现进行分析,不到之处还请指出. 在这里我们仅讨论客户端如何请求服务器进行注册,而服务器在收到客户端请求后进行的一系列操作并不在本文所述范围内,有兴趣大家可以参考 请求服务器 客户端在进行用户信息的注册和登录时一般使用post请求(携带参数)去服务器.以volley框架请求服务器为例,真正与服务器进行交互的就是如下代码: StringRequ

  • Android开发中实现用户注册和登陆的代码实例分享

    在android的应用中越来越多的包含了网络互动功能,这就带来了注册,登陆账号功能.本文完整的介绍对话框的方式实现用户登陆功能. 登陆效果: 应用程序判断当前用户还未登陆,弹出登陆对话框,用户输入账号和密码信息后,传到服务器验证,验证成功后,现实Toast 成功信息,并转到其他界面. 注册效果:用户如没有账号,则点击登陆对话框的 "没有账号,快速注册账号", 弹出注册界面,用户输入注册信息,点击注册按钮,注册成功后,弹出toast信息"注册成功",完成注册后,转到其

  • Android用户注册界面

    推荐阅读:Android如何通过手机获取验证码来完成注册功能 先给大家展示下界面效果图,感觉满意,请参考实现代码. Main.xml源码 <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_par

  • Android用户注册界面简单设计

    本文实例为大家分享了Android用户注册界面的设计,供大家参考,具体内容如下 I. 实例目标 设计一个用户注册界面,在其中要使用到一些基础控件,如 文本框.编辑框.按钮.复选框等控件 II. 技术分析 首先在布局文件中使用控件的标记来配置所需要的各个控件,然后在 主Activity中获取到该控件,给其添加监听器来监听其操作,最后在控制台输出所操作的内容. III. 实现步骤 在Eclipse中创建 Android项目,名称为 TestUserRegister .设计一个用户注册界面,在其中要使

  • Android实现带头像的用户注册页面

    1.首先是注册页面的布局: <?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实现QQ新用户注册界面遇到问题及解决方法

    在上篇文章给大家介绍了Android实现QQ登录界面遇到问题及解决方法,本篇文章继续给大家介绍有关android qq界面知识. 先给大家展示下效果图: 问题: 1.下拉列表(因为还没看到这里...) 2.标题栏显示问题 3.按钮的 Enable 设置 以下是代码: 布局 fragment_main(问题1) <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools

  • Android如何通过手机获取验证码来完成注册功能

    注册很多app或者网络账户的时候,经常需要手机获取验证码,来完成注册,那时年少,只是觉得手机获取验证码这件事儿很好玩,并没有关心太多,她是如何实现的,以及她背后的故事到底是什么样子的,现在小编接手的这个项目里面,就需要通过手机号进行注册,并且手机号发送相应的验证码,来完成注册,那么在一些应用app里面到底是如何实现点击按钮获取验证码,来完成注册这整个流程的呢?今天小编就以注册为例,和小伙伴们分享一下,如何通过手机号获取验证码来完成注册的一整套流程以及如何采用正则表达式来验证手机号码是否符合电信.

  • Android实现注册登录界面的实例代码

    本文讲述了在linux命令下导出导入.sql文件的方法.分享给大家供大家参考,具体如下: AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="online.geekgalaxy.layoutlearn

随机推荐