Commit bef6b035 by huangzhicong

允许起始页输入要访问的地址

parent 1b2d6adb
......@@ -71,6 +71,20 @@ class MainFragment : Fragment() {
result: JsResult?
): Boolean = false
}
wv.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
Log.d(TAG, "shouldOverrideUrlLoading old: $url")
return super.shouldOverrideUrlLoading(view, url)
}
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
Log.d(TAG, "shouldOverrideUrlLoading new: ${request?.url}")
return super.shouldOverrideUrlLoading(view, request)
}
}
wv.settings.apply {
javaScriptEnabled = true
}
......
......@@ -5,13 +5,25 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import android.widget.EditText
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.maxrocky.nativeview.R
import com.maxrocky.nativeview.utils.dataStore
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
class StartFragment : Fragment() {
private lateinit var urlTextView: TextView
companion object {
private const val STORAGE_URL = "storage_url"
}
private lateinit var urlEditText: EditText
private lateinit var button: Button
override fun onCreateView(
......@@ -22,11 +34,26 @@ class StartFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
urlTextView = view.findViewById(R.id.url)
urlEditText = view.findViewById(R.id.url)
button = view.findViewById(R.id.enter)
lifecycleScope.launch(Dispatchers.IO) {
val storageKey = stringPreferencesKey(STORAGE_URL)
context?.dataStore?.data?.map { it[storageKey] ?: "" }?.collect {
urlEditText.setText(it)
}
}
button.setOnClickListener {
val url = urlTextView.text.trim().toString()
val url = urlEditText.text.trim().toString()
if (url.isNotEmpty()) {
lifecycleScope.launch(Dispatchers.IO) {
val storageKey = stringPreferencesKey(STORAGE_URL)
context?.dataStore?.edit {
it[storageKey] = url
}
}
}
findNavController().navigate(
R.id.action_startFragment_to_mainFragment,
MainFragment.createStartBundle(url)
......
......@@ -14,13 +14,14 @@
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
<EditText
android:id="@+id/url"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="20dp"
app:layout_constraintTop_toTopOf="@id/url_label"
app:layout_constraintBottom_toBottomOf="@id/url_label"
app:layout_constraintStart_toEndOf="@id/url_label"
app:layout_constraintEnd_toEndOf="parent" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment