Add back navigation support in WebViewActivity
- Fixed the implemented onBackPressed method to enable back navigation within the WebView.
This commit is contained in:
parent
e835b5a1e4
commit
90cacdc914
5 changed files with 93 additions and 72 deletions
|
|
@ -45,6 +45,7 @@ class WebViewActivity : AppCompatActivity() {
|
|||
}
|
||||
}
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
private fun setupRefreshButton() {
|
||||
// 将按钮置于最前,并设置点击和拖动事件
|
||||
refreshButton.bringToFront()
|
||||
|
|
@ -63,7 +64,7 @@ class WebViewActivity : AppCompatActivity() {
|
|||
val webSettings: WebSettings = webView.settings
|
||||
webSettings.javaScriptEnabled = true
|
||||
webSettings.domStorageEnabled = true
|
||||
webSettings.cacheMode = WebSettings.LOAD_DEFAULT
|
||||
webSettings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
|
||||
webSettings.databaseEnabled = true
|
||||
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()
|
||||
}
|
||||
|
||||
override fun onBackPressed() {
|
||||
// 检查 WebView 是否可以后退
|
||||
if (webView.canGoBack()) {
|
||||
webView.goBack() // 如果可以后退,则调用 goBack()
|
||||
} else {
|
||||
super.onBackPressed() // 否则调用默认的后退行为
|
||||
}
|
||||
}
|
||||
|
||||
private inner class DraggableTouchListener : View.OnTouchListener {
|
||||
|
||||
private var dX = 0f
|
||||
private var dY = 0f
|
||||
private var isClick = false
|
||||
private val touchSlop = 10 // 触摸阈值,用于判断是否为点击
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
override fun onTouch(view: View, event: MotionEvent): Boolean {
|
||||
when (event.action) {
|
||||
MotionEvent.ACTION_DOWN -> {
|
||||
|
|
@ -166,20 +179,28 @@ class WebViewActivity : AppCompatActivity() {
|
|||
isClick = true
|
||||
}
|
||||
MotionEvent.ACTION_MOVE -> {
|
||||
view.animate()
|
||||
.x(event.rawX + dX)
|
||||
.y(event.rawY + dY)
|
||||
.setDuration(0)
|
||||
.start()
|
||||
isClick = false
|
||||
val deltaX = event.rawX + dX - view.x
|
||||
val deltaY = event.rawY + dY - view.y
|
||||
if (Math.abs(deltaX) > touchSlop || Math.abs(deltaY) > touchSlop) {
|
||||
isClick = false // 超过阈值视为拖动
|
||||
view.animate()
|
||||
.x(event.rawX + dX)
|
||||
.y(event.rawY + dY)
|
||||
.setDuration(0)
|
||||
.start()
|
||||
}
|
||||
}
|
||||
MotionEvent.ACTION_UP -> {
|
||||
if (isClick) {
|
||||
logAndReload()
|
||||
// 添加延迟,确保是点击而非拖动
|
||||
view.postDelayed({
|
||||
logAndReload()
|
||||
}, 100) // 延迟100毫秒
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<solid android:color="@color/gray_dark_2"/> <!-- 按钮的背景颜色,可以更改为你喜欢的颜色 -->
|
||||
<corners android:radius="10dp"/> <!-- 设置圆角半径,12dp 可以根据需要进行调整 -->
|
||||
<solid android:color="@color/gray_dark_2"/>
|
||||
<corners android:radius="10dp"/>
|
||||
<stroke
|
||||
android:width="1dp"
|
||||
android:color="@android:color/transparent"/> <!-- 按钮的边框颜色,可以修改 -->
|
||||
android:color="@android:color/transparent"/>
|
||||
<padding
|
||||
android:left="12dp"
|
||||
android:top="8dp"
|
||||
android:right="12dp"
|
||||
android:bottom="8dp"/> <!-- 设置按钮的内边距 -->
|
||||
android:bottom="8dp"/>
|
||||
</shape>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<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"/> <!-- 圆角半径 -->
|
||||
<stroke
|
||||
android:width="1dp"
|
||||
|
|
|
|||
|
|
@ -1,80 +1,83 @@
|
|||
<FrameLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:fitsSystemWindows="true">
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:fitsSystemWindows="true">
|
||||
|
||||
|
||||
<!-- 包含两个ImageView的容器 -->
|
||||
<LinearLayout
|
||||
android:id="@+id/logoPos"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
android:layout_marginTop="84dp">
|
||||
android:id="@+id/logoPos"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
android:layout_marginBottom="60dp"
|
||||
app:layout_constraintBottom_toTopOf="@+id/linearLayout">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/logo_3d"
|
||||
android:layout_width="80dp"
|
||||
android:layout_height="80dp"
|
||||
android:contentDescription="@string/logo_3d_description"
|
||||
android:src="@drawable/logo_3d" />
|
||||
android:id="@+id/logo_3d"
|
||||
android:layout_width="80dp"
|
||||
android:layout_height="80dp"
|
||||
android:contentDescription="@string/logo_3d_description"
|
||||
android:src="@drawable/logo_3d" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/appLogo"
|
||||
android:layout_width="200dp"
|
||||
android:layout_height="80dp"
|
||||
android:contentDescription="@string/app_logo"
|
||||
android:src="@drawable/logo" />
|
||||
android:id="@+id/appLogo"
|
||||
android:layout_width="200dp"
|
||||
android:layout_height="80dp"
|
||||
android:contentDescription="@string/app_logo"
|
||||
android:src="@drawable/logo" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<!-- 输入框和按钮 -->
|
||||
<LinearLayout
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/logoPos"
|
||||
android:layout_marginTop="20dp"
|
||||
app:layout_constraintWidth_percent="0.8">
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:gravity="center"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
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
|
||||
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_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:textSize="14sp" />
|
||||
android:layout_marginBottom="8dp">
|
||||
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:id="@+id/urlInput"
|
||||
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.button.MaterialButton
|
||||
android:id="@+id/loadUrlButton"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="10dp"
|
||||
android:backgroundTint="@color/gray_dark_0"
|
||||
android:text="@string/send"
|
||||
android:textColor="@android:color/white"
|
||||
android:textAllCaps="false"
|
||||
app:cornerRadius="4dp" />
|
||||
android:id="@+id/loadUrlButton"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="10dp"
|
||||
android:backgroundTint="@color/gray_dark_0"
|
||||
android:text="@string/send"
|
||||
android:textColor="@android:color/white"
|
||||
android:textAllCaps="false"
|
||||
app:cornerRadius="4dp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
|
|
|||
|
|
@ -5,11 +5,8 @@
|
|||
<item name="android:statusBarColor">
|
||||
@color/purple_dark_9
|
||||
</item>
|
||||
<item name="android:navigationBarColor">
|
||||
@color/design_default_color_background
|
||||
</item>
|
||||
<item name="android:windowTitleBackgroundStyle">
|
||||
@color/design_default_color_background
|
||||
@color/purple_dark_9
|
||||
</item>
|
||||
</style>
|
||||
</resources>
|
||||
Loading…
Reference in a new issue