Webview在onClick之前加载数据

时间:2014-09-17 08:10:39

标签: android webview background

我的项目就像

一样
  1. 显示活动主要和我超过13年前按钮。如果你点击按钮打开网站一些恐怖片。
  2. 打开网页浏览并加载网站。
  3. 现在我在这里失败了。用户点击按钮并等待5-6秒?我想这样做

    1. 显示activity_main并单击按钮(并加载网站背景)
    2. 显示webview没有等待页面已加载!
    3. Activity_main(第一个)

      public class MainActivity extends Activity {
      
      
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);        
      
          Button btn = (Button) findViewById(R.id.button1);                
      btn.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
               Intent it=new Intent(new Intent(MainActivity.this, web.class)) ;   
               startActivity(it);
          }
          });
      }
      

      web.class

      public class web extends Activity {     
          private WebView webView;
             private View mCustomView ;    
          private myWebViewClient mWebViewClient;
      
          /**
           * Called when the activity is first created.
           */
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.web);         
              webView = (WebView) findViewById(R.id.webView);
              mWebViewClient = new myWebViewClient();
              webView.setWebViewClient(mWebViewClient);    
              webView.getSettings().setJavaScriptEnabled(true);
              webView.getSettings().setAppCacheEnabled(true);       
              webView.getSettings().setSaveFormData(true);      
              webView.loadUrl("http:/zzzzzz..x.x.x.x..x.x");
      
              final ConnectivityManager connMgr = (ConnectivityManager) web.this
                      .getSystemService(Context.CONNECTIVITY_SERVICE);
      
              final android.net.NetworkInfo wifi = connMgr
                      .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
      
              final android.net.NetworkInfo mobile = connMgr
                      .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
      
              if (wifi.isConnected() || mobile.isConnected()) {
      
                  return;
      
          }else{
          setContentView(R.layout.main);
      
          }    
              }  
          @Override
          public boolean onKeyDown(int keyCode, KeyEvent event) {
              if (keyCode == KeyEvent.KEYCODE_BACK) {
      
      
                  if ((mCustomView == null) && webView.canGoBack()) {
                      webView.goBack();
                      return true;
                  } 
              }
              return super.onKeyDown(keyCode, event);
          } 
          class myWebViewClient extends WebViewClient {
              @Override
              public boolean shouldOverrideUrlLoading(WebView view, String url) {
                  return super.shouldOverrideUrlLoading(view, url);    //To change body of overridden methods use File | Settings | File Templates.
              }    
      }
      }
      

      活动主要(manitibo)

       <Button
              android:id="@+id/button1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true"
              android:layout_alignParentLeft="true"
              android:layout_alignParentRight="true"
              android:text="Onaylıyorum" />
      
          <TextView
              android:id="@+id/textView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_above="@+id/textView2"
              android:layout_centerHorizontal="true"
              android:text="Uyarı !"
              android:textAppearance="?android:attr/textAppearanceMedium" />
      
          <TextView
              android:id="@+id/textView2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_above="@+id/button1"
              android:layout_alignLeft="@+id/button1"
              android:text="Onaylıyorum butonuna bastığınızda 18 yaş ve üzeri olduğunuzu kabul etmiş sayılırsınız !"
              android:textAppearance="?android:attr/textAppearanceMedium" />
      <WebView
              android:id="@+id/webView"
              android:layout_height="match_parent"
              android:layout_width="match_parent"
              android:visibility="gone" />
          <TextView
              android:id="@+id/textView3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_above="@+id/textView1"
              android:layout_centerHorizontal="true"
              android:text="Destek: xxxxxxxxxxx"
              android:textAppearance="?android:attr/textAppearanceSmall" />
      
          <ImageView
              android:id="@+id/imageView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_above="@+id/textView3"
              android:layout_alignLeft="@+id/textView2"
              android:layout_alignParentTop="true"
              android:layout_alignRight="@+id/textView2"
              android:layout_marginTop="18dp"
              android:src="@drawable/asdaa" />
      
      </RelativeLayout>
      

1 个答案:

答案 0 :(得分:0)

我会做那样的事情:

public class MainActivity extends Activity
{
    // Layout elements
    private Button btn_aboveage = null;
    private WebView webView = null;
    private myWebViewClient mWebViewClient = null;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // Attach layout
        setContentView(R.layout.activity_main);

        // Retrieve layout elements
        btn_aboveage = (Button) findViewById(R.id.btn_aboveage);
        webView = (WebView) findViewById(R.id.webView);

        // Load page
        mWebViewClient = new myWebViewClient();
        webView.setWebViewClient(mWebViewClient);    
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAppCacheEnabled(true);       
        webView.getSettings().setSaveFormData(true);      
        webView.loadUrl("http:/zzzzzz..x.x.x.x..x.x");

        // Check Internet connectivity
        if (!hasInternet())
        {
            // TODO : Display alert dialog !
        }

        // Attach listener
        btn_aboveage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v)
            {
                webView.setVisibility(View.VISIBLE);
                btn_aboveage.setVisibility(View.VISIBLE);
                // TODO : Hide layout elements you don't want to be displayed while in webview mode.
            }
        });
    }

    private boolean hasInternet()
    {
        ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        return (wifi.isConnected() || mobile.isConnected());
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            if (webView != null && webView.getVisibility() == View.VISIBLE && webView.canGoBack())
            {
                webView.goBack();
                return true;
            } 
        }
        return super.onKeyDown(keyCode, event);
    }

    private static class myWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            return super.shouldOverrideUrlLoading(view, url);
        }
    }
}

最后我使用的布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <WebView
        android:id="@+id/webView"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:visibility="gone" />

    <Button
        android:id="@+id/btn_aboveage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignVertical="true"
        android:text="I'm above 13" />

</RelativeLayout>