如何在片段中制作按钮关闭电流并打开Kotlin的另一个片段
我需要在第一个片段中创建2个片段和2个按钮,一个片段将关闭当前片段并启动另一个片段,其次将关闭应用程序,我在做错了什么(每个持有者中的每个片段)?
首先片段打开良好,但是当我单击第一次活动中的按钮时,它们根本无法工作。
我该如何写这个右路?
MainAttivity.kt
package com.example.webviewapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportFragmentManager
.beginTransaction()
.replace(R.id.fullcreen_holder, start_fragment.newInstance())
.commit()
}
}
start_fragment.kt
package com.example.webviewapp
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
class start_fragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.layout_start_fragment, container, false)
val agreeButton: Button = view.findViewById(R.id.privacy_agree)
val declineButton: Button = view.findViewById(R.id.privacy_decline)
agreeButton.setOnClickListener{
requireActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.webview_holder, webview_fragment())
.hide(start_fragment())
.commit();
}
declineButton.setOnClickListener{
android.os.Process.killProcess(android.os.Process.myPid())
}
return inflater!!.inflate(R.layout.layout_start_fragment, container, false)
}
companion object {
@JvmStatic
fun newInstance() = start_fragment()
}
}
I need to create 2 fragments and 2 button in first fragment one which will close current fragment and start another, and second which will close app at all, what i'm doing wrong(each fragment in different holders)?
First fragment opens well, but when i click on buttons in first activity, they don't working at all.
How can i write this rightway?
MainActivity.kt
package com.example.webviewapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportFragmentManager
.beginTransaction()
.replace(R.id.fullcreen_holder, start_fragment.newInstance())
.commit()
}
}
start_fragment.kt
package com.example.webviewapp
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
class start_fragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.layout_start_fragment, container, false)
val agreeButton: Button = view.findViewById(R.id.privacy_agree)
val declineButton: Button = view.findViewById(R.id.privacy_decline)
agreeButton.setOnClickListener{
requireActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.webview_holder, webview_fragment())
.hide(start_fragment())
.commit();
}
declineButton.setOnClickListener{
android.os.Process.killProcess(android.os.Process.myPid())
}
return inflater!!.inflate(R.layout.layout_start_fragment, container, false)
}
companion object {
@JvmStatic
fun newInstance() = start_fragment()
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我们箱子 Android Resouse Directoty 。该目录名称为导航。
之后,我们创建导航恢复文件。该文件的名称为 nav_graph.xml
接下来,我们将其放入您的 activity_main.xml 中,
在 nav_graph.xml 中,我们添加了目的地。看起来,
当您启动应用程序时,您将启动 OneFragment 。
如果您调用
findnavcontroller()。导航(r.id.to_twofragment)
您将看到第二片片段。您可以阅读有关它的信息在这里。
另外,您需要依赖项,
如果要关闭应用程序,只需调用
sirceeactivity()。finishAndRemovEtask()
Let's crate an android resouse directoty. That directory name is navigation.
After that we create navigation resouse file. That file will have name as nav_graph.xml
The next we put it in your activity_main.xml
In nav_graph.xml we add destinations. It looks like
Then when you start application you will start whith OneFragment.
And if you call
findNavController().navigate(R.id.to_twoFragment)
you will see second fragment. You can read about it here.
Also you need dependencies
If you want to close application just call it
requireActivity().finishAndRemoveTask()