android 单击文本视图时加载web视图

fbcarpbf  于 2023-01-19  发布在  Android
关注(0)|答案(2)|浏览(137)

我想在Android应用中点击TextView时加载WebView,该怎么办?
我试过这个:

//Get a reference to your WebView//
WebView webView = (WebView) findViewById(R.id.webview);
webView.setVisibility(View.VISIBLE);
//Specify the URL you want to display// 
webView.loadUrl("https://example.com");

XML代码是:

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

<TextView
    android:id="@+id/weblink"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:linksClickable="true"
    android:text="xxxxxxxxxxxxxxxxxxxx"
    android:textColor="@color/blue"
    android:textColorLink="@color/blue"
    />

但它不能正常工作。id为“weblink”的TextView是指定的TextView

zfycwa2u

zfycwa2u1#

下面的代码:

WebView webView = (WebView) findViewById(R.id.webview);
TextView weblink=findElementById(R.id.weblink);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
    weblink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            webView.setVisibility(View.VISIBLE);
            webView.loadUrl("https://example.com");
        }
    });
ccrfmcuu

ccrfmcuu2#

public static void setDescriptionWebView(final Context context, final String description, final WebView mWebView) {
    try {
        mWebView.setFocusableInTouchMode(FALSE);
        mWebView.setFocusable(FALSE);
        mWebView.getSettings().setDefaultTextEncodingName("UTF-8");

        WebSettings webSettings = mWebView.getSettings();
        Resources res = context.getResources();
        final int fontSize = res.getInteger(R.integer.font_size);
        webSettings.setDefaultFontSize(fontSize);

        final String mimeType = "text/html; charset=UTF-8";
        final String encoding = "utf-8";
        final String rtl = new SessionManager(context).isRTLOn() ? "dir=\"rtl\"" : "";

        final String text = "<html><head>"+

                "<style type=\"text/css\">" +
                "@media (prefers-color-scheme: dark) { " +
                "body { background-color: transparent; color: white; } " +
                "table, th, td { border:1px solid black; }" +
                "table { border-collapse: collapse; width: 100%; }" +
                "th, td { text-align: left; padding: 8px; border: 1px solid;}" +
                "}" +

                "@media (prefers-color-scheme: light) { " +
                "body { background-color: transparent; color: black;  } " +
                "table, th, td { border:1px solid black; }" +
                "table { border-collapse: collapse; width: 100%; }" +
                "th, td { text-align: left; padding: 8px; border: 1px solid;}" +
                "}" +

                "</style>" +
                "</head><body " + rtl + ">" + description + "</body></html>";

        mWebView.loadDataWithBaseURL(null, text, mimeType, encoding, null);
    } catch (Exception e) {
        Utils.getErrors(e);
    }
}

相关问题