Android 開發(fā)使用Activity實(shí)現(xiàn)加載等待界面功能示例
本文實(shí)例講述了Android 開發(fā)使用Activity實(shí)現(xiàn)加載等待界面功能。分享給大家供大家參考,具體如下:
實(shí)現(xiàn)加載等待界面我用了兩種方式,一種是用PopupWindow實(shí)現(xiàn),另一種便是用Activity實(shí)現(xiàn)。用PopupWindow實(shí)現(xiàn)方法請見我的另一篇博客:
android使用PopupWindow實(shí)現(xiàn)加載等待界面
好了,下面開始。先上效果:
基本原理就是在主界面點(diǎn)擊按鈕(以登錄按鈕為例)之后,打開一個(gè)新的Activity,此Activity以對話框形式展示。首先,主界面(一個(gè)登錄按鈕以及它的監(jiān)聽事件):
activity_main.xml
<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:tools='http://schemas.android.com/tools' android: android:layout_width='match_parent' android:layout_height='match_parent' android:paddingBottom='@dimen/activity_vertical_margin' android:paddingLeft='@dimen/activity_horizontal_margin' android:paddingRight='@dimen/activity_horizontal_margin' android:paddingTop='@dimen/activity_vertical_margin' android:orientation='vertical' tools:context='com.toprs.waitingpractice.MainActivity'> <Button android:text='登錄' android:layout_width='match_parent' android:layout_height='wrap_content' android:onClick='loginClick' android: /></LinearLayout>
MainActivity.java
package com.toprs.waitingpractice;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void loginClick(View v){ Intent intent = new Intent(); intent.setClass(MainActivity.this,WaitingActivity.class); startActivity(intent); }}
接下來是彈出的新Activity,新的Activity及其布局:
waiting_activity.xml
<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:tools='http://schemas.android.com/tools' android: android:layout_width='match_parent' android:layout_height='match_parent' android:paddingBottom='@dimen/activity_vertical_margin' android:paddingLeft='@dimen/activity_horizontal_margin' android:paddingRight='@dimen/activity_horizontal_margin' android:paddingTop='@dimen/activity_vertical_margin' android:orientation='vertical' tools:context='com.toprs.waitingpractice.MainActivity'> <Button android:text='登錄' android:layout_width='match_parent' android:layout_height='wrap_content' android:onClick='loginClick' android: /></LinearLayout>
WaitingActivity.java
package com.tow.waitingpractice;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.widget.Toast;/** * Created by 39867 on 2017/4/18. */public class WaitingActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.loading_activity); new Handler().postDelayed(new Runnable() { @Override public void run() { WaitingActivity.this.finish(); Toast.makeText(WaitingActivity.this, '登錄成功', Toast.LENGTH_SHORT).show(); } },2000); }}
OK,運(yùn)行一下試試吧。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章:
1. Gitlab CI-CD自動(dòng)化部署SpringBoot項(xiàng)目的方法步驟2. 解決android studio引用遠(yuǎn)程倉庫下載慢(JCenter下載慢)3. ajax請求添加自定義header參數(shù)代碼4. ASP基礎(chǔ)知識(shí)VBScript基本元素講解5. 基于javascript處理二進(jìn)制圖片流過程詳解6. Kotlin + Flow 實(shí)現(xiàn)Android 應(yīng)用初始化任務(wù)啟動(dòng)庫7. 使用Python和百度語音識(shí)別生成視頻字幕的實(shí)現(xiàn)8. 教你如何寫出可維護(hù)的JS代碼9. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)10. 使用python 計(jì)算百分位數(shù)實(shí)現(xiàn)數(shù)據(jù)分箱代碼
