重新组合指令

时间:2014-08-21 14:43:50

标签: android progressdialog

在我的“首页”课程中,我有不同的点击这样(7或8):

public void onClickactu(View v) {
            //Init boite chargement
            final ProgressDialog pd = ProgressDialog.show(this, "", "Chargement en cours", true);
            setContentView(R.layout.activity_webapp);
            WebView myWebView = (WebView) findViewById(R.id.webView_web_app);
            myWebView.setWebViewClient(new WebViewClient() {  
                        // Disparirion boite quand page chargée 
                        @Override
                          public void onPageFinished(WebView view, String url) {    
                            pd.dismiss();
                          }
                        });
            myWebView.getSettings().setJavaScriptEnabled(true);
            myWebView.loadUrl("XXXXXXXXXX");

        }

        public void onClickagend(View v) {
            //Init boite chargement
            final ProgressDialog pd = ProgressDialog.show(this, "", "Chargement en cours", true);
            setContentView(R.layout.activity_webapp);
            WebView myWebView = (WebView) findViewById(R.id.webView_web_app);

            myWebView.setWebViewClient(new WebViewClient() {  
                // Disparirion boite quand page chargée 
                @Override
                  public void onPageFinished(WebView view, String url) {
                    pd.dismiss();
                  }
                });
            myWebView.loadUrl("XXXXXXXX");
            myWebView.getSettings().setJavaScriptEnabled(true);
        }

正如您所看到的,所有onClick void

上都有progressdialog

有没有办法重新组合所有这些,以便不将progressdialog放在每个onClick上?

希望我很清楚 感谢

2 个答案:

答案 0 :(得分:0)

这是你想要实现的目标吗?

OnClickListener listener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        //Init boite chargement
        final ProgressDialog pd = ProgressDialog.show(this, "", "Chargement en cours", true);
        setContentView(R.layout.activity_webapp);
        WebView myWebView = (WebView) findViewById(R.id.webView_web_app);
        myWebView.setWebViewClient(new WebViewClient() {  
                    // Disparirion boite quand page chargée 
                    @Override
                      public void onPageFinished(WebView view, String url) {    
                        pd.dismiss();
                      }
                    });
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.loadUrl("XXXXXXXXXX");
    }
};
button1.setOnClickListener(listener);
button1.setOnClickListener(listener);
// etc

答案 1 :(得分:0)

我最终做了这样的事情:

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

    public void ButtonOnClick(View v) {
        setContentView(R.layout.activity_webapp);
        final ProgressDialog pd = ProgressDialog.show(this, "", "Chargement en cours", true);
        WebView myWebView = (WebView) findViewById(R.id.webView_web_app);
        myWebView.setWebViewClient(new WebViewClient() {

            @Override
              public void onPageFinished(WebView view, String ur){ 
                pd.dismiss();
              }
            });
        myWebView.getSettings().setJavaScriptEnabled(true);
        switch (v.getId()) {
          case R.id.button1:
              myWebView.loadUrl("xx");
            break;
          case R.id.button2:
              myWebView.loadUrl("xx");
            break;
          case R.id.button3:
              myWebView.loadUrl("xx");
            break;
          case R.id.button4:
              myWebView.loadUrl("xx");
            break;
          case R.id.button5:
              myWebView.loadUrl("xx");
            break;
          case R.id.button6:
              myWebView.loadUrl("xx");
            break;
          case R.id.button10:
              myWebView.loadUrl("xx");
            break;
          case R.id.button8:
              myWebView.loadUrl("xx");
            break;
          case R.id.button9:
              myWebView.loadUrl("xx");
            break;

          }
    }

只需编辑android:onclick的XML文件,将所有这些设置为ButtonOnClick 这就是我想要的“重组” 谢谢你,你给了我一个提示: - )