【问题标题】:How to show dummy progressbar for 2 second?如何显示虚拟进度条 2 秒?
【发布时间】:2013-09-21 20:45:53
【问题描述】:

我只想在新活动开始前显示进度条。当我从上一个活动开始活动时,新屏幕首先显示虚拟进度条(只有进度条的白屏),然后在 2 或 3 秒后启动新活动。

listCategory.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        Intent iMenuList = new Intent(thirdstep.this,  fifthscreen.class);
        startActivity(iMenuList);
    }
});

public class fifthscreen extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fifthscreen);
    }
}

【问题讨论】:

    标签: android android-activity android-progressbar


    【解决方案1】:

    你可以放置一个宽度和高度为fill_parent的imageView,并经常改变它的背景图片,这会产生进度条递增的错觉。

    用另一个布局膨胀你的布局,其中 imageView 的宽度和高度为 fill_parent,progressBar 的背景图像显示初始值。然后使用处理程序..

    new Handler().postDelayed(new Runnable() {
    
            @Override
            public void run() 
            {
                imageView.setBackgroundResource(R.drawable.progressImage2);
    
            }
        }, 3200);
    
    
    new Handler().postDelayed(new Runnable() {
    
        @Override
        public void run() 
        {
    
            Intent intent=new Intent(getApplicationContext(),SecondActivity.class);
            finish();
            startActivity(intent);
        }
    }, 5000);
    

    【讨论】:

      【解决方案2】:

      您可以使用以下处理程序进行尝试:

      listCategory.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> arg0, View arg1,
              int position, long arg3) {
      //Display your progressBar here
      Handler mHand  = new Handler();
          mHand.postDelayed(new Runnable() {
      
              @Override
              public void run() {
                  // TODO Auto-generated method stub
                  Intent iMenuList = new Intent(thirdstep.this,  fifthscreen.class);
                  //Dismiss progressBar here
                  startActivity(iMenuList);
              }
          }, 2000);   
          }
      }
      });
      

      您需要将此代码放入onItemClick 方法中。

      【讨论】:

      • 但我想当用户点击第三屏上的列表视图时首先显示空白屏幕,3 或 5 秒后只有进度条显示新活动
      • 我想在开始新活动之前先显示进度条几秒钟然后开始新活动
      • 如果你想要一个完全空白的屏幕,那么你应该创建一个仅用于显示进度条的虚拟活动。
      • @smartguy 当您将意图放入 Handler.postDelayed 中时,正如我在代码中显示的那样,它将以给定的延迟开始,即在我的示例代码中为 2000 毫秒,即 2 秒。在此延迟之后,它将调用新活动。当用户单击列表时启动进度条,并在调用处理程序内部的意图时将其关闭。
      • 但在您的代码中,您在上一个活动上启动进度条,当用户单击上一个活动中的新活动时,屏幕上不会显示任何内容,除了在进度条完成时间后带有进度条的空白屏幕开始新活动跨度>
      【解决方案3】:
       private class log_in extends AsyncTask<String,Void,String>{
              ProgressDialog pDialog;
              int a=0;
             boolean value1;
              @Override
               protected void onPreExecute(){
                  pDialog = new ProgressDialog(LoginActivity.this);
                  pDialog.setMessage("Authenticating user...");
                  pDialog.show();
               }
              @Override
              protected Boolean doInBackground(String... params) {
                 if(a>300)
                 {value1=true;}
                 else{a++;
                   value1=false;}
                  return value1;
              }
              protected void onPostExecute(Boolean params){
                  super.onPostExecute(params);
                  pDialog.dismiss();
                  if(params){
                      Intent intent=new Intent(LoginActivity.this,menu.class);
                      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                      LoginActivity.this.startActivity(intent);
                  }
                 else{
                     error.setText("Wrong password or username combination");
                  }
      
              }
      
           }
      

      只需致电new log_in().execute(); 您想调用下一个活动的地方

      【讨论】:

      • 所以在开始我的第五屏之前是显示进度条???完成进度条后开始新的活动??
      • 我没找到你,但在 Screen1 中调用 execute,你会看到进度条,然后 asyncTask 将切换到 screen2
      • 这个 prgressbar 运行了多长时间?告诉我另外 1 件事我看到了这个解决方案androidcookbook.com/Recipe.seam?recipeId=1599
      • 但是这个例子显示错误 The method Sleep(int) is undefined for the type new Runnable(){}
      • 我的代码中没有给出线程,但如果你想以这种方式使用它仍然可以使用
      【解决方案4】:

      好的,如果您的问题是活动需要很长时间才能显示,那么您可能希望在活动显示后膨胀 xml。

      步骤如下:

      1. 加载一个简单的 Activity 并将其显示在屏幕上
      2. 显示进度条
      3. 在 Activity 中添加包含您需要的所有组件的布局
      4. 管理其他初始化
      5. 隐藏进度条

      【讨论】:

      • 有没有类似firstactivity--->progressbaractivity--->second activity的代码???
      猜你喜欢
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      相关资源
      最近更新 更多