运行进度栏切换活动 [英] Run progress bar while switching Activity

查看:97
本文介绍了运行进度栏切换活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯卡在一个情况下,我从活动一切换到活动2。 我使用视频下载(5000)5秒钟后,开始另一个活动 但是,我要运行五秒钟进度条也睡的第一个活动 Pleaze帮我一下,当我点击第一个活动Next按钮进度条跑7​​68,16五秒钟 然后活动应加载 我的code是:

 公共类活动1扩展活动{
    公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    按钮旁边=(按钮)findViewById(R.id.B);
    最终进度P =(进度)findViewById(R.id.pr);
    next.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图查看){
              p.setVisibility(4);
            线程t =新的Thread();
            尝试{
                t.sleep(5000);

        }
            赶上(例外五){}

            意图myIntent =新的意图(view.getContext(),activity2.class);
            startActivityForResult(myIntent,0);
        }

    });

}}
 

解决方案

不要使用Thread.sleep()方法 - 它为根,以一切邪恶。相反,使用处理程序 postDelayed(Runnable的,时间) -method象下面这样:

 公共类活动1扩展活动{
  公共无效的onCreate(包savedInstanceState){
  super.onCreate(savedInstanceState);
  的setContentView(R.layout.main);
  按钮旁边=(按钮)findViewById(R.id.B);
  最终进度P =(进度)findViewById(R.id.pr);
  next.setOnClickListener(新View.OnClickListener(){
    公共无效的onClick(视图查看){
      p.setVisibility(4);

      最终的处理程序处理程序=新的处理程序();
      handler.postDelayed(新的Runnable(){
        @覆盖
        公共无效的run(){
          意图myIntent =新的意图(view.getContext(),activity2.class);
          startActivityForResult(myIntent,0);
        }
      },5000);
    }
  });
}
 

im stuck in a situation where i am switching from activity 1 to activity 2. i am using Thread.sleep(5000) to start another activity after 5 seconds But the progress bar which i want to run for five seconds also sleeps with the first activity Pleaze help me as to when i click next Button on first activity a progress bar shoud run for five sec and then activity should be loaded My Code is:

    public class Activity1 extends Activity  {          
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button next = (Button) findViewById(R.id.B);
    final ProgressBar  p=(ProgressBar) findViewById(R.id.pr);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
              p.setVisibility(4);
            Thread t=new Thread();
            try{                    
                t.sleep(5000);              

        }
            catch(Exception e){}

            Intent myIntent = new Intent(view.getContext(), activity2.class);
            startActivityForResult(myIntent, 0);
        }

    });   

}}

解决方案

Don't use Thread.sleep() - it is the root to all evil. Instead, use a Handler and its postDelayed( Runnable, time )-method like below:

public class Activity1 extends Activity  {          
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  Button next = (Button) findViewById(R.id.B);
  final ProgressBar  p=(ProgressBar) findViewById(R.id.pr);
  next.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
      p.setVisibility(4);

      final Handler handler = new Handler();
      handler.postDelayed(new Runnable() {
        @Override
        public void run() {
          Intent myIntent = new Intent(view.getContext(), activity2.class);
          startActivityForResult(myIntent, 0);
        }
      }, 5000);
    }
  });   
}

这篇关于运行进度栏切换活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