登录+注册界面的实现(代码)

先看效果图!

登录+注册界面的实现(代码)_第1张图片 登录+注册界面的实现(代码)_第2张图片

话不多说,直接上代码!

//布局文件:activity_login.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fitsSystemWindows="true">

    <RelativeLayout
        android:id="@+id/login_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:gravity="center">

        <FrameLayout
            android:id="@+id/username_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="55dp"
            android:gravity="center">

            <EditText
                android:id="@+id/username"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:inputType="number"
                android:maxLength="20"
                android:paddingLeft="55dp"
                android:paddingRight="60dp"
                android:hint="6位数字账号"/>

            <ImageView
                android:layout_width="28dp"
                android:layout_height="28dp"
                android:layout_gravity="left|center_vertical"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:background="@drawable/login_username"
                android:visibility="visible" />

            <Button
                android:id="@+id/bt_username_clear"
                android:layout_width="23dp"
                android:layout_height="23dp"
                android:layout_gravity="right|center_vertical"
                android:layout_marginRight="10dp"
                android:background="@drawable/login_delete"
                android:visibility="invisible" />
        FrameLayout>

        <FrameLayout
            android:id="@+id/usercode_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/username_layout"
            android:layout_marginTop="6dp"
            android:gravity="center">

            <EditText
                android:id="@+id/password"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:maxLength="20"
                android:paddingLeft="55dp"
                android:paddingRight="60dp"
                android:hint="6位密码"/>

            <ImageView
                android:layout_width="28dp"
                android:layout_height="28dp"
                android:layout_gravity="left|center_vertical"
                android:layout_marginLeft="7dp"
                android:layout_marginStart="7dp"
                android:background="@drawable/login_password" />

            <Button
                android:id="@+id/bt_pwd_clear"
                android:layout_width="23dp"
                android:layout_height="23dp"
                android:layout_gravity="right|center_vertical"
                android:layout_marginRight="10dp"
                android:background="@drawable/login_delete"
                android:visibility="invisible" />



        FrameLayout>

        <Button
            android:id="@+id/login"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/usercode_layout"
            android:layout_marginTop="30dp"
            android:background="#ff336699"
            android:gravity="center"
            android:text="登 录"
            android:textColor="@android:color/white"
            android:textSize="24dp" />
        android:layout_height="@dimen/default_button_height"
    RelativeLayout>

    <Button
        android:id="@+id/register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:background="#00000000"
        android:text="注册"
        android:textSize="16sp"
        android:textColor="#63B8FF" />
        android:visibility="visible"

    <Button
        android:id="@+id/login_error"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="#00000000"
        android:text="忘记密码"
        android:textSize="16sp"
        android:textColor="#63B8FF"/>

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/login_layout"
        android:layout_alignStart="@+id/login_layout"
        android:layout_below="@+id/login_layout"
        android:layout_marginTop="28dp"
        android:text="记住密码" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自动登录"
        android:id="@+id/checkBox2"
        android:layout_below="@+id/checkBox1"
        android:layout_alignLeft="@+id/checkBox1"
        android:layout_alignStart="@+id/checkBox1" />

    
RelativeLayout>
//布局文件:activity_regist.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout
        android:id="@+id/regist_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:gravity="center">

        <FrameLayout
            android:id="@+id/username_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="55dp"
            android:gravity="center">

            <EditText
                android:id="@+id/username_regist"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:hint="6位数字账号"
                android:inputType="number"
                android:maxLength="20"
                android:paddingLeft="90dp"
                android:paddingRight="60dp" />

            <Button
                android:id="@+id/bt_username_clear_regist"
                android:layout_width="23dp"
                android:layout_height="23dp"
                android:layout_gravity="right|center_vertical"
                android:layout_marginRight="10dp"
                android:background="@drawable/login_delete"
                android:visibility="invisible" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="注册账号:"
                android:textSize="16dp"
                android:textColor="#333333"
                android:layout_marginLeft="8dp"
                android:layout_gravity="left|center_vertical" />
        FrameLayout>

        <FrameLayout
            android:id="@+id/usercode_layout_1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/username_layout"
            android:layout_marginTop="6dp"
            android:gravity="center">

            <EditText
                android:id="@+id/password_regist_1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:maxLength="20"
                android:paddingLeft="90dp"
                android:paddingRight="60dp"
                android:hint="6位密码"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="输入密码:"
                android:textSize="@dimen/activity_vertical_margin"
                android:layout_marginLeft="8dp"
                android:layout_gravity="left|center_vertical"
                android:textColor="#333333"/>

            <Button
                android:id="@+id/bt_pwd_clear_regist_1"
                android:layout_width="23dp"
                android:layout_height="23dp"
                android:layout_gravity="right|center_vertical"
                android:layout_marginRight="10dp"
                android:background="@drawable/login_delete"
                android:visibility="invisible" />

        FrameLayout>

        <FrameLayout
            android:id="@+id/usercode_layout_2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/usercode_layout_1"
            android:layout_marginTop="6dp"
            android:gravity="center">

            <EditText
                android:id="@+id/password_regist_2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:maxLength="20"
                android:paddingLeft="90dp"
                android:paddingRight="60dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="重复密码:"
                android:textSize="@dimen/activity_vertical_margin"
                android:layout_marginLeft="8dp"
                android:layout_gravity="left|center_vertical"
                android:textColor="#333333"/>

            <Button
                android:id="@+id/bt_pwd_clear_regist_2"
                android:layout_width="23dp"
                android:layout_height="23dp"
                android:layout_gravity="right|center_vertical"
                android:layout_marginRight="10dp"
                android:background="@drawable/login_delete"
                android:visibility="invisible" />
        FrameLayout>

        <Button
            android:id="@+id/regist"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/usercode_layout_2"
            android:layout_marginTop="30dp"
            android:background="#ff336699"
            android:gravity="center"
            android:text="立即注册"
            android:textColor="@android:color/white"
            android:textSize="24dp" />
    RelativeLayout>

RelativeLayout>
//LoginActivity.java
public class LoginActivity extends Activity implements View.OnClickListener,View.OnLongClickListener {
    // 声明控件对象
    private EditText et_name, et_pass;
    private Button mLoginButton,mLoginError,mRegister,ONLYTEST;

    int selectIndex=1;
    boolean isReLogin=false;
    private RelativeLayout countryselect;
    private TextView coutry_phone_sn, coutryName;//

    private Button bt_username_clear;
    private Button bt_pwd_clear;
   // private Button bt_pwd_eye;
    //记录账号、密码
    private SharedPreferences sp_userinfo;

    private CheckBox cb_remenber;
    private CheckBox cb_autologin;
    private String userNameValue,passwordValue;

    private TextWatcher username_watcher;
    private TextWatcher password_watcher;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//  requestWindowFeature(Window.FEATURE_NO_TITLE);
//  //不显示系统的标题栏
//  getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN,
//    WindowManager.LayoutParams.FLAG_FULLSCREEN );

        setContentView(R.layout.activity_login);
        et_name = (EditText) findViewById(R.id.username);
        et_pass = (EditText) findViewById(R.id.password);

        bt_username_clear = (Button)findViewById(R.id.bt_username_clear);
        bt_pwd_clear = (Button)findViewById(R.id.bt_pwd_clear);
        //bt_pwd_eye = (Button)findViewById(R.id.bt_pwd_eye);
        bt_username_clear.setOnClickListener(this);
        bt_pwd_clear.setOnClickListener(this);
        //bt_pwd_eye.setOnClickListener(this);
        initWatcher();
        et_name.addTextChangedListener(username_watcher);
        et_pass.addTextChangedListener(password_watcher);

