Java 用户按“图像”按钮打开URL而不显示新窗口

Java 用户按“图像”按钮打开URL而不显示新窗口,java,android,android-intent,start-activity,Java,Android,Android Intent,Start Activity,如何使用户按下图像按钮并按下URL链接而不打开新窗口 private class HandleClick implements View.OnClickListener { public void onClick(View arg0) { if(arg0.getId()==R.id.imageButton){ ((TextView) findViewById(R.id.textView2)).setText("Pressed: " +

如何使用户按下图像按钮并按下URL链接而不打开新窗口

private class HandleClick implements View.OnClickListener {
      public void onClick(View arg0) {




        if(arg0.getId()==R.id.imageButton){
            ((TextView) findViewById(R.id.textView2)).setText("Pressed: " + ++howManyClicks1);
            Uri uri = Uri.parse("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=1");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);

        }
        else if (arg0.getId()==R.id.imageButton1){
            ((TextView) findViewById(R.id.textView3)).setText("Pressed: " + ++howManyClicks2);
            Uri uri = Uri.parse("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=2");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
        else if (arg0.getId()==R.id.imageButton2){
            ((TextView) findViewById(R.id.textView5)).setText("Pressed: " + ++howManyClicks3);
            Uri uri = Uri.parse("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=3");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);

        }
        else if (arg0.getId()==R.id.imageButton4){
            ((TextView) findViewById(R.id.textView6)).setText("Pressed: " + ++howManyClicks4);
            Uri uri = Uri.parse("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=4");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
              startActivity(intent);

        }
我希望用户在不打开新窗口的情况下按下图像按钮和URL, -澄清: 我不想向用户显示当他/她按下URL链接时会发生什么的网页,只想显示它已被按下

Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

这将开始一项新的活动。如果您不想启动新活动,但希望url显示在当前活动中,则需要在当前活动的布局中添加一个webview,并在webview中打开url,而不是调用startActivity。

如果您通过intent调用url,它将打开浏览器(表示新选项卡),相反,您可以使用web视图在应用程序内部加载URL

在Xml布局中:

    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/webview"
     android:layout_width="fill_parent"
      android:layout_height="fill_parent"
     />

如果不想在外部浏览器中打开URI,则应使用
WebView

您可以将
WebView
添加到布局中

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</WebView>
按id查找您的
WebView
,并设置要打开的地址

webView.setWebViewClient(webViewClient);
webView.loadUrl(yourUrl);

如果您通过intent调用URL,它将打开浏览器(表示“新建”选项卡),您可以使用web视图在应用程序中加载URL。我们需要更多说明(可能是一个示例?)您希望实现什么?目前还不清楚想要的效果是什么。@SebastianPakieła更新了想要的效果。用户应该看不到网站的URL,当用户按下图像按钮时,它应该只加载URL。因此,基本上你不应该称之为意图。您必须根据应用程序的外观创建新的片段/活动,并使用诸如毕加索/Glide/UIL之类的库加载图像。
private WebViewClient webViewClient = new WebViewClient() {
    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        Toast.makeText(view.getContext(), error.getDescription(), Toast.LENGTH_SHORT).show();
    }
};
webView.setWebViewClient(webViewClient);
webView.loadUrl(yourUrl);