Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android仿QQ登錄界面實現原理與完整代碼

編輯:Android 開發實例

今天我們拿騰訊 QQ 練練手,一起做一個既簡潔又漂亮的登錄界面。先看官方效果圖:

程序啟動後先進入啟動頁(SplashActivity),隨後跳轉到登錄頁。導航頁這裡就不展開了,大家可參考微信的實現。先看啟動頁的代碼:

package com.example.imitateqq;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class SplashActivity extends Activity {

    private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        startMainAvtivity();
    }

    private void startMainAvtivity() {
        new Handler().postDelayed(new Runnable() {
            public void run() {
                intent = new Intent(SplashActivity.this, QQ.class);
                startActivity(intent);
                SplashActivity.this.finish();
            }
        }, 1000);
    }
}

啟動頁的布局就是一張全屏圖,這裡有個關鍵點:必須設置 android:scaleType="matrix",否則圖片無法鋪滿全屏。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="matrix"
android:src="@drawable/splash" />
</LinearLayout>

一秒後進入登錄頁。不得不說,騰訊的 UI 做得相當考究,簡潔而不失美感。先拆解一下這個界面:根布局是藍色背景,而「QQ 2012 Android」那塊其實是一張背景色與根布局一致的圖片,這樣拼接起來毫無違和感。往下是兩個 EditText 輸入框——注意這裡和官方略有不同,右側有個小箭頭,點擊它會在第一個輸入框下方展開已保存的 QQ 號列表,並附帶刪除按鈕。再往下是登錄按鈕,以及「記住密碼」和「註冊新賬號」。最底部的「更多登錄選項」點擊後會向上彈出一塊隱藏布局——這裡我用顯示/隱藏一個布局來實現,比 SlidingDrawer 抽屜更省事。看代碼就一目了然了:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_bg" >

<ImageView
android:id="@+id/loginbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:src="@drawable/login_pic" />

<LinearLayout
android:id="@+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/loginbutton"
android:layout_marginLeft="28.0dip"
android:layout_marginRight="28.0dip"
android:background="@drawable/login_input"
android:orientation="vertical" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="44.0dip"
android:background="@drawable/login_input"
android:gravity="center_vertical"
android:orientation="horizontal" >

<EditText
android:id="@+id/searchEditText"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@null"
android:ems="10"
android:imeOptions="actionDone"
android:singleLine="true"
android:textSize="16sp" >
<requestFocus />
</EditText>

<Button
android:id="@+id/button_clear"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_marginRight="8dip"
android:background="@drawable/login_input_arrow"
android:visibility="visible" />
</LinearLayout>

<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/password"
android:layout_width="fill_parent"
android:layout_height="44.0dip"
android:background="#00ffffff"
android:gravity="center_vertical"
android:inputType="textPassword"
android:maxLength="16"
android:maxLines="1"
android:textColor="#ff1d1d1d"
android:textColorHint="#ff666666"
android:textSize="16.0sp" />
</LinearLayout>

<Button
android:id="@+id/buton1"
android:layout_width="270dp"
android:background="@drawable/chat_send_button_bg"
android:paddingTop="5.0dip"
android:layout_height="50dp"
android:layout_marginLeft="28.0dip"
android:layout_marginRight="28.0dip"
android:layout_marginTop="12.0dip"
android:layout_below="@+id/input"
android:gravity="center"
android:textSize="20dp"
android:text="登錄" />

<RelativeLayout
android:id="@+id/relative"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/input"
android:layout_alignRight="@+id/input"
android:layout_below="@id/buton1" >

<CheckBox
android:id="@+id/auto_save_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@null"
android:button="@null"
android:checked="true"
android:drawableLeft="@drawable/checkbox_bg1"
android:drawablePadding="4.0dip"
android:text="記住密碼"
android:textColor="#ffffffff"
android:textSize="12.0sp" />

<Button
android:id="@+id/regist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/login_reg_normal"
android:clickable="true"
android:gravity="left|center"
android:paddingLeft="8.0dip"
android:paddingRight="18.0dip"
android:text="註冊新賬號"
android:textColor="#ffffffff"
android:textSize="12.0sp" />
</RelativeLayout>

<LinearLayout
android:id="@+id/more_bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/login_moremenu_back"
android:orientation="vertical" >

<RelativeLayout
android:id="@+id/input2"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/login_moremenu_back"
android:orientation="vertical" >

<ImageView
android:id="@+id/more_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="5.0dip"
android:layout_toLeftOf="@+id/more_text"
android:clickable="false"
android:src="@drawable/login_more_up" />

