英文:
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:
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
<?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="Sample button 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, "You pressed on button 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="Click here"
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")
}
}
}
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&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, "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()
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论