`

android之实现注册功能

 
阅读更多

android之实现注册功能:

注意:(1、之前一直找不到不成功 的原因,经过调试,找到了,原来是:Toast.makeText(this, "用户名不能为空", 2000).show()没调用show方法;2、在switch下的case语句后一定要加break,不然他就会一直执行下一个case的

布局:layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"/>
<EditText
android:id="@+id/etUser"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入1-10个字符"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"/>
<EditText
android:id="@+id/etPassw"

android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入1-10个字符"
android:password="true"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认密码:"/>
<EditText
android:id="@+id/etRePassw"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入1-10个字符"
android:password="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择性别:"/>
<RadioGroup
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/rbMale"
android:text="男"
android:checked="true"/>
<RadioButton
android:id="@+id/rbfaMale"
android:text="女"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="城市:"/>
<Spinner
android:id="@+id/spCity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="@array/citys"
/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="兴趣爱好:"/>
<RadioGroup
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
>
<CheckBox
android:id="@+id/cbRead"
android:text="读书"
android:checked="true"/>
<CheckBox
android:id="@+id/cbTourist"
android:text="旅游"
/>
<CheckBox
android:id="@+id/cbPlayGame"
android:text="打电子"
/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_weight="1"
/>
<Button
android:id="@+id/btRegister"
android:text="注册"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/login32x32"
android:background="@drawable/btn_bg"
android:onClick="onclick"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_weight="1"
/>
<Button
android:id="@+id/btEixt"
android:text="退出"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/exit32x32"
android:background="@drawable/btn_bg"
android:onClick="onclick"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<Button
android:text="返回"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|fill_horizontal"
android:background="@drawable/btn_bg"

/>
</LinearLayout>
</LinearLayout>

java代码 :

1、主要代码 :

package com.sxt.main;


import com.sxt.entity.User;
import com.sxt.main.R;




import android.text.TextUtils;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;




public class MainActivity extends Activity {
EditText etUser,etPassw,etRePassw;
RadioButton rbMale,rbfaMale;
CheckBox cbRead,cbTourist,cbPlayGame;
Spinner spCity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
// TODO Auto-generated method stub
etUser = (EditText) findViewById(R.id.etUser);
etPassw = (EditText) findViewById(R.id.etPassw);
etRePassw = (EditText) findViewById(R.id.etRePassw);
rbMale = (RadioButton) findViewById(R.id.rbMale);
rbfaMale = (RadioButton) findViewById(R.id.rbfaMale);
cbRead = (CheckBox) findViewById(R.id.cbRead);
cbTourist = (CheckBox) findViewById(R.id.cbTourist);
cbPlayGame = (CheckBox) findViewById(R.id.cbPlayGame);
spCity = (Spinner) findViewById(R.id.spCity);
}
public void onclick(View view){
switch(view.getId()){

case R.id.btRegister:
String userName = etUser.getText().toString();
if(TextUtils.isEmpty(userName)){
Toast.makeText(this, "用户名不能为空", 2000).show();
return;
}
String passW = etPassw.getText().toString();
if(TextUtils.isEmpty(passW)){
etPassw.setError("密码不能为空");
return;
}
String rePassw = etRePassw.getText().toString();
if(TextUtils.isEmpty(rePassw)){
etRePassw.setError("确认密码不能为空");
return;
}
if(!passW.equals(rePassw)){
Toast.makeText(this, "两次密码不一致", 2000).show();
return ;
}
char sex ;
if(rbMale.isChecked()){
sex = rbMale.getText().charAt(0);
}else{
sex = rbfaMale.getText().charAt(0);
}
StringBuffer s = new StringBuffer();
if(cbRead.isChecked()){
s.append(cbRead.getText().toString()+",");
}
if(cbTourist.isChecked()){
s.append(cbTourist.getText().toString()+",");
}
if(cbPlayGame.isChecked()){
s.append(cbPlayGame.getText().toString()+",");
}
String city = spCity.getSelectedItem().toString();
User user = new User(userName,passW,sex,
s.toString().equals("")?"":s.toString().substring(0, s.length()),
city);
Toast.makeText(this, "用户注册信息:"+user.toString(),100000).show();
break;
case R.id.btEixt:
finish();
break;
}
}

}

2、user类

package com.sxt.entity;


public class User {
private String userName;
private String passWord;
private char sex;
private String city;
private String hobby;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public User(String userName, String passWord, char sex, String city,
String hobby) {
super();
this.userName = userName;
this.passWord = passWord;
this.sex = sex;
this.city = city;
this.hobby = hobby;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return this.userName+","+this.passWord+","
+this.sex+","+this.city+","+this.hobby;
}

}

效果:



分享到:
评论

相关推荐

    android实现登录注册

    Android 实现登录注册功能是 Android 应用程序中非常重要的一部分。登录注册功能是确保用户身份的有效手段,保护用户的个人信息和数据安全。下面我们将详细讲解 Android 实现登录注册的知识点。 知识点一:Android ...

    自己实现的Android PHP JSON实现登陆注册功能

    总结来说,这个项目展示了如何使用Android的OkHttp3库配合PHP后端和JSON数据交换,实现用户登录和注册功能。它涵盖了客户端的网络请求、数据序列化,以及服务器端的数据处理和响应。这个过程中的每个环节都是现代...

    加入数据库mysql实现android注册登陆功能的客户端服务器源码与解析

    - 注册功能:当用户点击注册按钮,客户端通过网络库发送POST请求到服务器的注册接口,携带用户名和密码(可能经过加密)。 - 登录功能:类似地,登录时发送POST请求,包含用户名和密码,服务器验证后返回成功或...

    Android登陆注册实现

    本文将深入探讨如何在Android平台上实现登录和注册功能,主要围绕标题"Android登录注册实现"展开。 首先,我们需要理解Android的UI设计。在登录注册界面,通常包含输入框(EditText)用于收集用户名和密码,以及...

    Android PHP JSON 实现登陆注册功能

    本项目"Android PHP JSON 实现登陆注册功能"就是针对这一需求,通过使用Android客户端和PHP服务端相结合,利用JSON作为数据交换格式,实现用户信息的存储和检索。 首先,Android客户端是用户界面,它负责收集用户...

    Android之Android studio实现智能聊天

    开发者需要注册并获取API Key,然后在Android应用中集成这个API,以实现与机器人的通信。 要实现聊天功能,我们需要在Android应用中创建一个UI界面,这通常包括一个输入框让用户输入消息,以及一个显示机器人回复的...

    Android实现登陆注册连接数据库操作完整代码

    本项目"Android实现登陆注册连接数据库操作完整代码"提供了完整的源代码,涵盖了从用户界面设计到数据库交互的整个流程。接下来,我们将详细讨论这个项目中涉及的关键知识点。 1. **用户界面(UI)设计**: - 使用...

    android studio连接云服务器mysql实现登录注册

    在Android应用开发中,连接云服务器的MySQL数据库是常见的需求,尤其在实现用户登录注册功能时。本教程将深入探讨如何使用Android Studio、Java以及相关的网络技术来完成这一任务。 首先,我们需要理解Android ...

    android SQLite实现本地登录注册功能,SQLite简单应用(android studio)

    本篇文章将详细探讨如何在Android Studio中利用SQLite实现本地登录注册功能。 首先,我们需要在AndroidManifest.xml文件中添加读写权限,这是使用SQLite数据库的必要步骤: ```xml &lt;uses-permission android:name=...

    Android代码-用户注册登录的简单实现.zip

    在Android平台上,用户注册登录是移动应用开发的基本功能之一,涉及到客户端与服务器之间的数据交互以及安全验证。这个"Android代码-用户注册登录的简单实现.zip"压缩包可能包含了一个简单的示例项目,用于演示如何...

    android之利用SQLite实现登陆和注册

    综上所述,实现Android应用的登录和注册功能,需要掌握SQLite数据库的使用,包括创建数据库帮助类、设计数据库表结构、编写业务逻辑以及密码安全处理。在实际项目中,还需要考虑错误处理、事务管理和性能优化等问题...

    基于mysql-android studio-web服务器实现简单注册登录功能源码与解析

    此资源实现安卓APP注册登录功能,通过Android Studio编写界面和相应功能,用Eclipse开启web服务器进行客户端与服务端之间的通信,并且将用户的账户密码经过MD5的加密后存入Mysql数据库中。

    android登陆注册功能

    在这个实例中,我们关注的是Android登录注册功能的实现,包括记住密码的选项,以及使用本地SQLite数据库进行数据存储。 首先,登录功能允许用户通过输入他们的用户名和密码来验证自己的身份。在Android中,通常会...

    Android studio实现登陆

    在Android Studio中实现登录功能是开发移动应用的基本需求之一,尤其对于那些需要用户账户的社交、电商或服务类应用。本篇文章将详细讲解如何在Android Studio中构建一个基础的登录界面和逻辑,以及与服务器进行数据...

    Android登录注册功能 数据库SQLite验证

    Android登录注册功能的实现需要使用到Android的UI组件,如EditText、Button等,以及数据库存储用户信息。在本例中,我们使用SQLite数据库来存储用户信息,并使用Java语言来实现登录注册功能。 首先,我们需要在...

    Android手机端连接Java后台 实现登录注册功能

    登录注册功能可以通过POST请求来实现,其中注册通常是POST到"/register",登录则是POST到"/login"。 3. **Android端开发**:在Android端,你可以使用HttpURLConnection或者第三方库如Retrofit、Volley来发送HTTP...

    android sqlite数据库实现登录注册功能

    以上就是使用SQLite数据库实现Android应用登录注册功能的基本步骤。在实际开发中,还需考虑异常处理、数据加密、密码找回等功能,以及对用户输入的合法性进行验证,以提供更安全、友好的用户体验。同时,随着应用的...

    Android Studio实现功能丰富的仓库管理系统

    本次项目主要实现了仓库管理系统。用户分为超级管理员、出入库管理员和商品管理员这三种角色。首先进行超级管理员的注册,然后进行添加用户,超级管理员可以添加和查询其他两种用户角色。用超级管理员创建好的用户...

Global site tag (gtag.js) - Google Analytics