Add back navigation support in WebViewActivity

- Fixed the implemented onBackPressed method to enable back navigation within the WebView.
This commit is contained in:
moreoronce 2024-10-07 17:44:41 +08:00
parent e835b5a1e4
commit 90cacdc914
5 changed files with 93 additions and 72 deletions

View file

@ -45,6 +45,7 @@ class WebViewActivity : AppCompatActivity() {
} }
} }
@SuppressLint("ClickableViewAccessibility")
private fun setupRefreshButton() { private fun setupRefreshButton() {
// 将按钮置于最前,并设置点击和拖动事件 // 将按钮置于最前,并设置点击和拖动事件
refreshButton.bringToFront() refreshButton.bringToFront()
@ -63,7 +64,7 @@ class WebViewActivity : AppCompatActivity() {
val webSettings: WebSettings = webView.settings val webSettings: WebSettings = webView.settings
webSettings.javaScriptEnabled = true webSettings.javaScriptEnabled = true
webSettings.domStorageEnabled = true webSettings.domStorageEnabled = true
webSettings.cacheMode = WebSettings.LOAD_DEFAULT webSettings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
webSettings.databaseEnabled = true webSettings.databaseEnabled = true
webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE
@ -153,11 +154,23 @@ class WebViewActivity : AppCompatActivity() {
Snackbar.make(findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG).show() Snackbar.make(findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG).show()
} }
override fun onBackPressed() {
// 检查 WebView 是否可以后退
if (webView.canGoBack()) {
webView.goBack() // 如果可以后退,则调用 goBack()
} else {
super.onBackPressed() // 否则调用默认的后退行为
}
}
private inner class DraggableTouchListener : View.OnTouchListener { private inner class DraggableTouchListener : View.OnTouchListener {
private var dX = 0f private var dX = 0f
private var dY = 0f private var dY = 0f
private var isClick = false private var isClick = false
private val touchSlop = 10 // 触摸阈值,用于判断是否为点击
@SuppressLint("ClickableViewAccessibility")
override fun onTouch(view: View, event: MotionEvent): Boolean { override fun onTouch(view: View, event: MotionEvent): Boolean {
when (event.action) { when (event.action) {
MotionEvent.ACTION_DOWN -> { MotionEvent.ACTION_DOWN -> {
@ -166,20 +179,28 @@ class WebViewActivity : AppCompatActivity() {
isClick = true isClick = true
} }
MotionEvent.ACTION_MOVE -> { MotionEvent.ACTION_MOVE -> {
view.animate() val deltaX = event.rawX + dX - view.x
.x(event.rawX + dX) val deltaY = event.rawY + dY - view.y
.y(event.rawY + dY) if (Math.abs(deltaX) > touchSlop || Math.abs(deltaY) > touchSlop) {
.setDuration(0) isClick = false // 超过阈值视为拖动
.start() view.animate()
isClick = false .x(event.rawX + dX)
.y(event.rawY + dY)
.setDuration(0)
.start()
}
} }
MotionEvent.ACTION_UP -> { MotionEvent.ACTION_UP -> {
if (isClick) { if (isClick) {
logAndReload() // 添加延迟,确保是点击而非拖动
view.postDelayed({
logAndReload()
}, 100) // 延迟100毫秒
} }
} }
} }
return true return true
} }
} }
} }

View file

@ -1,13 +1,13 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/gray_dark_2"/> <!-- 按钮的背景颜色,可以更改为你喜欢的颜色 --> <solid android:color="@color/gray_dark_2"/>
<corners android:radius="10dp"/> <!-- 设置圆角半径12dp 可以根据需要进行调整 --> <corners android:radius="10dp"/>
<stroke <stroke
android:width="1dp" android:width="1dp"
android:color="@android:color/transparent"/> <!-- 按钮的边框颜色,可以修改 --> android:color="@android:color/transparent"/>
<padding <padding
android:left="12dp" android:left="12dp"
android:top="8dp" android:top="8dp"
android:right="12dp" android:right="12dp"
android:bottom="8dp"/> <!-- 设置按钮的内边距 --> android:bottom="8dp"/>
</shape> </shape>

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/> <!-- 输入框背景颜色 --> <solid android:color="@color/gray_dark_10"/> <!-- 输入框背景颜色 -->
<corners android:radius="8dp"/> <!-- 圆角半径 --> <corners android:radius="8dp"/> <!-- 圆角半径 -->
<stroke <stroke
android:width="1dp" android:width="1dp"

View file

@ -1,80 +1,83 @@
<FrameLayout <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_width="match_parent" android:layout_height="match_parent">
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true"> android:fitsSystemWindows="true">
<!-- 包含两个ImageView的容器 --> <!-- 包含两个ImageView的容器 -->
<LinearLayout <LinearLayout
android:id="@+id/logoPos" android:id="@+id/logoPos"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" android:orientation="horizontal"
android:gravity="center" android:gravity="center"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginBottom="60dp"
android:layout_marginTop="84dp"> app:layout_constraintBottom_toTopOf="@+id/linearLayout">
<ImageView <ImageView
android:id="@+id/logo_3d" android:id="@+id/logo_3d"
android:layout_width="80dp" android:layout_width="80dp"
android:layout_height="80dp" android:layout_height="80dp"
android:contentDescription="@string/logo_3d_description" android:contentDescription="@string/logo_3d_description"
android:src="@drawable/logo_3d" /> android:src="@drawable/logo_3d" />
<ImageView <ImageView
android:id="@+id/appLogo" android:id="@+id/appLogo"
android:layout_width="200dp" android:layout_width="200dp"
android:layout_height="80dp" android:layout_height="80dp"
android:contentDescription="@string/app_logo" android:contentDescription="@string/app_logo"
android:src="@drawable/logo" /> android:src="@drawable/logo" />
</LinearLayout> </LinearLayout>
<!-- 输入框和按钮 --> <!-- 输入框和按钮 -->
<LinearLayout <LinearLayout
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent" android:gravity="center"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/logoPos" app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="20dp" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.8"> app:layout_constraintTop_toBottomOf="@id/logoPos"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintWidth_percent="0.8"
android:id="@+id/linearLayout">
<com.google.android.material.textfield.TextInputLayout <com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/urlInput"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:hint="@string/enter_your_server_url" android:layout_marginBottom="8dp">
android:inputType="textUri"
android:textColor="@color/cyan_dark_0" <com.google.android.material.textfield.TextInputEditText
android:textColorHint="@color/gray_dark_4" android:id="@+id/urlInput"
android:textSize="14sp" /> android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/enter_your_server_url"
android:inputType="textUri"
android:textColor="@color/cyan_dark_0"
android:textColorHint="@color/gray_dark_4"
android:background="@color/browser_actions_bg_grey"
android:textSize="14sp" />
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/loadUrlButton" android:id="@+id/loadUrlButton"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="10dp" android:layout_marginTop="10dp"
android:backgroundTint="@color/gray_dark_0" android:backgroundTint="@color/gray_dark_0"
android:text="@string/send" android:text="@string/send"
android:textColor="@android:color/white" android:textColor="@android:color/white"
android:textAllCaps="false" android:textAllCaps="false"
app:cornerRadius="4dp" /> app:cornerRadius="4dp" />
</LinearLayout> </LinearLayout>

View file

@ -5,11 +5,8 @@
<item name="android:statusBarColor"> <item name="android:statusBarColor">
@color/purple_dark_9 @color/purple_dark_9
</item> </item>
<item name="android:navigationBarColor">
@color/design_default_color_background
</item>
<item name="android:windowTitleBackgroundStyle"> <item name="android:windowTitleBackgroundStyle">
@color/design_default_color_background @color/purple_dark_9
</item> </item>
</style> </style>
</resources> </resources>