Android 用Activity实现加载等待界面

转载请标明出处:http://blog.csdn.net/wangtaocsdn/article/details/70990789

实现加载等待界面我用了两种方式,一种是用PopupWindow实现,另一种便是用Activity实现。用PopupWindow实现方法请见我的另一篇博客:

用PopupWindow实现加载等待界面

好了,下面开始。先上效果:

Android 用Activity实现加载等待界面_第1张图片

基本原理就是在主界面点击按钮(以登录按钮为例)之后,打开一个新的Activity,此Activity以对话框形式展示。首先,主界面(一个登录按钮以及它的监听事件):

activity_main.xml


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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:id="@+id/button2"/>
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


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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:id="@+id/button2"/>
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,运行一下试试吧。

你可能感兴趣的:(Android)