【问题标题】:create a ProgressDialog when i click on a button当我点击一个按钮时创建一个 ProgressDialog
【发布时间】:2012-07-05 20:14:45
【问题描述】:

我想在单击 à 按钮时弹出一个 ProgressDialog。通过单击此按钮,它开始解析 XML 文件并在 ListView 中显示结果。

我试过了,但它不起作用:

// --- NEWS BUTTON
    OnClickListener newsButtonListener = new OnClickListener()
    {

      @Override
      public void onClick(View actuelView)
      {

        // Navigation to another screen
        Intent intent = new Intent(Lausanne_HC.this, NewsViewController.class);
        startActivity(intent);
        ProgressDialog dialog = ProgressDialog.show(Lausanne_HC.this, "", 
                  "Loading. Please wait...", true); 

      }

   };
   ImageButton newsButton = (ImageButton) findViewById(R.id.newsButton);
     newsButton.setOnClickListener(newsButtonListener);

我能做什么?

谢谢

【问题讨论】:

标签: android onclick progressdialog


【解决方案1】:

按钮点击

Button click = (Button) findViewById(R.id.button1); 
        click.setOnClickListener(new OnClickListener() { 

            public void onClick(View v) { 
                // TODO Auto-generated method stub 
                openprogresdialog(); 
            } 
        }); 

创建一个 this 方法。

private void openprogresdialog() { 
        // TODO Auto-generated method stub 
        final ProgressDialog progDailog = ProgressDialog.show( 
                HidekeybordActivity.this, "Testing", "Please wait...", true); 

        new Thread() { 
            public void run() { 
                try { 
                    // xml parser code here put... 
                } catch (Exception e) { 
                } 
                progDailog.dismiss(); 
            } 
        }.start(); 
    } 

【讨论】:

    【解决方案2】:

    通过使用 Intent 打开另一个 Activity,Android 会离开当前的 Activity。如果您打开 Intent 的 Activity 是显示已解析数据的 Activity,您应该执行以下操作:

    1. 打开您的 ProgressDialog
    2. 启动一个新线程来解析您的 XML 文件
    3. 文件解析完成后,杀死 线程并使用 Intent 显示 数据

    我在几乎相同的主题上找到了this older thread

    【讨论】:

      【解决方案3】:

      您应该将生成ProgressDialog 的代码放在被调用活动的最开始。然后使用另一个线程解析您的 XML 文件并生成ListView,工作完成后,关闭ProgressDialog

      所有这些代码都应该放在显示ListView的Activity中,因为ProgreeDialog显示的是XML解析的进度。

      【讨论】:

        【解决方案4】:
        new Handler().postDelayed(new Runnable() {
        
                @Override
                public void run() {
        
                    /* Create an Intent that will start the Menu-Activity. */
        
                    Intent mainIntent = new Intent(SplashScreenActivity.this,
                            FastMainActivity.class);
        
                    SplashScreenActivity.this.startActivity(mainIntent);
                    //overridePendingTransition(android.R.anim.slide_in_left,
                        //  android.R.anim.slide_out_right);
        
                    SplashScreenActivity.this.finish();
        
                }
        
            }, SPLASH_DISPLAY_LENGHT); // SPLASH_DISPLAY_LENGTH=3000 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-08-15
          • 1970-01-01
          • 2016-11-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-25
          相关资源
          最近更新 更多