程序员开发实例大全宝库

网站首页 > 编程文章 正文

带你玩转Android 之《利用独特方式实现注册和登陆功能》

zazugpt 2024-08-12 03:23:56 编程文章 38 ℃ 0 评论

~~~~~~开始啦~~~~~~

在安卓开发中,常用的实现登陆和注册的功能有基于http访问网页的形式和安卓端调用服务器接口和方法实现的注册和登陆,采用SOAP来封装数据,还有基于服务器和数据库,在本地创建物理数据库。今天我们使用访问http网页的形式来实现注册和登陆,这里不需要数据库也能够存储数据,而且登陆的密码采用了一种特殊的方式来实现。这里所说的特殊的方式就是最重要的方法。这里小编这里告诉大家,或许大家不一定能听懂。这个机理很简单就是我们访问的网址+用户名做固定网址,如:

Stringurlstr="http://www.XXXX.com"+login_username.getText().toString().trim();

而这个网址被访问了以后将会获取到的网页内容,这个网页内容就是密码。并且这个密码是我们注册输入的用户名和密码,所以这里还需要实现获取网页的内容,获取这个内容的方法小编的第一篇就已经发表,去查看即可。但实现这里不一样的登陆注册方式,还需要在后台做服务器,而关于这个服务器小编也发表了。一下将使用代码解释给大家这个独特的方法。最终结果如图:

注意:完整代码如下,但是很长,建议先收藏,需要时调用。

1:.java文件一,对应布局一:(注册实现)

package com.example.dlzc;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

import java.util.List;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.ParseException;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.HttpClient;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

import android.annotation.SuppressLint;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.os.StrictMode;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.View.OnFocusChangeListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

//使用网页是实现的注册 (这部分是注册)

public class MainActivity extends Activity {

private EditText register_username;//输入用户名

private EditText register_passwd;//输入密码

private EditText reregister_passwd;//确认输入密码

private Button register_submit;//注册按钮

TextView tv;

Button btn;

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy);

setContentView(R.layout.activity_main);

register_username=(EditText)findViewById(R.id.register_username);

register_passwd=(EditText)findViewById(R.id.register_passwd);

reregister_passwd=(EditText)findViewById(R.id.reregister_passwd);

register_submit=(Button)findViewById(R.id.register_submit);

tv=(TextView)findViewById(R.id.textView1);

btn=(Button)findViewById(R.id.button1);

btn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

Intent intent = new Intent(MainActivity.this,dljm.class);

startActivity(intent);

}

});

//使用焦点 用户名设置监听来体现输入用户名如果小于4位数则 Toast显示 可包含空格

register_username.setOnFocusChangeListener(new OnFocusChangeListener()

{

@Override

public void onFocusChange(View v, boolean hasFocus) {

// TODO Auto-generated method stub

if(!hasFocus){

if(register_username.getText().toString().trim().length()<4){

Toast.makeText(MainActivity.this, "用户名不能小于4个字符", Toast.LENGTH_SHORT).show();

}

}

}

});

//同理,只是密码不能少于8位

register_passwd.setOnFocusChangeListener(new OnFocusChangeListener()

{

@Override

public void onFocusChange(View v, boolean hasFocus) {

// TODO Auto-generated method stub

if(!hasFocus){

if(register_passwd.getText().toString().trim().length()<6){

Toast.makeText(MainActivity.this, "密码不能小于8个字符", Toast.LENGTH_SHORT).show();

}

}

}

});

//确认输入第二次,如果不对则提示

