android - 如何在 Android Kotlin 中将 MainActivity 代码转换为片段
问题描述
我正在尝试将主要活动代码转换为导航抽屉模板中的片段。我看过这篇文章,但这是用 Java 编写的。我可以就如何将以下主要内容转换为片段幻灯片获得一些帮助和指导吗?
我的主要:
package com.example.testingapp
import android.graphics.Color
import android.os.Bundle
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import android.view.inputmethod.EditorInfo
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Variable and constants declaration
val btnShow = findViewById<Button>(R.id.btnShow)
val editText = findViewById<EditText>(R.id.editText)
val btnExit = findViewById<Button>(R.id.btnExit)
val webview = findViewById<WebView>(R.id.webview)
btnExit.setOnClickListener{
finish()
}
// Function that runs when the showButton is clicked.
// It takes one argument, which is the text entered by the user.
// Then it loads a web page, using the text as a part of the url.
btnShow.setOnClickListener{
// Declare the text from editText
val text = editText.text
// Show the text from the user in a small toast window
Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
// Make the webView
webview.webViewClient = WebViewClient()
webview.setBackgroundColor(Color.parseColor("#ffffff"))
}
}
}
主要的xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/btnUpdate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
tools:context=".MainActivity">
<Button
android:id="@+id/btnExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="24dp"
android:backgroundTint="@color/colorPrimary"
android:textColor="@android:color/white"
android:text="Exit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="24dp"
android:backgroundTint="@color/colorPrimary"
android:text="show temp"
android:textColor="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
<WebView
android:id="@+id/webview"
android:layout_width="337dp"
android:layout_height="386dp"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.581"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.213" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:ems="10"
android:hint="Enter sensor name"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/btnShow"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
目标:将上述主要代码转换为导航抽屉模板中的片段
class GalleryFragment : Fragment() {
private lateinit var galleryViewModel: GalleryViewModel
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
galleryViewModel =
ViewModelProvider(this).get(GalleryViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_gallery, container, false)
val textView: TextView = root.findViewById(R.id.text_gallery)
galleryViewModel.text.observe(viewLifecycleOwner, Observer {
textView.text = it
})
return root
}
}
\
解决方案
您发布的答案似乎直截了当,但我看到 Java 对新的 Kotlin Android 开发人员来说是多么难以理解。不过,我强烈建议您尝试一下,学习一点 Java 也可以让您更好地理解 Kotlin。
我试图以尽可能少的代码更改来回答,因此将来如何重现它会更加明显。只有大约三行发生了变化。
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.fragment.app.Fragment
class MainFrag: Fragment() {
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?, savedInstanceState: Bundle?): View?
= inflater.inflate(R.layout.activity_main, container, false).apply {
// Variable and constants declaration
val btnShow = findViewById<Button>(R.id.btnShow)
val editText = findViewById<EditText>(R.id.editText)
val btnExit = findViewById<Button>(R.id.btnExit)
val webview = findViewById<WebView>(R.id.webview)
btnExit.setOnClickListener { activity?.finish() }
// Function that runs when the showButton is clicked.
// It takes one argument, which is the text entered by the user.
// Then it loads a web page, using the text as a part of the url.
btnShow.setOnClickListener {
// Declare the text from editText
val text = editText.text
// Show the text from the user in a small toast window
Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
// Make the webView
webview.webViewClient = WebViewClient()
webview.setBackgroundColor(Color.parseColor("#ffffff"))
}
}
}
推荐阅读
- java - Spring RestTemplate JSON 响应被截断
- ios - 如何对 Realm 中的对象进行异步排序
- node.js - 无法扩展接口“http.IncomingMessage”。您指的是 “implements” 吗?
- python - 我在运行 train.py 时遇到问题,我很困惑
- javascript - HTML5 drawImage:Chrome 比 Mozilla 慢九倍
- azure - Azure 资源
- python - 无法使用漂亮的汤访问某些标签
- node.js - 如何创建一个“x”命令来退出节点
- java - 为什么我们需要 List 使用 Collection.sort() 方法进行排序?
- c - 我对链表有问题。这是一个双重列表的蛇游戏,它不应该吃东西,它只是每 5 步增长 1 个方块