您的位置:首页 > 移动开发 > Android开发

Android简单的自定义用户登陆对话框

2014-06-11 22:31 302 查看

 
         


二、涉及到的基本组件和原理

1、基本组件:AndroidUI、View、TextView、EditText、Button、CheckBox、动画Animation、线程Thread及Handler等。

2、原理:点击button前判断用户输入的用户名和密码是否为空,两个都不为空时执行登陆,启动登陆动画,在新线程中模拟从远程登陆的方法(程序中sleep(2000)假设验证登陆失败),刷新界面组件。

三、我的理解和想法,全在代码中.....

package com.ui;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import com.common.UIHelper;
import com.example.appmainui.R;
import com.widget.BaseActivity;

/**
* @name 自定义用户登陆对话框
* @descripation 填写用户名和密码后,执行登陆,点击注册按钮转注册页面
* @author 樊俊彬
* @date 2014-6-8
* @version 1.0
*/
public class LoginDialogActivity extends BaseActivity {

private EditText loginDialogInputUserName;
private EditText loginDialogInputPassword;
private Button loginDialogButtonRegist;
private CheckBox loginDialogCheckBoxRememberMe;
private Button loginDialogButtonLogin;
private Button loginDialogButtonCancel;
private ViewSwitcher loginDialogViewSwitcher;
private View loginDialogView;
private AnimationDrawable loginDialogAnimationLoading;
private boolean isLoginSuccess = false;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_dialog);
initUI();
}

/**
* 初始化界面组件
*/
public void initUI() {
// 加载界面组件
loginDialogInputUserName = (EditText) findViewById(R.id.loginDialogInputUserName);
loginDialogInputPassword = (EditText) findViewById(R.id.loginDialogInputPassword);
loginDialogButtonRegist = (Button) findViewById(R.id.loginDialogRegisterButton);
loginDialogCheckBoxRememberMe = (CheckBox) findViewById(R.id.loginDialogCheckBoxRememberMe);
loginDialogButtonLogin = (Button) findViewById(R.id.loginDialogButtonLogin);
loginDialogButtonCancel = (Button) findViewById(R.id.loginDialogButtonCancel);
loginDialogViewSwitcher = (ViewSwitcher) findViewById(R.id.loginDialogViewSwitcher);
loginDialogView = (View) findViewById(R.id.loginDialogViewLoading);

// 点击取消按钮时结束
loginDialogButtonCancel.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
LoginDialogActivity.this.finish();
}
});

// 点击登陆时执行登陆
loginDialogButtonLogin.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// 用户名和密码不能为空
String inputUserName = loginDialogInputUserName.getText()
.toString();
String inputPassword = loginDialogInputPassword.getText()
.toString();
if (!inputUserName.equals("")) {
if (!inputPassword.equals("")) {

// 执行登陆操作,开启登陆动画
loginDialogAnimationLoading = (AnimationDrawable) loginDialogView
.getBackground();
loginDialogAnimationLoading.start();
loginDialogViewSwitcher.showNext();
loginSystem(inputUserName, inputPassword);
} else {
Toast.makeText(LoginDialogActivity.this, "请输入密码",
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(LoginDialogActivity.this, "请输入用户名",
Toast.LENGTH_SHORT).show();
}
}
});

// 点击注册按钮时转注册页面
loginDialogButtonRegist.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
UIHelper.showRegistUserInfo(LoginDialogActivity.this);
}
});
}

