Commit 67c73370 by huangzhicong

增加启动页,允许自定义要访问的url

parent a2092b33
......@@ -27,6 +27,14 @@ import kotlin.RuntimeException
class MainFragment : Fragment() {
companion object {
private const val TAG = "MainFragment"
private const val DEFAULT_URL = "file:///android_asset/index.html"
private const val ARG_URL = "arg_url"
fun createStartBundle(): Bundle = Bundle()
fun createStartBundle(url: String): Bundle = Bundle().apply {
putString(ARG_URL, url)
}
}
private lateinit var wv: WebView
......@@ -68,8 +76,11 @@ class MainFragment : Fragment() {
javaScriptEnabled = true
}
wv.addJavascriptInterface(WebInterface(), "maxrocky")
wv.loadUrl("file:///android_asset/index.html")
// wv.loadUrl("http://114.115.202.253:8080/#/")
var url = arguments?.getString(ARG_URL)
if (url.isNullOrEmpty()) {
url = DEFAULT_URL
}
wv.loadUrl(url)
videoContract = VideoContract(this, wv)
cameraContract = CameraContract(this, wv)
......
package com.maxrocky.nativeview.fragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.maxrocky.nativeview.R
class StartFragment : Fragment() {
private lateinit var urlTextView: TextView
private lateinit var button: Button
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater.inflate(R.layout.fragment_start, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
urlTextView = view.findViewById(R.id.url)
button = view.findViewById(R.id.enter)
button.setOnClickListener {
val url = urlTextView.text.trim().toString()
findNavController().navigate(
R.id.action_startFragment_to_mainFragment,
MainFragment.createStartBundle(url)
)
}
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/url_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/url_label"
android:layout_marginTop="20dp"
android:layout_marginStart="20dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
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_constraintStart_toEndOf="@id/url_label"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/enter"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/enter_button"
android:layout_marginTop="10dp"
app:layout_constraintStart_toStartOf="@id/url_label"
app:layout_constraintTop_toBottomOf="@id/url_label"
app:layout_constraintEnd_toEndOf="@id/url" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
......@@ -2,7 +2,7 @@
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_graph"
app:startDestination="@id/mainFragment">
app:startDestination="@id/startFragment">
<fragment
android:id="@+id/mainFragment"
......@@ -11,6 +11,9 @@
<action
android:id="@+id/action_mainFragment_to_recorderBottomSheet"
app:destination="@id/recorderBottomSheet" />
<argument
android:name="url"
app:argType="string" />
</fragment>
<dialog
android:id="@+id/recorderBottomSheet"
......@@ -20,4 +23,12 @@
android:name="uri"
app:argType="string" />
</dialog>
<fragment
android:id="@+id/startFragment"
android:name="com.maxrocky.nativeview.fragment.StartFragment"
android:label="StartFragment" >
<action
android:id="@+id/action_startFragment_to_mainFragment"
app:destination="@id/mainFragment" />
</fragment>
</navigation>
\ No newline at end of file
<resources>
<string name="app_name">NativeView</string>
<string name="url_label">网址:</string>
<string name="enter_button">启动</string>
<string name="camera_denied">请到系统设置允许访问相机</string>
<string name="record_audio_denied">请到系统设置允许访问麦克风</string>
......
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