ProgressDialog的用法示例
【摘要】
1.新建Android项目 ProgressDialog 在包andorid.huada下建文件ProgressDialogActivity.java
2.ProgressDialogActivity.java代码如下
package android.huada; import and...
1.新建Android项目 ProgressDialog 在包andorid.huada下建文件ProgressDialogActivity.java
2.ProgressDialogActivity.java代码如下
-
package android.huada;
-
-
-
import android.app.Activity;
-
import android.app.ProgressDialog;
-
import android.os.Bundle;
-
import android.os.Handler;
-
import android.os.Message;
-
-
public class ProgressDialogActivity extends Activity
-
{
-
/** Called when the activity is first created. */
-
-
-
ProgressDialog progressDialog;
-
-
-
public void onCreate(Bundle savedInstanceState)
-
{
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
-
-
progress("下载数据中", "请稍等......");
-
}
-
-
-
public void progress(String title, String message)
-
{
-
progressDialog = newProgressDialog(this);
-
updateThread thread = new updateThread();
-
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
-
progressDialog.setMessage(message);
-
progressDialog.setTitle(title);
-
progressDialog.setProgress(0);
-
progressDialog.setMax(100);
-
progressDialog.show();
-
thread.start();
-
-
-
}
-
-
-
Handler handler = new Handler()
-
{
-
-
-
@Override
-
public void handleMessage(Message msg)
-
{
-
// TODO Auto-generated method stub
-
if (msg.what >= 100)
-
{
-
progressDialog.cancel();
-
}
-
progressDialog.setProgress(msg.what);
-
super.handleMessage(msg);
-
}
-
-
-
};
-
-
-
class updateThread extends Thread
-
{
-
publicvoid run()
-
{
-
handler.sendEmptyMessage(0);
-
try
-
{
-
updateThread.sleep(1000);
-
handler.sendEmptyMessage(10);
-
updateThread.sleep(1000);
-
handler.sendEmptyMessage(20);
-
updateThread.sleep(1000);
-
handler.sendEmptyMessage(30);
-
updateThread.sleep(1000);
-
handler.sendEmptyMessage(40);
-
updateThread.sleep(1000);
-
handler.sendEmptyMessage(50);
-
updateThread.sleep(1000);
-
handler.sendEmptyMessage(60);
-
updateThread.sleep(1000);
-
handler.sendEmptyMessage(70);
-
updateThread.sleep(1000);
-
handler.sendEmptyMessage(80);
-
updateThread.sleep(1000);
-
handler.sendEmptyMessage(100);
-
updateThread.sleep(1000);
-
-
-
} catch (InterruptedException e)
-
{
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
-
-
}
-
};
-
}
3.配置文件main.xml代码如下
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
>
-
<TextView
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text="@string/hello"
-
/>
-
</LinearLayout>
4.此项目是通过线程及时修改UI,运行效果图如下
文章来源: panda1234lee.blog.csdn.net,作者:panda1234lee,版权归原作者所有,如需转载,请联系作者。
原文链接:panda1234lee.blog.csdn.net/article/details/8788191
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)