如何在android中添加到ImageButton的url链接

时间:2016-08-16 15:47:18

标签: android webview imagebutton

我有一个Android网络视图应用程序,其中我添加了一些ImageButtons,现在我想添加到该ImageButton的URL链接,链接应该在webview中使用app打开。以下是我的代码

image_button.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"
    android:layout_above="@+id/adView">

    <ImageButton
    android:id="@+id/image_button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/_button"
        android:clickable="true"
        style="@style/Widget.AppCompat.Button"
       />
    <ImageButton
        android:id="@+id/image_button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/image_button"
        style="@style/Widget.AppCompat.Button"
        />
    <ImageButton
        android:id="@+id/image_button3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/image_button"
        style="@style/Widget.AppCompat.Button"
        />


</LinearLayout>

ImageButton.java

public class ImageButton扩展了AppCompatActivity {

DrawerLayout drawerLayout;
ActionBarDrawerToggle drawerToggle;
NavigationView navigation;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.image_button);
    initInstances();
}
public void image_button1 (View view) {
    goToUrl ( "http://google.com/");
}

public void image_button2 (View view) {
    goToUrl ( "http://google.com/");
}

private void goToUrl (String url) {
    Uri uriUrl = Uri.parse(url);
    Intent WebView = new Intent(Intent.ACTION_VIEW, uriUrl);
    startActivity(WebView;
}

3 个答案:

答案 0 :(得分:0)

你必须在你的xml中调用image_button1()和image_button2()方法,如android:onClick =“image_buton1”和android:onClick =“image_button2”

答案 1 :(得分:0)

您不需要Uri.parse或使用意图。您只需在Webview上调用loadUrl即可。 loadUrl方法为你处理后台的所有内容,所以如果它在主UI线程上调用它就可以了

除非您使用布局充气程序,否则您需要在xml中创建Web视图

<WebView
    android:id="@+id/web_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

在Java中

WebView webView = (WebView) findViewById(R.id.web_view);

然后在ImageButton的onClickListener中,只需调用webview.loadUrl(someUrl)

ImageButton imgButton = (ImageButton) findViewById(R.id.image_button);

imgButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.loadUrl("https://www.google.com");
            }
        });

最后,请确保您拥有AndroidManifest.xml中的权限

<uses-permission android:name="android.permission.INTERNET"/>

答案 2 :(得分:0)

您必须编写自己的网络视图客户端,客户端在应用中打开网址

//您的FirstActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.image_button);
    initInstances();
}
public void image_button1 (View view) {
    goToUrl ( "http://google.com/");
}

public void image_button2 (View view) {
    goToUrl ( "http://google.com/");
}

private void goToUrl (String url) {

    Intent intent = new Intent(FirstActivity.this, WebViewActivity.class);
    intent.putExtra("Url",url);
    startActivity(WebView;
}

//您的WebView活动  WebView webview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview_activity);

    //get url from intent 
    String url= getIntent().getStringExtra("Url");
    webview = (WebView)rootview.findViewById(R.id.webView);

     yelpWeb.getSettings().setBuiltInZoomControls(true);
        webview.getSettings().setSupportZoom(true);
        webview.getSettings().setLoadWithOverviewMode(true);
        webview.getSettings().setUseWideViewPort(true);
        webview.getSettings().setJavaScriptEnabled(true);

        webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webview.getSettings().setBuiltInZoomControls(true);
        webview.getSettings().setUseWideViewPort(true);
        webview.getSettings().setAllowFileAccess(true);
        webview.getSettings().setAllowContentAccess(true);
        if (Build.VERSION.SDK_INT >= 19) {
            webview.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        }
        else {
            webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        webview.setWebViewClient(new myWebClientTwo());
        webview.loadUrl(url); 
} 

public class myWebClientTwo extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
           // progressBar.setVisibility(View.VISIBLE);
            //progressBar.setProgress(0);
            SVProgressHUD.showInView(getActivity(), "Loading", true);
            super.onPageStarted(view, url, favicon);
        }

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


            SVProgressHUD.showInView(getActivity(), "Loading", true);
           // progressBar.setVisibility(View.VISIBLE);
            view.loadUrl(url);
            return true;

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
            //progressBar.setProgress(100);
            //progressBar.setVisibility(View.GONE);
            SVProgressHUD.dismiss(getActivity());
        }
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && yelpWeb.canGoBack()) {
            yelpWeb.goBack();
            return true;
        }
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return getActivity().onKeyDown(keyCode, event);
    }

在清单文件中添加Permision

<uses-permission android:name="android.permission.INTERNET"/>

@Sagar