先看效果圖!
話不多說,直接上代碼!
//佈局文件:activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<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" />
<!-- <Button
android:id="@+id/bt_pwd_eye"
android:layout_width="23dp"
android:layout_height="23dp"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="33dp"
android:background="@drawable/login_illegal" />-->
</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
android:id="@+id/remember_layout000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="1dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" >
<Button
android:id="@+id/registfer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:singleLine="true"
android:text="AnyLife,Make the world a better place."
android:textSize="16sp"
android:visibility="visible" />
</RelativeLayout>-->
</RelativeLayout>
//佈局文件:activity_regist.xml
<?xml version="1.0" encoding="utf-8"?>
<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();
}
}
以上,希望對你有用!