From b7654c644c0d9e8d8bf255e7b10311ccf7993e00 Mon Sep 17 00:00:00 2001 From: kaixed Date: Tue, 3 Dec 2024 21:37:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0WebView=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/render.experimental.xml | 6 + app/src/main/AndroidManifest.xml | 3 + .../kaixed/kchat/ui/activity/TestActivity.kt | 69 +++++++---- .../kchat/ui/activity/WebViewActivity.kt | 110 ++++++++++++++++++ .../kaixed/kchat/ui/fragment/HomeFragment.kt | 1 + .../kaixed/kchat/ui/widget/CustomTitleBar.kt | 4 + .../kchat/ui/widget/MyBottomSheetFragment.kt | 1 + .../main/res/drawable/progress_bar_color.xml | 14 +++ app/src/main/res/layout/activity_test.xml | 16 ++- app/src/main/res/layout/activity_web_view.xml | 80 +++++++++++++ app/src/main/res/layout/view_webview.xml | 42 +++++++ 11 files changed, 315 insertions(+), 31 deletions(-) create mode 100644 .idea/render.experimental.xml create mode 100644 app/src/main/java/com/kaixed/kchat/ui/activity/WebViewActivity.kt create mode 100644 app/src/main/res/drawable/progress_bar_color.xml create mode 100644 app/src/main/res/layout/activity_web_view.xml create mode 100644 app/src/main/res/layout/view_webview.xml diff --git a/.idea/render.experimental.xml b/.idea/render.experimental.xml new file mode 100644 index 0000000..8ec256a --- /dev/null +++ b/.idea/render.experimental.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 763112a..83b441a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -39,6 +39,9 @@ android:theme="@style/Theme.KChatAndroid" android:usesCleartextTraffic="true" tools:targetApi="31"> + diff --git a/app/src/main/java/com/kaixed/kchat/ui/activity/TestActivity.kt b/app/src/main/java/com/kaixed/kchat/ui/activity/TestActivity.kt index 96f0deb..3553000 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/activity/TestActivity.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/activity/TestActivity.kt @@ -1,19 +1,27 @@ package com.kaixed.kchat.ui.activity import android.os.Bundle +import android.text.Spannable +import android.text.SpannableString +import android.text.style.ClickableSpan +import android.text.util.Linkify +import android.util.Patterns import android.view.View +import android.webkit.WebChromeClient +import android.webkit.WebResourceRequest +import android.webkit.WebView +import android.webkit.WebViewClient +import android.widget.ProgressBar +import android.widget.TextView import androidx.activity.enableEdgeToEdge -import androidx.recyclerview.widget.LinearLayoutManager -import com.kaixed.kchat.R -import com.kaixed.kchat.data.model.dynamic.Comment -import com.kaixed.kchat.data.model.dynamic.FriendCircleItem import com.kaixed.kchat.databinding.ActivityTestBinding -import com.kaixed.kchat.ui.adapter.FriendCircleAdapter import com.kaixed.kchat.ui.base.BaseActivity +import java.util.regex.Pattern class TestActivity : BaseActivity() { + override fun inflateBinding(): ActivityTestBinding { return ActivityTestBinding.inflate(layoutInflater) } @@ -22,33 +30,44 @@ class TestActivity : BaseActivity() { super.onCreate(savedInstanceState) enableEdgeToEdge() - initView() + val text = "访问我们的站点 https://www.example.com/ 或者 http://www.abc.com" + val spannableString = SpannableString(text) + + // 使用正则表达式找到 URL + val urlPattern = Pattern.compile("(https?://[\\w\\-]+(?:\\.[\\w\\-]+)+[/#?]?.*)") + val matcher = urlPattern.matcher(text) + + // 创建可点击的 span + while (matcher.find()) { + val url = matcher.group() + spannableString.setSpan(object : ClickableSpan() { + override fun onClick(widget: View) { + // 这里可以做点击处理,例如打开 WebView 或外部浏览器 + println("点击了链接: $url") + } + }, matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) + } + + val textView: TextView = binding.textView + + textView.text = spannableString + textView.isClickable = true + textView.movementMethod = android.text.method.LinkMovementMethod.getInstance() } + private fun extractUrlsWithPatterns(text: String): List { + val matcher = Patterns.WEB_URL.matcher(text) + val urls = mutableListOf() - private fun initView() { + while (matcher.find()) { + urls.add(matcher.group()) + } + return urls } override fun initData() { -// PictureSelector.create(this) -// .openGallery(SelectMimeType.ofImage()) -// .setImageEngine(ImageEngines()) -// .forResult(object : OnResultCallbackListener { -// override fun onResult(result: ArrayList) { -// result.forEach { localMedia -> -// localMedia?.let { -// val imagePath = it.realPath -// toast("图片路径: $imagePath") -// } -// } -// } -// -// override fun onCancel() { -// toast("已取消") -// } -// }) - } + } \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/ui/activity/WebViewActivity.kt b/app/src/main/java/com/kaixed/kchat/ui/activity/WebViewActivity.kt new file mode 100644 index 0000000..4d74e63 --- /dev/null +++ b/app/src/main/java/com/kaixed/kchat/ui/activity/WebViewActivity.kt @@ -0,0 +1,110 @@ +package com.kaixed.kchat.ui.activity + +import android.os.Bundle +import android.view.View +import android.webkit.WebChromeClient +import android.webkit.WebResourceRequest +import android.webkit.WebView +import android.webkit.WebViewClient +import android.widget.ProgressBar +import androidx.activity.enableEdgeToEdge +import com.kaixed.kchat.databinding.ActivityWebViewBinding +import com.kaixed.kchat.ui.base.BaseActivity + +class WebViewActivity : BaseActivity() { + + private lateinit var webView: WebView + + private lateinit var progressBar: ProgressBar + + private var url = "" + + override fun inflateBinding(): ActivityWebViewBinding { + return ActivityWebViewBinding.inflate(layoutInflater) + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + enableEdgeToEdge() + + binding.ivSetting.setOnClickListener { + finish() + } + + webView = binding.webView + progressBar = binding.progressBar + + // 初始化 WebView + setupWebView() + + // 加载网页 + webView.loadUrl(url) + + setListener() + } + + private fun setListener() { + binding.ivBack.setOnClickListener { + if (webView.canGoBack()) { + webView.goBack() + } else { + finish() + } + } + } + + override fun initData() { + url = intent.getStringExtra("url") ?: "" + } + + private fun setupWebView() { + // 启用 JavaScript + webView.settings.javaScriptEnabled = true + + // 设置 WebViewClient 来处理页面跳转 + webView.webViewClient = object : WebViewClient() { + override fun onPageStarted( + view: WebView, + url: String, + favicon: android.graphics.Bitmap? + ) { + super.onPageStarted(view, url, favicon) + // 页面开始加载时显示 ProgressBar + progressBar.visibility = View.VISIBLE + } + + override fun onPageFinished(view: WebView, url: String) { + super.onPageFinished(view, url) + // 页面加载完成时隐藏 ProgressBar + progressBar.visibility = View.INVISIBLE + + binding.tvTitle.visibility = View.VISIBLE + binding.tvTitle.text = webView.title + binding.tvUrl.text = webView.url + binding.tvUrl.visibility = View.VISIBLE + } + + override fun shouldOverrideUrlLoading( + view: WebView, + request: WebResourceRequest + ): Boolean { + return false + } + } + + // 设置 WebChromeClient 监听进度变化 + webView.webChromeClient = object : WebChromeClient() { + override fun onProgressChanged(view: WebView, newProgress: Int) { + super.onProgressChanged(view, newProgress) + // 更新进度条的进度 + progressBar.progress = newProgress + + if (newProgress == 100) { + progressBar.visibility = View.INVISIBLE + } else { + progressBar.visibility = View.VISIBLE + } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/kaixed/kchat/ui/fragment/HomeFragment.kt b/app/src/main/java/com/kaixed/kchat/ui/fragment/HomeFragment.kt index ca950a8..a238783 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/fragment/HomeFragment.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/fragment/HomeFragment.kt @@ -32,6 +32,7 @@ import com.kaixed.kchat.ui.activity.AddFriendsActivity import com.kaixed.kchat.ui.activity.FriendCircleActivity import com.kaixed.kchat.ui.activity.SearchActivity import com.kaixed.kchat.ui.activity.TestActivity +import com.kaixed.kchat.ui.activity.WebViewActivity import com.kaixed.kchat.ui.adapter.ConversationAdapter import com.kaixed.kchat.ui.adapter.MyGridAdapter import com.kaixed.kchat.ui.base.BaseFragment diff --git a/app/src/main/java/com/kaixed/kchat/ui/widget/CustomTitleBar.kt b/app/src/main/java/com/kaixed/kchat/ui/widget/CustomTitleBar.kt index 938c906..a1b9edb 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/widget/CustomTitleBar.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/widget/CustomTitleBar.kt @@ -6,6 +6,7 @@ import android.graphics.Color import android.util.AttributeSet import android.view.LayoutInflater import android.view.View +import android.widget.ImageView import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.content.ContextCompat import com.kaixed.kchat.R @@ -26,6 +27,9 @@ class CustomTitleBar @JvmOverloads constructor( private var isBtnEnabled: Boolean = false + val ivSetting: ImageView + get() = binding.ivSetting + init { attrs?.let { val typedArray = context.obtainStyledAttributes(it, R.styleable.CustomTitleBar, 0, 0) diff --git a/app/src/main/java/com/kaixed/kchat/ui/widget/MyBottomSheetFragment.kt b/app/src/main/java/com/kaixed/kchat/ui/widget/MyBottomSheetFragment.kt index cec45bd..c405bae 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/widget/MyBottomSheetFragment.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/widget/MyBottomSheetFragment.kt @@ -3,6 +3,7 @@ package com.kaixed.kchat.ui.widget import android.content.Intent import android.os.Bundle +import android.text.util.Linkify import android.view.LayoutInflater import android.view.View import android.view.ViewGroup diff --git a/app/src/main/res/drawable/progress_bar_color.xml b/app/src/main/res/drawable/progress_bar_color.xml new file mode 100644 index 0000000..91f5d22 --- /dev/null +++ b/app/src/main/res/drawable/progress_bar_color.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_test.xml b/app/src/main/res/layout/activity_test.xml index 1e82704..f10dcff 100644 --- a/app/src/main/res/layout/activity_test.xml +++ b/app/src/main/res/layout/activity_test.xml @@ -2,16 +2,20 @@ - + android:layout_height="wrap_content" /> + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_web_view.xml b/app/src/main/res/layout/activity_web_view.xml new file mode 100644 index 0000000..f36da50 --- /dev/null +++ b/app/src/main/res/layout/activity_web_view.xml @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/view_webview.xml b/app/src/main/res/layout/view_webview.xml new file mode 100644 index 0000000..28cecf8 --- /dev/null +++ b/app/src/main/res/layout/view_webview.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + +