如何使底部弹出式片段中的按钮可点击?

huangapple go评论115阅读模式
英文:

How do I make buttons clickable in the bottom sheet fragment?

问题

以下是翻译好的部分,不包括代码部分:

流程: 有两个 XML 文件:

activity_main.xml
如何使底部弹出式片段中的按钮可点击?

bottomsheet_fragment.xml
如何使底部弹出式片段中的按钮可点击?

我想点击"点击这里"按钮(XML ID: btn_show),显示底部弹出片段(这一部分正常运行)... 相应的文件:activity_main.xml,MainActivity.kt

现在,我想点击底部弹出窗口中的按钮(XML ID: btn_button1),通过 toast 显示你按下按钮 1 的文本(或其他方式也可以,我只是想在点击按钮时显示一些东西)(这部分没有正常运行)... 相应的文件:bottomsheet_fragment.xml,BottomSheetFragment.kt

我附上了文件的代码如下:

bottomsheet_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="示例按钮 1"
        />

</LinearLayout>

BottomSheetFragment.kt

package android.example.naruto

import android.example.naruto.databinding.ActivityMainBinding
import android.example.naruto.databinding.BottomsheetFragmentBinding
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

class BottomSheetFragment: BottomSheetDialogFragment() {

    private lateinit var binding2: BottomsheetFragmentBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding2 = BottomsheetFragmentBinding.inflate(layoutInflater)

        binding2.btnButton1.setOnClickListener{
            Toast.makeText(context, "你按下了按钮 1!", Toast.LENGTH_SHORT).show()
        }
    }
}

activity_main.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击这里"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

main_activity.kt

package android.example.naruto

import android.example.naruto.databinding.ActivityMainBinding
import android.example.naruto.databinding.BottomsheetFragmentBinding
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

    private lateinit var binding1: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding1 = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding1.root)

        val bottomSheetFragment= BottomSheetFragment()

        binding1.btnShow.setOnClickListener {
           bottomSheetFragment.show(supportFragmentManager, "BottomSheetDialog")
        }
    }
}

提前感谢您的帮助!如果您需要更多细节或澄清,请告诉我。我参考了以下教程:https://www.youtube.com/watch?v=yqnVPiWAw0o&t=21s, https://www.youtube.com/watch?v=4GXflIdrlus

英文:

**The flow: ** There are two xml files:

activity_main.xml
如何使底部弹出式片段中的按钮可点击?

bottomsheet_fragment.xml
如何使底部弹出式片段中的按钮可点击?

I want to click on "click here" button (xml id: btn_show) and show the bottom sheet fragment [This is happening correctly) .. corresponding files: activity_main.xml, MainActivity.kt

Now I want to click on the button in the bottom sheet(xml id: btn_button1), and show the text you pressed button 1 via toast(or anything else is fine too, I just want to show something when I click the button) [This part is not happenning correctly) .. corresponding files: bottomsheet_fragment.xml, BottomSheetFragment.kt

I am attaching codes for the files below:

bottomsheet_fragment.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:orientation=&quot;vertical&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;

    &lt;Button
        android:id=&quot;@+id/btn_button1&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_margin=&quot;20dp&quot;
        android:text=&quot;Sample button 1&quot;
        /&gt;

&lt;/LinearLayout&gt;

BottomSheetFragment.kt

package android.example.naruto

import android.example.naruto.databinding.ActivityMainBinding
import android.example.naruto.databinding.BottomsheetFragmentBinding
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

class BottomSheetFragment: BottomSheetDialogFragment() {

    private lateinit var binding2: BottomsheetFragmentBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding2 = BottomsheetFragmentBinding.inflate(layoutInflater)

        binding2.btnButton1.setOnClickListener{
            Toast.makeText(context, &quot;You pressed on button 1!&quot;, Toast.LENGTH_SHORT).show()
        }
    }
}

activity_main.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot;&gt;

    &lt;Button
        android:id=&quot;@+id/btn_show&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;Click here&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

main_activity.kt

package android.example.naruto

import android.example.naruto.databinding.ActivityMainBinding
import android.example.naruto.databinding.BottomsheetFragmentBinding
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

    private lateinit var binding1: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding1 = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding1.root)

        val bottomSheetFragment= BottomSheetFragment()

        binding1.btnShow.setOnClickListener {
           bottomSheetFragment.show(supportFragmentManager, &quot;BottomSheetDialog&quot;)
        }
    }
}

Thanks in advance for the help! Please let me know if you need any more details or clarifications.

I referred to these tutorials: https://www.youtube.com/watch?v=yqnVPiWAw0o&amp;t=21s, https://www.youtube.com/watch?v=4GXflIdrlus

答案1

得分: 1

I am not able to see onCreateView method in BottomSheetFragment and probably that's why it is not working.

You can change your code like this:

From:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    binding2 = BottomsheetFragmentBinding.inflate(layoutInflater)

    binding2.btnButton1.setOnClickListener {
        Toast.makeText(context, "You pressed on button 1!", Toast.LENGTH_SHORT).show()
    }
}

To:

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    binding2 = BottomSheetFamilyTreeBinding.inflate(inflater, container, false)
    return binding2.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    binding2.btnButton1.setOnClickListener {
        Toast.makeText(context, "You pressed on button 1!", Toast.LENGTH_SHORT).show()
    }
}
英文:

I am not able to see onCreateView method in BottomSheetFragment and probably that's why it is not working

You can change your code like this

From

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding2 = BottomsheetFragmentBinding.inflate(layoutInflater)

        binding2.btnButton1.setOnClickListener{
            Toast.makeText(context, &quot;You pressed on button 1!&quot;, Toast.LENGTH_SHORT).show()
        }
    }

To

override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding2 = BottomSheetFamilyTreeBinding.inflate(inflater, container, false)
        return binding2.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding2.btnButton1.setOnClickListener{
            Toast.makeText(context, &quot;You pressed on button 1!&quot;, Toast.LENGTH_SHORT).show()
        }
        
    }

huangapple
  • 本文由 发表于 2023年2月8日 17:01:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定