亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用戶按圖像按鈕打開 URL,但不顯示新窗口

用戶按圖像按鈕打開 URL,但不顯示新窗口

RISEBY 2022-09-28 16:13:58
如何使用戶按下圖像按鈕并在不打開新窗口的情況下按下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鏈接時會發生什么的網頁,只是它已被按下。
查看完整描述

3 回答

?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

如果您通過意圖調用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"

     />

在“活動”中:


   WebView browser = (WebView) findViewById(R.id.webview);


        String url = "http://www.google.com";

        browser .setWebViewClient(new MyBrowser());

        browser .getSettings().setLoadsImagesAutomatically(true);

        browser .getSettings().setJavaScriptEnabled(true);

        browser .setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        browser .loadUrl(url);


 private class MyBrowser extends WebViewClient {

  @Override

  public boolean shouldOverrideUrlLoading(WebView view, String url) {

     view.loadUrl(url);

     return true;

  }

 }


查看完整回答
反對 回復 2022-09-28
?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

如果不想在外部瀏覽器中打開 URI,則應使用WebView


您可以添加到布局中WebView


<WebView

    android:id="@+id/webView"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

</WebView>

然后在你的活動中:


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();

    }

};

按 ID 查找您的地址并設置要打開的地址WebView


webView.setWebViewClient(webViewClient);

webView.loadUrl(yourUrl);


查看完整回答
反對 回復 2022-09-28
?
哆啦的時光機

TA貢獻1779條經驗 獲得超6個贊

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

這將啟動一個新活動。如果不想啟動新活動,但希望該 URL 顯示在當前活動中,則需要在布局中添加一個 web 視圖,并在 web 視圖中打開該 url,而不是調用 startActivity。


查看完整回答
反對 回復 2022-09-28
  • 3 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號