如何在出现进度对话框时在后台显示另一个活动?

时间:2012-09-11 07:31:30

标签: android view

我正在开发一款Android应用。 Activity显示Button 按下按钮时ProgressDialog应显示在ProgressDialog后面,应该有另一个应该打开的活动 我希望活动除了按钮之外的这个活动。我怎样才能做到这一点?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.gamemenu);


    TextView tvStart = (TextView)findViewById(R.id.start);
    //TextView tvSettings = (TextView)findViewById(R.id.settings);
    TextView tvAbout = (TextView)findViewById(R.id.instruction);
    TextView tvExit = (TextView)findViewById(R.id.exit);

    tvStart.setOnClickListener(this);
    //tvSettings.setOnClickListener(this);
    tvAbout.setOnClickListener(this);
    tvExit.setOnClickListener(this);

}

public void onClick(View v) {

    switch(v.getId()){

    case R.id.start:

        ProgressDialog pd=new ProgressDialog(StartMenu.this);  
        pd=ProgressDialog.show(StartMenu.this,"Loading","Please Wait",false);  
        Handler handler=new Handler();
           Runnable gotologin=new Runnable() 
           {
           public void run() 
           {
            startActivity(new Intent(StartMenu.this,AndroidGame.class));
            finish();
           }
           };
           handler.postDelayed(gotologin, 3000);       
           pd.dismiss();    
        //   finish();     
          break;
     // Intent iStart = new Intent(getApplicationContext(), AndroidGame.class );
    //  startActivity(iStart);
    //  break;

    /*case R.id.settings:
        Intent iSettings = new Intent(getApplicationContext(), GameSettings.class );
        startActivity(iSettings);
        break;*/

    case R.id.instruction:
        Intent inInstructions = new Intent(getApplicationContext(), Instructions.class );
        startActivity(inInstructions);
        break;

    case R.id.exit:
        ((Activity)v.getContext()).finish();
        //this.finish();

        break;
    }

}

2 个答案:

答案 0 :(得分:3)

您无法在后台加载另一个活动,就像属于上一个活动的窗口一样。

在您的情况下,我建议您在单击按钮时加载第二个活动。但是,在第二个活动中,只要您愿意,就显示所需的进度对话框。

答案 1 :(得分:1)

您可以通过这种方式显示ProgressDialog。但是,我不认为这个progressDialog会被视为新的Activity会在显示ProgressDialog之前加载到屏幕上。

ProgressDialog pd=new ProgressDialog(ActivityName.this);  
Button n=new Button(ActivityName.this);  
n.setOnClickListener(new OnClickListener() {  
@Override  
public void onClick(View v) {  

       pd=ProgressDialog.show(ActivityName.this,"Loading","Please Wait",false);  
       Intent i=new Intent(ActivityName.this,TargetActivityName.class)  
       startActivity(i);  
       pd.dismiss();    
       finish();     
   }  
});

如果您希望在点击时显示ProgressDialog一段时间以满足您的要求,您可以使用Handler来实现这一点,这会在预定的时间之后加载新的活动。

如果 3秒保持ProgressDialog的示例示例如下:

ProgressDialog pd=new ProgressDialog(ActivityName.this);  
Button n=new Button(ActivityName.this);  
n.setOnClickListener(new OnClickListener() {  
@Override  
public void onClick(View v) {  

   pd=ProgressDialog.show(ActivityName.this,"Loading","Please Wait",false);  

   Handler handler=new Handler();
   Runnable gotologin=new Runnable() 
   {
   public void run() 
   {
    startActivity(new Intent(ActivityName.this, TargetActivityName.class));
    finish();
   }
   };
   handler.postDelayed(gotologin, 3000);       
   pd.dismiss();    
   finish();     
  }  
});

必填答案:

ProgressDialog pd;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.gamemenu);


    pd=new ProgressDialog(StartMenu.this);   // Edited here


    TextView tvStart = (TextView)findViewById(R.id.start);
    //TextView tvSettings = (TextView)findViewById(R.id.settings);
    TextView tvAbout = (TextView)findViewById(R.id.instruction);
    TextView tvExit = (TextView)findViewById(R.id.exit);

    tvStart.setOnClickListener(this);
    //tvSettings.setOnClickListener(this);
    tvAbout.setOnClickListener(this);
    tvExit.setOnClickListener(this);

}

public void onClick(View v) {

    switch(v.getId()){

    case R.id.start:

         runOnUiThread(new Runnable() {

         @Override
           public void run() {
              pd=ProgressDialog.show(StartMenu.this,"Loading","Please Wait",false);  
              Handler handler=new Handler();
               Runnable gotologin=new Runnable() 
               {
                public void run() 
                {
                  startActivity(new Intent(StartMenu.this,AndroidGame.class));
                  finish();
                }
               };
              handler.postDelayed(gotologin, 3000);       
              pd.dismiss();    
              finish();  

            }
      });
      //     break;
     // Intent iStart = new Intent(getApplicationContext(), AndroidGame.class );
    //  startActivity(iStart);
    //  break;

    /*case R.id.settings:
        Intent iSettings = new Intent(getApplicationContext(), GameSettings.class );
        startActivity(iSettings);
        break;*/

    case R.id.instruction:
        Intent inInstructions = new Intent(getApplicationContext(), Instructions.class );
        startActivity(inInstructions);
        break;

    case R.id.exit:
        ((Activity)v.getContext()).finish();
        //this.finish();

        break;
    }

}