編輯:關於 Android 程式設計
本文將帶大家動手打造一個模仿新浪微博的啟動頁與登入頁,從開場動畫到登入按鈕的跳轉邏輯,一步步還原熟悉的 App 使用體驗,完整程式碼如下。
啟動界面
啟動頁主要肩負兩項任務:
1. 播放啟動動畫;
2. 偵測網路狀態,有網路就進入登入頁,沒有則引導使用者前往設定開啟網路。
實作起來並不複雜,主要運用 AlphaAnimation 搭配動畫監聽器,讓一張圖片產生透明度漸變的效果。在動畫啟動的瞬間先檢查網路,等到動畫結束再完成判斷並跳轉登入頁。
/** * Created by D&LL on 2016/5/25. * 初始頁面加載界面 */ public class SplashActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); ImageView splashimage = (ImageView) findViewById(R.id.splashimage); //透明度漸變動畫 AlphaAnimation animation = new AlphaAnimation(0.1f, 1.0f); //設置動畫時長 animation.setDuration(3000); //將組件和動畫進行關聯 splashimage.setAnimation(animation); animation.setAnimationListener(new Animation.AnimationListener() { //動畫啟動執行 @Override public void onAnimationStart(Animation animation) { Toast.makeText(SplashActivity.this, "歡迎使用DeMon制作微博", Toast.LENGTH_LONG).show(); //檢查並網絡的方法 Tools.checkNetWork(SplashActivity.this); } //動畫結束執行 @Override public void onAnimationEnd(Animation animation) { Intent intent = new Intent(SplashActivity.this, LoginActivity.class); startActivity(intent); finish(); } //動畫重復執行 @Override public void onAnimationRepeat(Animation animation) { } }); } 網路偵測的部分,先透過 ConnectivityManager 取得系統的連線服務,再取得代表連網狀態的 NetworkInfo 物件來判斷網路情況;若偵測不到網路,就彈出一個 AlertDialog 引導使用者前往設定。以下兩個方法皆位於 Tools.java 中。
/** * 設置網絡 * * @param context */ public static void checkNetWork(final SplashActivity context) { if (!NetWorkStatus(context)) { TextView msg = new TextView(context); msg.setText("請設置網絡!"); AlertDialog.Builder b = new AlertDialog.Builder(context). setIcon(R.drawable.notnet) .setTitle("沒有可用的網絡") .setMessage("是否對網絡進行設置?"); b.setPositiveButton("是", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //跳轉到設置網絡界面 context.startActivity(new Intent(Settings.ACTION_SETTINGS)); } }).setNeutralButton("否", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.cancel(); context.finish(); } }).create().show(); } } /** * 判斷網絡狀態 */ public static boolean NetWorkStatus(Context context) { //獲取系統的連接服務 ConnectivityManager connect = (ConnectivityManager) context.getSystemService(Context .CONNECTIVITY_SERVICE); if (connect == null) { return false; } else { // 獲取代表聯網狀態的NetWorkInfo對象,獲取網絡的連接情況 NetworkInfo[] infos = connect.getAllNetworkInfo(); if (infos != null) { for (NetworkInfo network : infos) { if (network.getState() == NetworkInfo.State.CONNECTED) { return true; } } } } return false; } 啟動頁 SplashActivity 的佈局檔 splash.xml 如下,畫面上放一張鋪滿螢幕的圖片,底部再擺一個載入進度條:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/splashimage" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitXY" android:src="@drawable/splashimage"/> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentStart="true" android:layout_gravity="center" android:layout_marginBottom="64dp" android:layout_marginStart="130dp"> </ProgressBar> </RelativeLayout>
登入界面
登入頁的結構相對簡單,主要由幾個按鈕組成,因此一併收錄在這篇文章中。其中微博登入按鈕按下後,會跳轉到授權界面。
public class LoginActivity extends Activity { private Button sinalogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); sinalogin = (Button) findViewById(R.id.sinalogin); sinalogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(LoginActivity.this, OAuthActivity.class); startActivity(intent); LoginActivity.this.finish(); } }); } } 佈局檔 login.xml 內有兩個自訂樣式的 EditText 與一個普通按鈕(此處僅作擺設),真正派上用場的只有微博登入按鈕。
<?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:background="@drawable/background" android:gravity="center_vertical" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="40dip" android:background="@drawable/bg_edittext" android:ems="10" android:inputType="textPersonName"> </EditText> <EditText android:id="@+id/passworld" android:layout_width="match_parent" android:layout_height="40dip" android:layout_marginTop="20dip" android:background="@drawable/bg_edittext" android:ems="10" android:inputType="textPassword"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/login" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_marginTop="20dip" android:layout_weight="1" android:text="登錄"/> <Button android:id="@+id/sinalogin" android:layout_width="200dp" android:layout_height="38dp" android:layout_marginTop="20dip" android:layout_weight="1" android:background="@drawable/weibologin"/> </LinearLayout> </LinearLayout>

以上便是啟動頁與登入頁的完整實作,希望對大家的學習有所幫助,也歡迎多多支持本站。
Android SQLite 資料庫儲存入門
SQLite 是 Android 上常用的本地關聯式資料庫。透過 SQLiteOpenHelper,開發者可以較方便地建立資料庫、管理版本,並執行基本資料操作。 建立資料
Google Play服務已停止運行解決方法
Google Play服務已停止運行解決方法 許多游戲與應用依賴Google Play服務,若出現停止運行提示,可能是服務框架缺失、版本衝突或文件權限異常。 處理思路
版本控制入門實戰:SVN 與 Git 的安裝、操作與衝突處理
版本控制這件事,一個人寫玩具專案時感覺不出價值,團隊協作第一天就會明白它為什麼不能少。這篇把 SVN 與 Git 兩套工具從安裝到實戰走一遍,當成課堂筆記的整理。 一、
Android 開發實戰:用 Intent 在頁面間傳遞數據
Intent 幾乎是每個 Android 開發者都會接觸到的核心類別,它承擔著在不同元件之間傳遞訊息與調度頁面的職責。本文就透過一個貼近實務的小範例,帶大家走一遍「點擊清