        mLoginButton = (Button) findViewById(R.id.login);
        mLoginError  = (Button) findViewById(R.id.login_error);
        mRegister    = (Button) findViewById(R.id.register);
        //ONLYTEST     = (Button) findViewById(R.id.registfer);
        //ONLYTEST.setOnClickListener(this);
        //ONLYTEST.setOnLongClickListener((OnLongClickListener) this);
        mLoginButton.setOnClickListener(this);
        mLoginError.setOnClickListener(this);
        mRegister.setOnClickListener(this);

        sp_userinfo = this.getSharedPreferences("userInfo", Context.MODE_PRIVATE);

        if(sp_userinfo.getBoolean("ISCHECK", false))
        {
            //设置默认是记录密码状态
            //rem_pw.setChecked(true);
            et_name.setText(sp_userinfo.getString("USER_NAME", ""));
            et_pass.setText(sp_userinfo.getString("PASSWORD", ""));
            //判断自动登陆多选框状态
            if(
                    //sp_userinfo.getBoolean("AUTO_ISCHECK", false)
                //测试,设为false
                false
                    )
            {
                //设置默认是自动登录状态
                //auto_login.setChecked(true);
                //跳转界面
                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                LoginActivity.this.startActivity(intent);

            }
        }

        cb_remenber = (CheckBox)findViewById(R.id.checkBox1);
        cb_autologin =(CheckBox)findViewById(R.id.checkBox2);
        CompoundButton.OnCheckedChangeListener listener1 = new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    //选中,保存密码
                    sp_userinfo.edit().putBoolean("ISCHECK", true).commit();
                    Toast.makeText(LoginActivity.this, compoundButton.getText()+"被选择",Toast.LENGTH_SHORT ).show();
                }else{
                    //取消选中,删除保存的密码
                    sp_userinfo.edit().putBoolean("ISCHECK", false).commit();
                    Toast.makeText(LoginActivity.this, compoundButton.getText()+"取消选择",Toast.LENGTH_SHORT ).show();
                }
            }
        };
        cb_remenber.setOnCheckedChangeListener(listener1);
        CompoundButton.OnCheckedChangeListener listener2 = new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    //选中,保存密码
                    sp_userinfo.edit().putBoolean("AUTO_ISCHECK", true).commit();
                    Toast.makeText(LoginActivity.this, compoundButton.getText()+"被选择",Toast.LENGTH_SHORT ).show();
                }else{
                    //取消选中,删除保存的密码
                    sp_userinfo.edit().putBoolean("AUTO_ISCHECK", false).commit();
                    Toast.makeText(LoginActivity.this, compoundButton.getText()+"取消选择",Toast.LENGTH_SHORT ).show();
                }
            }
        };
        cb_autologin.setOnCheckedChangeListener(listener2);
    }
    /**
     * 手机号,密码输入控件公用这一个watcher
     */
    private void initWatcher() {
        username_watcher = new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
            public void afterTextChanged(Editable s) {
                et_pass.setText("");
                if(s.toString().length()>0){
                    bt_username_clear.setVisibility(View.VISIBLE);
                }else{
                    bt_username_clear.setVisibility(View.INVISIBLE);
                }
            }
        };

        password_watcher = new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
            public void afterTextChanged(Editable s) {
                if(s.toString().length()>0){
                    bt_pwd_clear.setVisibility(View.VISIBLE);
                }else{
                    bt_pwd_clear.setVisibility(View.INVISIBLE);
                }
            }
        };
    }



    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {

            case R.id.login:  //登陆
                userNameValue = et_name.getText().toString();
                passwordValue = et_pass.getText().toString();
                //判断信息是否符合
                if (userNameValue.toString().length()==6 ) {
                    if (passwordValue.toString().length()==6){
                        if (cb_remenber.isChecked()) {
                            //记住用户名、密码、
                            SharedPreferences.Editor editor = sp_userinfo.edit();
                            editor.putString("USER_NAME", userNameValue);
                            editor.putString("PASSWORD", passwordValue);
                            editor.commit();
                        }
                        Intent intent_login = new Intent().setClass(LoginActivity.this, MainActivity.class);
                        //Intent intent_login = new Intent();
                        //intent_login.putExtra("fragment",0);
                        startActivity(intent_login);
                    }else{Toast.makeText(LoginActivity.this, "密码格式错误!", Toast.LENGTH_LONG).show();}
                }else{Toast.makeText(LoginActivity.this, "账号格式错误!", Toast.LENGTH_LONG).show();}
                break;

            case R.id.login_error: //无法登陆(忘记密码了吧)
                AlertDialog.Builder builder = new AlertDialog.Builder(this).
                        setTitle("忘记密码").
                        setIcon(R.drawable.login_illegal1).
                        setMessage("请拨打:  110 \n").
                        setPositiveButton("确定",null);
                builder.create().show();
                break;

            case R.id.register:    //注册新的用户
                Intent intent_regist = new Intent(LoginActivity.this, RegistActivity.class);
                startActivityForResult(intent_regist,0);//请求码为0
                break;

            case R.id.bt_username_clear:
                et_name.setText("");
                et_pass.setText("");
                break;

            case R.id.bt_pwd_clear:
                et_pass.setText("");
                break;
        }
    }

    //重写,回调方式来获取RegistActivity返回的数据

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0 && resultCode == 0){
            Bundle d = data.getExtras();
            String info = d.getString("info");
            et_name.setText(info);
        }
    }

    /**
     * 登陆
     */
    private void login() {
    }
    @Override
    public boolean onLongClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            //case R.id.registfer:
              //  if(SERVER_FLAG>9){

               // }
                //   SERVER_FLAG++;
              //  break;
        }
        return true;
    }


    /**
     * 监听Back键按下事件,方法2:
     * 注意:
     * 返回值表示:是否能完全处理该事件
     * 在此处返回false,所以会继续传播该事件.
     * 在具体项目中此处的返回值视情况而定.
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            if(isReLogin){
                Intent mHomeIntent = new Intent(Intent.ACTION_MAIN);
                mHomeIntent.addCategory(Intent.CATEGORY_HOME);
                mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                LoginActivity.this.startActivity(mHomeIntent);
            }else{
                LoginActivity.this.finish();
            }
            return false;
        }else {
            return super.onKeyDown(keyCode, event);
        }
    }

}
//RigistActivity.java
package com.example.businesstriphelper.login;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.businesstriphelper.R;

/**
 * Created by Administrator on 2016/7/5.
 */
public class RegistActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText et_name, et_pass1,et_pass2;
    private Button mRegistButton;

    private Button bt_username_clear;
    private Button bt_pwd_clear1,bt_pwd_clear2;
    private TextWatcher username_watcher;
    private TextWatcher password_watcher1,password_watcher2;
    //是否显示注册信息核对框
    private boolean isInfoChecked = false;
    private String userNameValue,passwordValue1,passwordValue2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_LEFT_ICON);
        setContentView(R.layout.activity_regiset);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("注册");

        et_name = (EditText) findViewById(R.id.username_regist);
        et_pass1 = (EditText) findViewById(R.id.password_regist_1);
        et_pass2 = (EditText) findViewById(R.id.password_regist_2);

        bt_username_clear = (Button) findViewById(R.id.bt_username_clear_regist);
        bt_pwd_clear1 = (Button) findViewById(R.id.bt_pwd_clear_regist_1);
        bt_pwd_clear2 = (Button) findViewById(R.id.bt_pwd_clear_regist_2);
        bt_username_clear.setOnClickListener(this);
        bt_pwd_clear1.setOnClickListener(this);
        bt_pwd_clear2.setOnClickListener(this);

        initWatcher();
        et_name.addTextChangedListener(username_watcher);
        et_pass1.addTextChangedListener(password_watcher1);
        et_pass2.addTextChangedListener(password_watcher2);

        mRegistButton = (Button) findViewById(R.id.regist);
        mRegistButton.setOnClickListener(this);

    }

    private void initWatcher() {
        username_watcher = new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
            public void afterTextChanged(Editable s) {
                isInfoChecked = false;

                if(s.toString().length()>0){
                    bt_username_clear.setVisibility(View.VISIBLE);
                }else{
                    bt_username_clear.setVisibility(View.INVISIBLE);
                }
            }
        };

        password_watcher1 = new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
            public void afterTextChanged(Editable s) {
                isInfoChecked = false;
                et_pass2.setText("");
                if(s.toString().length()>0){
                    bt_pwd_clear1.setVisibility(View.VISIBLE);
                }else{
                    bt_pwd_clear1.setVisibility(View.INVISIBLE);
                }
            }
        };
        password_watcher2 = new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
            public void afterTextChanged(Editable s) {
                isInfoChecked = false;
                if(s.toString().length()>0){
                    bt_pwd_clear2.setVisibility(View.VISIBLE);
                }else{
                    bt_pwd_clear2.setVisibility(View.INVISIBLE);
                }
            }
        };
    }

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.regist:
                userNameValue = et_name.getText().toString();
                passwordValue1 = et_pass1.getText().toString();
                passwordValue2 = et_pass2.getText().toString();
                //判断信息是否符合,
                //千万不要这样写,而是写一个判断方法,
                if (userNameValue.length()==6 ) {
                    if (passwordValue1.length()==6){
                        if (passwordValue1.equals(passwordValue2)){
                            if (isInfoChecked == false) {
                                isInfoChecked = true;
                                AlertDialog.Builder builder = new AlertDialog.Builder(this).
                                    setTitle("注册信息").
                                    setIcon(R.drawable.login_illegal1).
                                    setMessage("账号: " + et_name.getText().toString() + "\n密码: "
                                            + et_pass2.getText().toString()
                                            + "\n请再次点击“立即注册”").
                                    setPositiveButton("确定", null);
                                builder.create().show();
                            }else {
                                //注册,携带注册信息返回
                                Intent intent = getIntent();

                                intent.putExtra("info",userNameValue);
                                RegistActivity.this.setResult(0,intent);
                                RegistActivity.this.finish();
                                //Intent intent_login = new Intent().setClass(RegistActivity.this, MainActivity.class);
                                //startActivity(intent_login);
                            }
                        }else{Toast.makeText(RegistActivity.this, "两次密码不一致!"+"\n"+passwordValue1+"\n"+passwordValue2, Toast.LENGTH_LONG).show();}
                    }else{Toast.makeText(RegistActivity.this, "密码格式错误!", Toast.LENGTH_LONG).show();}
                } else{Toast.makeText(RegistActivity.this, "账号格式错误!", Toast.LENGTH_LONG).show();}

                break;


            case R.id.bt_username_clear_regist:
                et_name.setText("");
                break;
            case R.id.bt_pwd_clear_regist_1:
                et_pass1.setText("");
                et_pass2.setText("");
                break;
            case R.id.bt_pwd_clear_regist_2:
                et_pass2.setText("");
                break;

        }
    }

    //标题栏返回按键
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            //不反回,判断语句的问题
            //android.R 和R的区别:
            //android.R是要获取系统资源时使用的;
            //R当前应用程序中的资源。

            Intent intent = getIntent();

            intent.putExtra("info",et_name.getText().toString());
            RegistActivity.this.setResult(0,intent);
            RegistActivity.this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    //重写,防止按返回键崩溃
    @Override
    public void onBackPressed() {
        // super.onBackPressed();   不要调用父类的方法

        Intent intent = getIntent();
        intent.putExtra("info",et_name.getText().toString());
        RegistActivity.this.setResult(0,intent);
        RegistActivity.this.finish();
    }
}

以上,希望对你有用!

你可能感兴趣的:(demo)