android实现注册登录程序

本文实例为大家分享了android实现注册登录程序的具体代码,供大家参考,具体内容如下

注册页面:

user_register.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"
    android:background="@drawable/bg_01">"
    
      <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注册"
        android:textSize="22dip"
        android:textColor="#FFFFFF"
        android:paddingLeft="140dip"
        android:paddingRight="50dip"
        android:paddingTop="10dip"
        android:background="@drawable/topbg"
        />
    "
    <EditText 
        android:id="@+id/register_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:background="@drawable/search" 
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:height="40dip"
        android:hint="用户名"
        />
 
     <EditText 
        android:id="@+id/register_passwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:background="@drawable/search" 
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:height="40dip"
        android:hint="密码"
        />
    
      <EditText 
        android:id="@+id/reregister_passwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:background="@drawable/search" 
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:height="40dip"
        android:hint="确认密码"
        />
      <Button 
          android:id="@+id/register_submit"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@drawable/topbg"
          android:height="40dip"
          android:width="70dip"
          android:layout_marginTop="60dip"
          android:text="确定"
          android:textColor="#FFFFFF"
          android:textSize="22dip"
      
          />
     
</LinearLayout>

处理注册页面的Activity:

package com.example.foreveross.office;
 
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 com.example.wenandroid.R;
import android.app.Activity;
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.Toast;
 
public class UserRegister extends Activity {
 
private EditText register_username;
private EditText register_passwd;
private EditText reregister_passwd;
private Button register_submit;
    @Override
    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.user_register);
        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);
        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(UserRegister.this, "用户名不能小于4个字符", Toast.LENGTH_SHORT).show();
                    }
                }
            }
            
        });
        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(UserRegister.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(UserRegister.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show(); 
                    }
                }
            }
            
        });
        register_submit.setOnClickListener(new OnClickListener(){
 
            @Override
            public void onClick(View v) {
                
                if(!checkEdit()){
                    return;
                }
                // TODO Auto-generated method stub
                String httpUrl="http://192.168.1.100:8080/web-test/register.jsp";
                HttpPost httpRequest=new HttpPost(httpUrl);
                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;
                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(UserRegister.this, strResult, Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(UserRegister.this, "请求错误", Toast.LENGTH_SHORT).show();
                }
                
            }
            
        });
    }
    
    private boolean checkEdit(){
        if(register_username.getText().toString().trim().equals("")){
            Toast.makeText(UserRegister.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
        }else if(register_passwd.getText().toString().trim().equals("")){
            Toast.makeText(UserRegister.this, "密码不能为空", Toast.LENGTH_SHORT).show();
        }else if(!register_passwd.getText().toString().trim().equals(reregister_passwd.getText().toString().trim())){
            Toast.makeText(UserRegister.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
        }else{
            return true;
        }
        return false;
    }
    
}

登录页面:

user_login.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" 
    android:background="@drawable/bg_01">
    
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"
        android:textSize="22dip"
        android:textColor="#FFFFFF"
        android:paddingLeft="140dip"
        android:paddingRight="50dip"
        android:paddingTop="10dip"
        android:background="@drawable/topbg"
        />
    
    <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="用户名"
        android:paddingTop="10dip"
        android:textSize="18dip"
        android:background="@drawable/search">
            
        </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="密码"
        android:background="@drawable/search">
            
        </EditText>
    </LinearLayout>
 
     <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="15dip">
 
         <CheckBox
             android:id="@+id/cb1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_marginLeft="50dip"
             android:layout_marginRight="30dip"
             android:text="记住密码" 
             android:button="@drawable/checkbox_icon_no" />"
         <CheckBox
             android:id="@+id/cb2"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="自动登录" 
             android:paddingRight="50dip"
             android:button="@drawable/checkbox_icon_no"/>
        </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:text="注册"
             android:layout_marginLeft="50dip"
             android:textColor="#F7FBFD"
             android:width="70dip"
             android:height="40dip"
             android:background="#0F9000"
             android:textSize="18dip"
             />
         
     </LinearLayout>
     
</LinearLayout>

登录页面Activity:

package com.example.foreveross.office;
 
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 com.example.wenandroid.R;
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.Toast;
 
public class UserLogin extends Activity implements OnClickListener {
private EditText login_username;
private EditText login_password;
private Button user_login_button;
private Button user_register_button;
 
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    setContentView(R.layout.user_login);
    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);
        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(UserLogin.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(UserLogin.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())
            {
                login();
            }
            
            break;
        case R.id.user_register_button:
            Intent intent2=new Intent(UserLogin.this,UserRegister.class);
            startActivity(intent2);
            break;
        }
    }
    
    private boolean checkEdit(){
        if(login_username.getText().toString().trim().equals("")){
            Toast.makeText(UserLogin.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
        }else if(login_password.getText().toString().trim().equals("")){
            Toast.makeText(UserLogin.this, "密码不能为空", Toast.LENGTH_SHORT).show();
        }else{
            return true;
        }
        return false;
    }
    
    private void login(){
        String httpUrl="http://192.168.1.102:8080/web-test/login.jsp";
        HttpPost httpRequest=new HttpPost(httpUrl);
        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(UserLogin.this, strResult, Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(UserLogin.this,IndexActivity.class);
            startActivity(intent);
        }
        else
        {
            Toast.makeText(UserLogin.this, "登录失败!", Toast.LENGTH_SHORT).show();
        }
        
    }
}

登录成功则跳转到IndexActivity.java

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

(0)

相关推荐

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

    上文中介绍了安卓客户端与服务器交互,实现注册功能,Android客户端实现注册/登录详解(一) 本文将继续介绍App与服务器的交互实现登录和自动登录的功能,上文说到请求服务器进行注册主要是通过POST请求携带参数实现,起作用的主要代码: StringRequest request=new StringRequest(Method.POST, url, new Listener<String>() { //请求成功 @Override public void onResponse(String

  • Android Studio连接SQLite数据库的登录注册实现

    1.先看一下项目目录: 2.新建一个AS项目,创建如上图所示的目录结构,然后添加内容: (1)修改添加布局文件: activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android&quo

  • Android开发之注册登录方法示例

    本文所述,继续上一篇关于Android端向服务器端发送数据的方法进一步完善注册登录的方法,由于版本问题出现一点瑕疵,今天经过调试已经解决,在这里给大家介绍一下. 在Android4.0以后版本的对于网络权限要求变得严格,致使上一篇所述的案例无法将数据发送到服务器端,当你一点击发送数据,Android控制台就会报错,错误当然是很让人头疼,基本上都是关于http的错误,所以可以肯定是Android虚拟机向服务器发送数据时出现了错误,经过一番检查与测试后才知道,4.0之后的版本,主线程中不允许调用网络

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

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

  • Android Studio连接MySql实现登录注册(附源代码)

    本文主要介绍了Android Studio连接MySql实现登录注册,分享给大家,具体如下: 一.创建工程 1.创建一个空白工程 2.随便起一个名称 3.设置网络连接权限 <uses-permission android:name="android.permission.INTERNET"/> 二.引入Mysql驱动包 1.切换到普通Java工程 2.在libs当中引入MySQL的jar包 将mysql的驱动包复制到libs当中 三.编写数据库和dao以及JDBC相关代码

  • Android实现闪屏及注册和登录界面之间的切换效果

    在没给大家介绍正文之前先给大家说下实现思路: 先分别实现闪屏.注册界面.登录界面的活动,再用Intent将相关的活动连接起来,实现不同活动之间的跳转.此次试验代码较多,我只列出主要代码,详细的代码可用底部的下载链接下载. 一.实验效果图: 二.主要代码: (1)WelcomeActivity.Java(这部分代码实现的是第一页的欢迎页面) package com.example.flashscreendemo; import android.app.Activity; import androi

  • 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

  • Android实现登录注册功能封装

    我们都知道Android应用软件基本上都会用到登录注册功能,那么对一个一个好的登录注册模块进行封装就势在必行了.这里给大家介绍一下我的第一个项目中所用到的登录注册功能的,已经对其进行封装,希望能对大家有帮助,如果有什么错误或者改进的话希望各位可以指出. 我们都知道登录注册系列功能的实现有以下几步: 注册账号 登录账号 (第三方账号登录) 记住密码 自动登录 修改密码 大体的流程如下 对于需要获取用户登录状态的操作,先判断用户是否已经登录. 如果用户已经登录,则继续后面的操作,否则,跳转到登录页面

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

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

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

    本文实例为大家分享了Android登录注册功能的具体代码,供大家参考,具体内容如下 展示效果 代码区 MainActivity(登录方法) public class MainActivity extends AppCompatActivity { @BindView(R.id.editText) EditText editText; @BindView(R.id.editText2) EditText editText2; @BindView(R.id.button) Button button

随机推荐