1. 程式人生 > >android -簡單註冊登入頁面應用

android -簡單註冊登入頁面應用

小白今天寫了一段註冊登入程式碼與大家分享不足請指出

首先寫一個javabean

package com.my.domain;

public class User {
private String account;//賬號
private String password;//密碼
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

public String toString() {
return "account=" + account + ", password=" + password ;
}
public User(){
super();
}


public User(String account ,String password){
super();
this.account=account;
this.password=password;
}


}

寫一個dao層

package com.my.db.service;
import com.my.domain.User;


import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


/**
 * 
 *登入使用者資料的操作
 *
 */


public class UserService {
private DBOpenHelper helper;// 建立DBOpenHelper物件
private SQLiteDatabase db;// 建立SQLiteDatabase物件
public UserService(Context cxt){
helper=new DBOpenHelper(cxt);// 初始化DBOpenHelper物件
}


 //登入用   
    public boolean login(String account,String psw){  
         db=helper.getReadableDatabase();  
        String sql1="select account,password from user where account=? and password=?";  
        Cursor cursor=db.rawQuery(sql1, new String[]{account,psw});         
        if(cursor.moveToFirst()==true){  
            cursor.close();  
            return true;  
        }  
        return false;  
    }  
    //註冊用   
    public boolean register(User user){  
        db=helper.getWritableDatabase();  
        String sql= "insert into user(account,password) values(?,?)"; 
        Object obj[]={user.getAccount().toString(),user.getPassword().toString()};  
        db.execSQL(sql, obj);    
        return true;  
       
    }  
   
    //檢查使用者名稱是否存在
    public boolean checkAccount(String account){
    db=helper.getReadableDatabase();
    String query="select * from user where account=? ";
    Cursor cursor=db.rawQuery(query, new String[]{account});
    if(cursor.moveToNext()){
    cursor.close();
    return true;
    }
    cursor.close();
    return false;
   
    }

}

註冊頁面

package com.activity;


import com.my.db.service.DBOpenHelper;
import com.my.db.service.UserService;
import com.my.domain.User;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


/**
 * 
 * @author  註冊頁面
 *
 */
public class Register extends Activity {


public EditText enter_account, enter_psw, confirm_psw;
public String psw1;
public String psw2;
public String account;
UserService uService = new UserService(this);
User user = new User();


protected void onCreate(Bundle saBundle) {
super.onCreate(saBundle);
setContentView(R.layout.register);
// 獲取輸入賬號id
enter_account = (EditText) findViewById(R.id.register_enter_account);
// 獲取輸入密碼id
enter_psw = (EditText) findViewById(R.id.register_enter_psw);
// 獲取確認密碼id
confirm_psw = (EditText) findViewById(R.id.register_confirm_psw);


// 獲取註冊按鈕
Button sign_up = (Button) findViewById(R.id.sign_up);
// 新增事件
sign_up.setOnClickListener(new OnClickListener() {


public void onClick(View v) {


account = enter_account.getText().toString().trim();
psw1 = enter_psw.getText().toString().trim();
psw2 = confirm_psw.getText().toString().trim();
// 輸入賬號或者密碼為空時候不能登入
if ("".equals(account) || "".equals(psw1)) {
new AlertDialog.Builder(Register.this).setTitle("錯誤").setMessage("帳號或密碼不能空")
.setPositiveButton("確定", null).show();
}
// 判斷輸入賬號是否存在
else if (uService.checkAccount(account)) {
Toast.makeText(Register.this, "該使用者名稱已被註冊", Toast.LENGTH_SHORT).show();
enter_account.setText("");


}
// 如果兩次輸入密碼一樣就插入資料,
else if (psw1.equals(psw2)) {
user.setAccount(account);
user.setPassword(psw1);
uService.register(user);
Toast.makeText(Register.this, "註冊成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Register.this, Login.class);
startActivity(intent);
} else {
Toast.makeText(Register.this, "註冊失敗,兩次輸入密碼不一樣", Toast.LENGTH_SHORT).show();
enter_psw.setText("");
confirm_psw.setText("");
}


}
});
// 獲取重置按鈕並且新增事件
Button cz = (Button) findViewById(R.id.cz);
cz.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
enter_account.setText("");
enter_psw.setText("");
confirm_psw.setText("");


}
});


}


}

登入頁面

package com.activity;


import com.my.db.service.UserService;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


/**
 * 、
 * 
 * @author登入頁面
 *
 */


