是否可以将代码分成两个类文件?

时间:2013-12-09 16:29:52

标签: android if-statement

我有一个项目,我想在其他6000个if语句中实现,但问题是我的类文件中出现错误。

以下是我收到错误1 http://pho.to/4I2Zh

的错误的屏幕截图

错误2 http://pho.to/4I2c8

有没有办法在两个类中拆分或分割我的else-if语句?或者从xml文件中调用它们?

这是我的班级文件

 public class WebViewActivity extends Activity {
    WebView web;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        web = (WebView) findViewById(R.id.webView1);
        web.setWebViewClient(new myWebClient());
        web.getSettings().setJavaScriptEnabled(true);
        int pos = getIntent().getIntExtra("key",0);
        if(pos==0)
        {
        web.loadUrl("file:///android_asset/1work1.html");
        }
        else if(pos==1)
        {
        web.loadUrl("file:///android_asset/work1.html");
        }  
        else if(pos==2)
        {
        web.loadUrl("file:///android_asset/work2.html");
        } 
        else if(pos==3)
        {
        web.loadUrl("file:///android_asset/work3.html");
        }  
      // similarly for 4 and 5 and so on.
    }

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            view.loadUrl(url);
            return true;

        }
        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

        }
    }
}

1 个答案:

答案 0 :(得分:5)

看起来您的代码可以简化为:

int pos = getIntent().getIntExtra("key",0);
if(pos==0)
{
    web.loadUrl("file:///android_asset/1work1.html");
}
else
{
    web.loadUrl("file:///android_asset/work" + pos + ".html");
}