reregister_passwd.setOnFocusChangeListener(new OnFocusChangeListener()

{

@Override

public void onFocusChange(View v, boolean hasFocus) {

// TODO Auto-generated method stub

if(!hasFocus){

if(!reregister_passwd.getText().toString().trim().equals(register_passwd.getText().toString().trim())){

Toast.makeText(MainActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();

}

}

}

});

//注册按钮 if()表示用户名和密码不输入则return返回,无法注册,否则,用户名和密码,确认密码输入完后,点击注册按钮监听

//注册按钮监听将会访问http,因为这里实现的注册是使用http的形式

register_submit.setOnClickListener(new OnClickListener(){

@Override

public void onClick(View v) {

if(!checkEdit()){

return;

}

//注册访问网址

String httpUrl="http://www.XXXX.com/zc/zc.php?name="+register_username.getText().toString().trim()+"&mm="+register_passwd.getText().toString().trim();

HttpPost httpRequest=new HttpPost(httpUrl);

Toast.makeText(getBaseContext(), "注册成功!", 4000);

List<NameValuePair> params=new ArrayList<NameValuePair>();

//添加用户名和密码

params.add(new BasicNameValuePair("username",register_username.getText().toString().trim()));

params.add(new BasicNameValuePair("password",register_passwd.getText().toString().trim()));

HttpEntity httpentity = null;

try {

httpentity = new UrlEncodedFormEntity(params,"utf8");

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

httpRequest.setEntity(httpentity);

HttpClient httpclient=new DefaultHttpClient();

HttpResponse httpResponse = null;

// UserDataWriteHelper uw=new UserDataWriteHelper(MainActivity.this);

try {

httpResponse = httpclient.execute(httpRequest);

} catch (ClientProtocolException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//页面请求的状态值,分别有:200请求成功、303重定向、400请求错误、401未授权、403禁止访问、404文件未找到、500服务器错误

//判断是否请求成功

if(httpResponse.getStatusLine().getStatusCode()==200)

{

String strResult = null;

try {

strResult = EntityUtils.toString(httpResponse.getEntity());

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Toast.makeText(MainActivity.this, strResult, Toast.LENGTH_SHORT).show();

}

else

{

Toast.makeText(MainActivity.this, "请求错误", Toast.LENGTH_SHORT).show();

}

}

});

}

private boolean checkEdit(){

if(register_username.getText().toString().trim().equals("")){

Toast.makeText(MainActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();

}else if(register_passwd.getText().toString().trim().equals("")){

Toast.makeText(MainActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();

}else if(!register_passwd.getText().toString().trim().equals(reregister_passwd.getText().toString().trim())){

Toast.makeText(MainActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();

}else{

return true;

}

return false;

}

}

注册布局:

<?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"

>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="注册"

android:textSize="22dip"

android:paddingLeft="140dip"

android:paddingRight="50dip"

android:paddingTop="10dip"

/>

<EditText

android:id="@+id/register_username"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginLeft="20dip"

android:layout_marginRight="20dip"

android:layout_marginTop="20dip"

android:height="40dip"

android:hint="用户名/手机号/Email" />

<EditText

android:id="@+id/register_passwd"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginLeft="20dip"

android:layout_marginRight="20dip"

android:layout_marginTop="20dip"

android:height="40dip"

android:hint="密码" />

<EditText

android:id="@+id/reregister_passwd"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginLeft="20dip"

android:layout_marginRight="20dip"

android:layout_marginTop="20dip"

android:height="40dip"

android:hint="确认密码" />

<Button

android:id="@+id/register_submit"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical|center_horizontal"

android:layout_marginTop="60dip"

android:height="40dip"

android:text="确定"

android:textSize="22dip"

android:width="70dip" />

<Button

android:id="@+id/button1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"

android:text="返回登陆"

android:textSize="22sp" />

</LinearLayout>

2:.java文件登陆对应布局:

package com.example.dlzc;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

import java.util.List;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.ParseException;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.HttpClient;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.os.StrictMode;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.View.OnFocusChangeListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

//这部分是登陆,要实现的功能是:访问注册的信息,登陆用户名和密码必须和注册一样,不然不能登陆

//我们设置的登陆密码的基本原理是,登陆访问网址是访问网址+用户名的形式,这样可以实现注册过的用户名不能再注册,而密码

//就是访问网址+用户名网页下的内容。并用if判断。

//dlzc继承Activity并实现接口的监听

public class dljm extends Activity implements OnClickListener {

private static final String httpUrl = null;//给网址一个初始常量

private EditText login_username;//输入用户名控件

private EditText login_password;//输入密码

private Button user_login_button;//登陆控件

private Button user_register_button;//注册控件

TextView tv;

Thread t;//使用线程

String mm;//定义一个字符串

Runnable r=new Runnable(){ //运行,多线程

@Override

public void run() {

Message msg=new Message(); //Handler()中的两个用途之一,Message()获取消息

try{//下面用get的方式打开网页,判断用户名用密码是否正确

String urlstr="http://www.XXXX.com/zc/"+login_username.getText().toString().trim();

urlstr=new String(urlstr.getBytes("utf-8"),"ISO-8859-1");

HttpClient hc=new DefaultHttpClient();//

HttpGet get=new HttpGet(urlstr);

HttpResponse hr=hc.execute(get);

HttpEntity he=hr.getEntity();

String s=EntityUtils.toString(he,"utf-8");//s获取的网页内容

mm=s;

msg.what=0;

//Thread.sleep(2000);

}catch(Exception e){e.printStackTrace();msg.what=1;}

h.sendMessage(msg);

}

};

Handler h=new Handler(){

@Override

public void handleMessage(Message msg) {

if(msg.what==0){

//这段代码是访问网页+用户名网页的内容,通过mm获取的内容,用if来,将密码文本做判断

if(new String(mm).equals(login_password.getText().toString().trim().toString()))

{

Intent intent=new Intent(dljm.this,zlzccg.class);

startActivity(intent);

}else {

Toast.makeText(getBaseContext(), "密码输入不正确", 4000).show();

return;

}

}

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy);

setContentView(R.layout.dljm);

initWidget();//构造方法

}

private void initWidget()

{

login_username=(EditText)findViewById(R.id.login_username);

login_password=(EditText)findViewById(R.id.login_password);

user_login_button=(Button)findViewById(R.id.user_login_button);

user_register_button=(Button)findViewById(R.id.user_register_button);

user_login_button.setOnClickListener(this);//登陆和注册设置监听

user_register_button.setOnClickListener(this);

//输入用户名不小4位数,显示提示

login_username.setOnFocusChangeListener(new OnFocusChangeListener()

{

@Override

public void onFocusChange(View v, boolean hasFocus) {

// TODO Auto-generated method stub

if(!hasFocus){

String username=login_username.getText().toString().trim();

if(username.length()<4){

Toast.makeText(dljm.this, "用户名不能小于4个字符", Toast.LENGTH_SHORT);

}

}

}

});

login_password.setOnFocusChangeListener(new OnFocusChangeListener()

{

@Override

public void onFocusChange(View v, boolean hasFocus) {

// TODO Auto-generated method stub

if(!hasFocus){

String password=login_password.getText().toString().trim();

if(password.length()<4){

Toast.makeText(dljm.this, "密码不能小于4个字符", Toast.LENGTH_SHORT);

}

}

}

});

}

@Override

public void onClick(View v) { //使用构造方法

// TODO Auto-generated method stub

switch(v.getId())

{

case R.id.user_login_button:

if(checkEdit())

{

new Thread(r).start();

login();//在这里就是,当你未输入点击登陆时,会显示提示

}

break;

case R.id.user_register_button://注册,点击将跳转到注册界面

Intent intent2=new Intent(dljm.this,MainActivity.class);

startActivity(intent2);

break;

}

}

private boolean checkEdit(){

if(login_username.getText().toString().trim().equals("")){

Toast.makeText(dljm.this, "用户名不能为空", Toast.LENGTH_SHORT).show();

}else if(login_password.getText().toString().trim().equals("")){

Toast.makeText(dljm.this, "密码不能为空", Toast.LENGTH_SHORT).show();

}else{

return true;

}

return false;

}

//注册完成后,返回登陆,登陆按钮将会通过访问http的数据,来判断输入是否和用户名,密码注册一样

private void login(){

//这部分是登陆访问网址 ,只要获取该网址http://www.XXXX.com/zc/"+login_username.getText().toString().trim()

//只要获取该网址下的内容,在用密码输入框判断是否和该内容相等,相等则可以实现登陆

String httpUrl="http://www.itdoy.com/zc/"+login_username.getText().toString().trim();

HttpPost httpRequest=new HttpPost(httpUrl);

/* if(login_username.getText().toString().trim().equals(httpUrl)&&login_password.getText().toString().trim().equals(httpUrl)){

Intent intent=new Intent(dljm.this,zlzccg.class);

startActivity(intent);

} else

{

Toast.makeText(dljm.this, "登录失败!", Toast.LENGTH_SHORT).show();

}*/

List<NameValuePair> params=new ArrayList<NameValuePair>();

params.add(new BasicNameValuePair("username",login_username.getText().toString().trim()));

params.add(new BasicNameValuePair("password",login_password.getText().toString().trim()));

HttpEntity httpentity = null;

try {

//设置参数信息

httpentity = new UrlEncodedFormEntity(params,"utf8");

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

httpRequest.setEntity(httpentity);

HttpClient httpclient=new DefaultHttpClient();

HttpResponse httpResponse = null;

try {

httpResponse = httpclient.execute(httpRequest);

} catch (ClientProtocolException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

if(httpResponse.getStatusLine().getStatusCode()==200)

{

String strResult = null;

try {

strResult = EntityUtils.toString(httpResponse.getEntity());

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Toast.makeText(dljm.this, strResult, Toast.LENGTH_SHORT).show();

Intent intent=new Intent(dljm.this,zlzccg.class);

//startActivity(intent);

}

else

{

Toast.makeText(dljm.this, "登录失败!", Toast.LENGTH_SHORT).show();

}

}

}

登陆布局:

<?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"

>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="登录"

android:textSize="22dip"

android:paddingLeft="140dip"

android:paddingRight="50dip"

android:paddingTop="10dip"

/>

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical" >

<EditText

android:id="@+id/login_username"

android:layout_width="fill_parent"

android:layout_height="40dip"

android:layout_marginLeft="20dip"

android:layout_marginRight="20dip"

android:layout_marginTop="30dip"

android:hint="用户名/手机号/Email"

android:paddingTop="10dip"

android:textSize="18dip" >

</EditText>

<EditText

android:id="@+id/login_password"

android:layout_width="fill_parent"

android:layout_height="40dip"

android:layout_marginLeft="20dip"

android:layout_marginRight="20dip"

android:layout_marginTop="10dip"

android:password="true"

android:paddingTop="10dip"

android:textSize="18dip"

android:hint="密码"

>

</EditText>

</LinearLayout>

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:layout_marginTop="15dip">

</LinearLayout>

<LinearLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:layout_marginTop="20dip">

<Button

android:id="@+id/user_login_button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="登录"

android:layout_marginLeft="50dip"

android:textColor="#F7FBFD"

android:background="#FF0000"

android:width="70dip"

android:height="40dip"

android:textSize="18dip"

/>

<Button

android:id="@+id/user_register_button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="55dp"

android:background="#0F9000"

android:height="40dip"

android:text="注册"

android:textColor="#F7FBFD"

android:textSize="18dip"

android:width="70dip" />

</LinearLayout>

</LinearLayout>

跳转界面.Java文件对应布局:

package com.example.dlzc;

import android.app.Activity;

import android.os.Bundle;

public class zlzccg extends Activity{

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.dlzccg);

}

}

布局:

<?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" >

<TextView

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="恭喜你已经实现登陆注册功能" />

</LinearLayout>

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表