public class Login extends Activity {
public EditText login_edit_account, login_edit_pwd;
public String edit_account;
public String edit_pwd;
UserService uService = new UserService(Login.this);


protected void onCreate(Bundle saBundle) {
super.onCreate(saBundle);
setContentView(R.layout.login);
// 獲取註冊按鈕併為其新增事件
Button register = (Button) findViewById(R.id.register);
register.setOnClickListener(new OnClickListener() {


public void onClick(View v) {
// 點選按鈕 new一個意圖 開啟註冊頁面
Intent intent1 = new Intent(Login.this, Register.class);
startActivity(intent1);


}
});


login_edit_account = (EditText) findViewById(R.id.login_edit_account);
login_edit_pwd = (EditText) findViewById(R.id.login_edit_pwd);


// 獲取登入按鈕併為其繫結事件
Button login = (Button) findViewById(R.id.login);
login.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {


edit_account = login_edit_account.getText().toString().trim();
edit_pwd = login_edit_pwd.getText().toString().trim();
//判斷輸入賬號或者密碼是否為空
if (edit_account.equals("") || edit_pwd.equals("")) {
new AlertDialog.Builder(Login.this).setTitle("錯誤").setMessage("賬號或密碼不能為空")
.setPositiveButton("確定", null).show();
}
// 對輸入的值進行判斷,呼叫UserService login函式


else if (uService.login(edit_account, edit_pwd)) {
Toast.makeText(Login.this, "登入成功", Toast.LENGTH_SHORT).show();
Intent intent2 = new Intent(Login.this, MainActivity.class);
startActivity(intent2);


} else {
Toast.makeText(Login.this, "登入失敗,輸入賬號或者密碼錯誤,請重新輸入!", Toast.LENGTH_SHORT).show();
login_edit_account.setText("");
login_edit_pwd.setText("");
}
}
});


}


}

login.xml

<?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"
 android:background="@drawable/login_bj"


 >


 <RelativeLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:paddingBottom="10px"
     android:paddingTop="150px" >


     <!-- 登入左邊logo -->


     <ImageView
         android:id="@+id/faceImg"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="@drawable/dl_2" />


     <EditText
         android:id="@+id/login_edit_account"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_toRightOf="@+id/faceImg"
         android:hint="輸入帳號"
         android:paddingLeft="45sp"
         android:textColor="#ff0000" />


     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignBottom="@+id/login_edit_account"
         android:layout_alignLeft="@+id/login_edit_account"
         android:layout_alignTop="@+id/login_edit_account"
         android:layout_marginRight="15sp"
         android:gravity="center_vertical"
         android:paddingLeft="7sp"
         android:text="帳號:"
         android:textSize="10dp" />


     <EditText
         android:id="@+id/login_edit_pwd"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/login_edit_account"
         android:layout_alignRight="@+id/login_edit_account"
         android:layout_below="@+id/login_edit_account"
         android:hint="輸入密碼"
         android:paddingLeft="45sp"
          android:inputType="textPassword"
        android:textColor="#ff0000" />


     <TextView
         android:id="@+id/textView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignBottom="@+id/login_edit_pwd"
         android:layout_alignLeft="@+id/login_edit_pwd"
         android:layout_alignTop="@+id/login_edit_pwd"
         android:layout_marginRight="15.0sp"
         android:gravity="center_vertical"
         android:paddingLeft="7sp"
         android:text="密碼:"
         android:textSize="10dp" 
        />


    


     <Button
         android:id="@+id/register"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerVertical="true"
         android:layout_below="@+id/login_edit_pwd"
         android:layout_alignLeft="@+id/login_edit_pwd"
         android:text="注   冊"
         android:textColor="#FF0000" />
      <Button
         android:id="@+id/login"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@id/login_edit_pwd"
         android:layout_toRightOf="@id/register"
         android:text="登   錄"
         android:textColor="#FF0000" />


 </RelativeLayout>
 
 </LinearLayout>

register.xml


     <?xml version="1.0" encoding="utf-8"?>
<!-- 使用者註冊頁面 -->
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tablayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/login_bj" >


    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/sign1" />


    <TableRow
        android:id="@+id/tablerow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >


        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="賬  號:"
            android:textSize="15sp" />


        <EditText
            android:id="@+id/register_enter_account"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:hint="請輸入賬號"
            android:singleLine="true" />
    </TableRow>


    <TableRow
        android:id="@+id/tablerow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >


        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="密   碼:"
            android:textSize="15sp" />


        <EditText
            android:id="@+id/register_enter_psw"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:hint="請輸入密碼"
            android:inputType="textPassword"
            android:singleLine="true" />
    </TableRow>


    <TableRow
        android:id="@+id/tablerow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >


        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="確認密碼:"
            android:textSize="15sp" />


        <EditText
            android:id="@+id/register_confirm_psw"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:hint="請確認密碼"
            android:inputType="textPassword"
            android:singleLine="true" />
    </TableRow>


    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >


        <Button
            android:id="@+id/sign_up"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_weight="1"
            android:text="注   冊"
            android:textColor="#FF0000" />


        <Button
            android:id="@+id/cz"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="重   置"
            android:textColor="#FF0000" />
    </TableRow>


</TableLayout>

效果圖