博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ProcessOnLoading
阅读量:5789 次
发布时间:2019-06-18

本文共 1929 字,大约阅读时间需要 6 分钟。

=========================================

3.18程序加载中,请稍候

ProgressDialog与线程整合使用.

效果图:

 

 

代码:

package edu.cqutzx.ProgramOnloading;

 

import android.app.Activity;

import android.app.ProgressDialog;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

 

publicclass ProgramOnLoadingActivity extends Activity {

    private Button btn;

    private TextView tv;

    public ProgressDialog pd = null;

    /** Called when the activity is first created. */

    @Override

    publicvoid onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        /* Find the object by ID.*/

        btn = (Button) findViewById(R.id.btn);

        tv = (TextView) findViewById(R.id.tv);

       

        /* Add a listener to the Button (btn) */

        btn.setOnClickListener(new Button.OnClickListener ()

        {

           @Override

           publicvoid onClick(View v)

           {

              /* Find the Strings in file strings.xml */

              final CharSequence strDialogTitle = getString(R.string.strDialogTitle);

              final CharSequence strDialogBody = getString(R.string.strDialogBody);

             

              /*Show Progreess Dialog. */       

              /*It's ok to write like this :*/

            pd = ProgressDialog.show(ProgramOnLoadingActivity.this,strDialogTitle, strDialogBody);

           //pd = ProgressDialog.show(ProgramOnLoadingActivity.this,strDialogTitle, strDialogBody,true);

           tv.setText(strDialogBody);

          

           new Thread()

           {

              @Override

              publicvoid run() {

                  try

                  {

                    

                     //System.out.println("aaaaaa");

                     sleep(10000);

                     //System.out.println("bbbbbb");

                     //Toast.makeText(getBaseContext(), strDialogBody, Toast.LENGTH_LONG).show();

                     //System.out.println("ccccc");

                  }

                  catch(Exception e)

                  {

                     e.printStackTrace();

                  }

                  finally

                  {

                     /* Let the dialog disappear .*/

                     pd.dismiss();

                    

                     /*Mobile tv had used this method: when EIXT the program, show a Dialog first

                      * and then kill the progrom.*/

                     //finish();

                  }

              }

             

           }.start();

           }       

        }

        );

       

    }

}

问题:

Can't create handler inside thread that has not called Looper.prepare()

解决办法:

总结:线程的使用

new Thread()

{

         Public void run()

{

         Tasktodo();

}

}

转载地址:http://tmlyx.baihongyu.com/

你可能感兴趣的文章
这些Java面试题必须会-----鲁迅
查看>>
Linux 常用命令
查看>>
CSS盒模型
查看>>
ng2路由延时加载模块
查看>>
使用GitHub的十个最佳实践
查看>>
脱离“体验”和“安全”谈盈利的游戏运营 都是耍流氓
查看>>
慎用!BLEU评价NLP文本输出质量存在严重问题
查看>>
JAVA的优势就是劣势啊!
查看>>
ELK实战之logstash部署及基本语法
查看>>
帧中继环境下ospf的使用(点到点模式)
查看>>
BeanShell变量和方法的作用域
查看>>
LINUX下防恶意扫描软件PortSentry
查看>>
由数据库对sql的执行说JDBC的Statement和PreparedStatement
查看>>
springmvc+swagger2
查看>>
软件评测-信息安全-应用安全-资源控制-用户登录限制(上)
查看>>
我的友情链接
查看>>
Java Web Application 自架构 一 注解化配置
查看>>
如何 debug Proxy.pac文件
查看>>
Python 学习笔记 - 面向对象(特殊成员)
查看>>
Kubernetes 1.11 手动安装并启用ipvs
查看>>