如何创建一个加载网页的android按钮?

时间:2019-03-04 06:15:55

标签: android

我是android应用程序的超级新手,我正在尝试创建一个仅在单击时加载网页的应用程序。 但是,我不断收到错误消息:can't find symbol variable URL

这是我的MainActivity代码:

  package com.example.logger;

    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;


    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
        public void browser1(View view) {
            Intent browserIntent=new Intent(Intent.ACTION_VIEW,url.parse("xxxxxxxxxxx"));
        startActivity(browserIntent);
      }
}

请帮忙弄清楚我的缺失。

2 个答案:

答案 0 :(得分:2)

应该为Uri,而不是url

 Intent browserIntent=new Intent(Intent.ACTION_VIEW,Uri.parse("xxxxxxx"));

确保顶部有import android.net.Uri

答案 1 :(得分:0)

  1. 在清单文件中添加权限

     <uses-permission android:name="android.permission.INTERNET" />
    
  2. 将此添加到xml文件中

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webview"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
    
  3. 在MainActivity中添加此

    public class MainActivity extends AppCompatActivity {
        ProgressDialog pd;
        WebView wv;
        String url="www.google .com";
    
        private ImageView back3;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_social_media);
            back3 = (ImageView) findViewById(R.id.back3);
            back3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
            wv=(WebView)findViewById(R.id.webview);
            pd = new ProgressDialog(SocialMedia.this);
            pd.setCancelable(true);
            pd.setMessage("Loading....");
            // pd.setIcon(R.drawable.load);
            // wv.getSettings().setLoadsImagesAutomatically(true);
            wv.setWebViewClient(new MyBrowser(pd));
            wv.getSettings().setJavaScriptEnabled(true);
    
            wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            wv.loadUrl(url);
        }
        public  class MyBrowser extends WebViewClient{
            ProgressDialog pd;
    
            public MyBrowser(ProgressDialog pd) {
                this.pd = pd;
                pd.show();
            }
    
    
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, request);
            }
    
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if(pd.isShowing()){
                    pd.dismiss();
                }
            }
    
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                Toast.makeText(getApplicationContext(),"Error"+description,Toast.LENGTH_SHORT).show();
            }
        }
    
    }