如何在片段中制作按钮关闭电流并打开Kotlin的另一个片段

发布于 2025-02-12 18:27:08 字数 2053 浏览 0 评论 0原文

我需要在第一个片段中创建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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人问我粥可暖 2025-02-19 18:27:08

让我们箱子 Android Resouse Directoty 。该目录名称为导航
之后,我们创建导航恢复文件。该文件的名称为 nav_graph.xml
接下来,我们将其放入您的 activity_main.xml 中,

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph" />

nav_graph.xml 中,我们添加了目的地。看起来,

<navigation 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/nav_host"
app:startDestination="@id/oneFragment">


<fragment
    android:id="@+id/oneFragment"
    android:name="com.uogames.androidjun.OneFragment"
    android:label="OneFragment"
    tools:layout="@layout/fragment_one" >
</fragment>

<fragment
    android:id="@+id/twoFragment"
    android:name="com.uogames.androidjun.TwoFragment"
    android:label="TwoFragment"
    tools:layout="@layout/fragment_two" />
</navigation>

当您启动应用程序时,您将启动 OneFragment
如果您调用findnavcontroller()。导航(r.id.to_twofragment)
您将看到第二片片段。您可以阅读有关它的信息在这里

另外,您需要依赖项,

implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.4.2'

如果要关闭应用程序,只需调用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

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph" />

In nav_graph.xml we add destinations. It looks like

<navigation 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/nav_host"
app:startDestination="@id/oneFragment">


<fragment
    android:id="@+id/oneFragment"
    android:name="com.uogames.androidjun.OneFragment"
    android:label="OneFragment"
    tools:layout="@layout/fragment_one" >
</fragment>

<fragment
    android:id="@+id/twoFragment"
    android:name="com.uogames.androidjun.TwoFragment"
    android:label="TwoFragment"
    tools:layout="@layout/fragment_two" />
</navigation>

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

implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.4.2'

If you want to close application just call it requireActivity().finishAndRemoveTask()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文