/**
* 启动新线程验证用户名和密码,登陆到系统,同步刷新界面
*
* @param inputUserName
* @param inputPassword
*/
protected void loginSystem(String inputUserName, String inputPassword) {

// 刷新界面
final Handler handler = new Handler() {
public void handleMessage(Message msg) {

// 登陆成功
if (msg.what == 1) {
Toast.makeText(LoginDialogActivity.this, "登陆成功",
Toast.LENGTH_SHORT).show();
finish();
}
// 登陆失败
if (msg.what == 0) {
Toast.makeText(LoginDialogActivity.this, "登陆失败",
Toast.LENGTH_SHORT).show();
loginDialogViewSwitcher.showPrevious();
}
}
};

// 登陆验证线程
new Thread() {
@Override
public void run() {
super.run();
Message msg = new Message();
try {
// 验证用户名和密码
sleep(2000);
isLoginSuccess = false;

if (isLoginSuccess) {
// 登陆成功
msg.what = 1;
} else {
// 登陆失败
msg.what = 0;
}
} catch (InterruptedException e) {
e.printStackTrace();
msg.what = -1;
msg.obj = e;
}
handler.sendMessage(msg);
}
}.start();

}
}


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/logindialog_space"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ScrollView
android:id="@+id/login_scrollview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingLeft="14.0dip"
android:paddingRight="14.0dip"
android:paddingTop="14.0dip"
android:scrollbars="none" >

<ViewSwitcher
android:id="@+id/loginDialogViewSwitcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/login_dialog_background"
android:paddingBottom="00.0dip">

<!-- 账号与密码输入 -->

<LinearLayout
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="60.0dip"
android:background="@drawable/login_dialog_input"
android:orientation="vertical" >

<EditText
android:id="@+id/loginDialogInputUserName"
android:layout_width="fill_parent"
android:layout_height="44.0dip"
android:background="#00ffffff"
android:ems="10"
android:hint="点此输入账号/邮箱"
android:maxLength="16"
android:maxLines="1"
android:paddingLeft="12.0dip"
android:textColor="#ff1d1d1d"
android:textColorHint="#ff666666"
android:textSize="16.0sp" />

<View
android:layout_width="fill_parent"
android:layout_height="1.0px"
android:layout_marginLeft="1.0px"
android:layout_marginRight="1.0px"
android:background="#ffc0c3c4" />

<EditText
android:id="@+id/loginDialogInputPassword"
android:layout_width="fill_parent"
android:layout_height="43dp"
android:background="#00ffffff"
android:ems="10"
android:gravity="center_vertical"
android:hint="点此请输入密码"
android:inputType="textPassword"
android:maxLength="16"
android:maxLines="1"
android:paddingLeft="12.0dip"
android:textColor="#ff1d1d1d"
android:textColorHint="#ff666666"
android:textSize="16.0sp" />
</LinearLayout>

<RelativeLayout
android:id="@+id/layout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/layout1"
android:layout_marginBottom="00dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="0dip" >

<CheckBox
android:id="@+id/loginDialogCheckBoxRememberMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/login_dialog_checkbox_selector"
android:checked="true"
android:text="记住我的信息"
android:textColor="@color/black"
android:textSize="@dimen/text_size_14" />

<Button
android:id="@+id/loginDialogRegisterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dip"
android:background="@drawable/login_dialog_register_btn_selector"
android:clickable="true"
android:text="注册新账号"
android:textColor="@color/black"
android:textSize="@dimen/text_size_12" />
</RelativeLayout>

<RelativeLayout
android:id="@+id/layout3"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:layout_below="@+id/layout2"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip" >

<Button
android:id="@+id/loginDialogButtonLogin"
android:layout_width="140dip"
android:layout_height="40dip"
android:layout_alignParentLeft="true"
android:background="@drawable/login_dailog_button_selector"
android:text="登陆"
android:textColor="@color/black"
android:textSize="@dimen/text_size_20" />

<Button
android:id="@+id/loginDialogButtonCancel"
android:layout_width="140dip"
android:layout_height="40dip"
android:layout_alignParentRight="true"
android:background="@drawable/login_dailog_button_selector"
android:text="取消"
android:textColor="@color/black"
android:textSize="@dimen/text_size_20" />
</RelativeLayout>
</RelativeLayout>

<View
android:id="@+id/loginDialogViewLoading"
android:layout_width="135.0dip"
android:layout_height="135.0dip"
android:layout_gravity="center"
android:background="@anim/login_loading" />
</ViewSwitcher>
</ScrollView>

</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息