0

我正在开发一个 Android 应用程序。有一个Activity显示Button
当一个按钮被按下时,ProgressDialog应该出现并且后面ProgressDialog应该有另一个应该打开的 Activity。
我想要其他活动,而不是按钮所在的 Activity。我怎样才能做到这一点?

@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;
    }

}
4

2 回答 2

3

您不能像在属于上一个活动的窗口的上面那样在后台加载另一个活动。

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

于 2012-09-11T07:34:37.290 回答
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's 来实现,这将在预先确定的时间后加载新的 Activity。

将保持 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;
    }

}
于 2012-09-11T07:40:35.720 回答