用户按图像按钮打开 URL,但不显示新窗口

如何使用户按下图像按钮并在不打开新窗口的情况下按下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链接时会发生什么的网页,只是它已被按下。


RISEBY
浏览 117回答 3
3回答

萧十郎

如果您通过意图调用URL,它将打开浏览器(意味着新选项卡),而不是这样,您可以使用Web视图在应用程序内加载URL在 Xml 布局中:&nbsp; &nbsp; <WebView&nbsp; xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; &nbsp;android:id="@+id/webview"&nbsp; &nbsp; &nbsp;android:layout_width="fill_parent"&nbsp; &nbsp; &nbsp; android:layout_height="fill_parent"&nbsp; &nbsp; &nbsp;/>在“活动”中:&nbsp; &nbsp;WebView browser = (WebView) findViewById(R.id.webview);&nbsp; &nbsp; &nbsp; &nbsp; String url = "http://www.google.com";&nbsp; &nbsp; &nbsp; &nbsp; browser .setWebViewClient(new MyBrowser());&nbsp; &nbsp; &nbsp; &nbsp; browser .getSettings().setLoadsImagesAutomatically(true);&nbsp; &nbsp; &nbsp; &nbsp; browser .getSettings().setJavaScriptEnabled(true);&nbsp; &nbsp; &nbsp; &nbsp; browser .setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);&nbsp; &nbsp; &nbsp; &nbsp; browser .loadUrl(url);&nbsp;private class MyBrowser extends WebViewClient {&nbsp; @Override&nbsp; public boolean shouldOverrideUrlLoading(WebView view, String url) {&nbsp; &nbsp; &nbsp;view.loadUrl(url);&nbsp; &nbsp; &nbsp;return true;&nbsp; }&nbsp;}

偶然的你

如果不想在外部浏览器中打开 URI,则应使用WebView您可以添加到布局中WebView<WebView&nbsp; &nbsp; android:id="@+id/webView"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"></WebView>然后在你的活动中:private WebViewClient webViewClient = new WebViewClient() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(view.getContext(), error.getDescription(), Toast.LENGTH_SHORT).show();&nbsp; &nbsp; }};按 ID 查找您的地址并设置要打开的地址WebViewwebView.setWebViewClient(webViewClient);webView.loadUrl(yourUrl);

哆啦的时光机

Intent&nbsp;intent&nbsp;=&nbsp;new&nbsp;Intent(Intent.ACTION_VIEW,&nbsp;uri); startActivity(intent);这将启动一个新活动。如果不想启动新活动,但希望该 URL 显示在当前活动中,则需要在布局中添加一个 web 视图,并在 web 视图中打开该 url,而不是调用 startActivity。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java