<TextView
android:id="@+id/more_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@null"
android:gravity="center"
android:maxLines="1"
android:text="更多登錄選項"
android:textColor="#ffc6e6f9"
android:textSize="14.0sp" />
</RelativeLayout>

<LinearLayout
android:id="@+id/morehidebottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone" >

<View android:layout_width="fill_parent" android:layout_height="1.0px" android:background="#ff005484" />
<View android:layout_width="fill_parent" android:layout_height="1.0px" android:background="#ff0883cb" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30.0dip"
android:layout_marginRight="30.0dip"
android:layout_marginTop="12.0dip"
android:orientation="horizontal" >
<CheckBox android:id="@+id/hide_login" android:layout_width="1.0px" android:layout_height="wrap_content" android:layout_weight="2.0" android:background="@null" android:button="@null" android:checked="false" android:drawableLeft="@drawable/checkbox_bg1" android:drawablePadding="4.0dip" android:text="隱身登陸" android:textColor="#ffc6e6f9" android:textSize="12.0sp" />
<CheckBox android:id="@+id/silence_login" android:layout_width="1.0px" android:layout_height="wrap_content" android:layout_weight="1.0" android:background="@null" android:button="@null" android:checked="false" android:drawableLeft="@drawable/checkbox_bg1" android:drawablePadding="4.0dip" android:text="靜音登錄" android:textColor="#ffc6e6f9" android:textSize="12.0sp" />
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="18.0dip"
android:layout_marginLeft="30.0dip"
android:layout_marginRight="30.0dip"
android:layout_marginTop="18.0dip"
android:orientation="horizontal" >
<CheckBox android:id="@+id/accept_accounts" android:layout_width="1.0px" android:layout_height="wrap_content" android:layout_weight="2.0" android:background="@null" android:button="@null" android:checked="true" android:drawableLeft="@drawable/checkbox_bg1" android:drawablePadding="4.0dip" android:singleLine="true" android:text="允許手機/電腦同時在心線" android:textColor="#ffc6e6f9" android:textSize="12.0sp" />
<CheckBox android:id="@+id/accept_troopmsg" android:layout_width="1.0px" android:layout_height="wrap_content" android:layout_weight="1.0" android:background="@null" android:button="@null" android:checked="true" android:drawableLeft="@drawable/checkbox_bg1" android:drawablePadding="4.0dip" android:text="接受群消息" android:textColor="#ffc6e6f9" android:textSize="12.0sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>

各個組件本身並不難用,關鍵在於屬性的細節設置。想做出美觀的界面,靠的就是對這些屬性的反覆調校。建議大家拿這個例子當練習,好好體會一下 UI 設計。從中你能學到不少東西:比如 ViewGroup 的嵌套技巧、background 的設置,還有兩個 EditText 上下拼接時,給其中一個設 android:background="@null",就能消除多餘間隔,看起來像一體。這些東西得靠自己多做多試,時間長了自然就有手感了。

最後看下 MainActivity 的代碼,邏輯比較簡單:

package com.example.imitateqq;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class QQ extends Activity implements OnClickListener {

    private Button login_Button;
    private View moreHideBottomView, input2;
    private ImageView more_imageView;
    private boolean mShowBottom = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qq);
        initView();
    }

    private void initView() {
        login_Button = (Button) findViewById(R.id.buton1);
        login_Button.setOnClickListener(this);
        moreHideBottomView = findViewById(R.id.morehidebottom);
        more_imageView = (ImageView) findViewById(R.id.more_image);
        input2 = findViewById(R.id.input2);
        input2.setOnClickListener(this);
    }

    public void showBottom(boolean bShow) {
        if (bShow) {
            moreHideBottomView.setVisibility(View.GONE);
            more_imageView.setImageResource(R.drawable.login_more_up);
            mShowBottom = true;
        } else {
            moreHideBottomView.setVisibility(View.VISIBLE);
            more_imageView.setImageResource(R.drawable.login_more);
            mShowBottom = false;
        }
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.input2:
                showBottom(!mShowBottom);
                break;
            case R.id.buton1:
                showRequestDialog();
                break;
            default:
                break;
        }
    }

    private Dialog mDialog = null;
    private void showRequestDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
            mDialog = null;
        }
        mDialog = DialogFactory.creatRequestDialog(this, "正在驗證賬號...");
        mDialog.show();
    }
}

最終效果如下:

總結:這篇可以當作一個不錯的 UI 練習 Demo,建議大家動手獨立寫一遍,遇到問題可以留言交流。下一篇我會講主頁面的實現,敬請期待。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved