android 加载或登录或其他需要等待情况下显示loading

主要代码

ProgressDialog progressDialog;

progressDialog = ProgressDialog.show(MainActivity.this, "", "正在登录...");

progressDialog.dismiss();


地址: http://thedevelopersinfo.wordpress.com/2009/10/16/showing-progressdialog-in-android-activity/


不翻墙也能看:

Showing ProgressDialog in Android Activity

 
 
 
 
 
 
3 Votes


We moved. Please visit this link for this post.

In situations when need to wait some operation it is good practice to notify user that operation is in progress.

For this cases in Android present several classes which can help with this. One of them I am going to demonstrate.
I will show how to use ProgressDialog class for showing progress dialog. I will show how to create preogress dialog with title and without title.

There are several show methods.

I will take this one:

ProgressDialog.show(Context context, CharSequence title, CharSequence message);

In my case I write

ProgressDialog.show(Main.this, "In progress", "Loading");

Now my progress dialog shows on activity
with_title

Okay. It is working.

What if I want to hide title? It is also very easy, just put empty string as title parameter. Example:

ProgressDialog.show(Main.this, "", "Loading...");

It is working very good.
without_title
Need to stop it:

progressDialog.dismiss();

Full code, for testing my words:

import android.app.Activity;

import android.app.ProgressDialog;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

/**

* Class for showing how to work with progress dialog

* @author FaYna Soft Labs

*/

public class Main extends Activity {

private Button clickBtn;

private ProgressDialog progressDialog;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

clickBtn = (Button) findViewById(R.id.click);

clickBtn.setText("Click me");

clickBtn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

//start the progress dialog

progressDialog = ProgressDialog.show(Main.this, "", "Loading...");

new Thread() {

public void run() {

try{

sleep(10000);

} catch (Exception e) {

Log.e("tag", e.getMessage());

}

// dismiss the progress dialog

progressDialog.dismiss();

}

}.start();

}

});

}

}

Progress dialog is one of UI classes which helps to notify user about long operations.

Personally I very often use this class.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值