如何在弹出对话框中设置带有按钮的OnClickListener



我想制作一个youtube应用程序,在那里粘贴链接,点击button后,视频将添加到recyclerView。按钮位于PopupDialog内部,还有一个EditText字段。因此,首先我想将粘贴的链接添加到titlesList,并添加一个toast,这样我就可以查看按钮是否工作。问题是,什么都没发生。这是应用程序的预览

主要活动.kt

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val popupButton: FloatingActionButton = findViewById(R.id.fab)
val bottomNav: BottomNavigationView = findViewById(R.id.bottomNavigationView)
addDataButton = findViewById(R.id.addButton)
bottomNav.background = null
bottomNav.menu.findItem(R.id.placeholder).isEnabled = false
replaceFragment(home)
bottomNav.setOnItemSelectedListener {
when (it.itemId) {
R.id.home -> replaceFragment(home)
R.id.player -> replaceFragment(player)
R.id.profile -> replaceFragment(profile)
R.id.settings -> replaceFragment(settings)
}
true
}
popupButton.setOnClickListener {
showDialog()
}
addDataButton?.setOnClickListener {
implementAdapterData()
}
}
private fun replaceFragment(fragment: Fragment) {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_container, fragment)
transaction.commit()
}
private fun showDialog() {
val dialog = Dialog(this)
dialog.setContentView(R.layout.popup)
dialog.show()
}
private fun implementAdapterData() {
val title = RecyclerAdapter().titles
val editText: EditText = findViewById(R.id.plain_text_input)
if (editText.text.isEmpty()) {
Toast.makeText(applicationContext, "Empty field", Toast.LENGTH_SHORT).show()
} else {
title.add(editText.text.toString())
Toast.makeText(applicationContext, "Added", Toast.LENGTH_SHORT).show()
}
}
}

RecycleAdapter.kt

open class RecyclerAdapter: RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {

public open var titles = mutableListOf(
"Chapter one",
"Chapter two",
"Chapter three",
"Chapter four",
"Chapter five",
"Chapter six",
"Chapter seven",
"Chapter eight",
"Chapter nine",
"Chapter ten",
"Chapter eleven",
"Chapter twelve",
"Chapter thirteen",
"Chapter fourteen",
"Chapter fifteen",
"Chapter sixteen")
private var details = mutableListOf(
"Item one details",
"Item two details",
"Item three details",
"Item four details",
"Item five details",
"Item six details",
"Item seven details",
"Item eight details",
"Item nine details",
"Item ten details",
"Item eleven details",
"Item twelve details",
"Item thirteen details",
"Item fourteen details",
"Item fifteen details",
"Item sixteen details",)
private var images = mutableListOf(
R.drawable.samoyed,
R.drawable.samoyed,
R.drawable.samoyed,
R.drawable.samoyed,
R.drawable.samoyed,
R.drawable.samoyed,
R.drawable.samoyed,
R.drawable.samoyed,
R.drawable.samoyed,
R.drawable.samoyed,
R.drawable.samoyed,
R.drawable.samoyed,
R.drawable.samoyed,
R.drawable.samoyed,
R.drawable.samoyed,
R.drawable.samoyed)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val vi = LayoutInflater.from(parent.context).inflate(R.layout.card_layout, parent, false)
return ViewHolder(vi)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.itemTitle.text = titles[position]
holder.itemDetails.text = details[position]
holder.itemImage.setImageResource(images[position])
}
override fun getItemCount(): Int {
return titles.size
}
inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
var itemImage: ImageView
var itemTitle: TextView
var itemDetails: TextView
init {
itemImage = itemView.findViewById(R.id.tv_thumbnail)
itemTitle = itemView.findViewById(R.id.tv_title)
itemDetails = itemView.findViewById(R.id.tv_description)
}
}

}

Popup.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"
android:layout_width="350dp"
android:layout_height="350dp"
android:layout_gravity="center"
android:background="@drawable/cercle_backgoud">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="36dp"
android:contentDescription="@string/samoyed"
android:src="@drawable/samoyed_popup"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_light_italic"
android:gravity="center"
android:text="@string/enter_youtube_video_link"
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/plain_text_input"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<EditText
android:id="@+id/plain_text_input"
android:layout_width="match_parent"
android:layout_height="30dp"
android:hint="@string/here_paste_the_link"
android:inputType="text"
android:minHeight="48dp"
android:gravity="center"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.612"
android:autofillHints=""
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto_light_italic"
android:text="@string/add"
android:textColor="@color/white"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/plain_text_input"
app:layout_constraintVertical_bias="0.26" />

</androidx.constraintlayout.widget.ConstraintLayout>

您调用addDataButton = findViewById(R.id.addButton)在CCD_ 7中。但此时,对话框尚未创建。(只有调用showDialog()才能创建它(。为了让findViewById((定位EditText,需要首先膨胀对话框的布局。

通常情况下,每个对话框/片段/活动都会负责扩展自己的布局。但是,在调用对话框上的setContentView()之前,可以在MainActivity.showDialog((中扩展布局,从而使您有机会连接侦听器。

示例MainActivity.kt:

var dialog:Dialog?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val popupButton: FloatingActionButton = findViewById(R.id.fab)
val bottomNav: BottomNavigationView = findViewById(R.id.bottomNavigationView)
addDataButton = findViewById(R.id.addButton)
bottomNav.background = null
bottomNav.menu.findItem(R.id.placeholder).isEnabled = false
replaceFragment(home)
bottomNav.setOnItemSelectedListener {
when (it.itemId) {
R.id.home -> replaceFragment(home)
R.id.player -> replaceFragment(player)
R.id.profile -> replaceFragment(profile)
R.id.settings -> replaceFragment(settings)
}
true
}
popupButton.setOnClickListener {
showDialog()
}
addDataButton?.setOnClickListener {
implementAdapterData()
}
}
private fun replaceFragment(fragment: Fragment) {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_container, fragment)
transaction.commit()
}
private fun showDialog() {
dialog = Dialog(this)
val dialogView:View = d.layoutInflater.inflate(R.layout.popup, null)
val editText = dialogView.findViewById<EditText>(R.id.plain_text_input)
dialogView.findViewById<Button>(R.id.button)?.setOnClickListener {
if (editText.text.isEmpty()) {
Toast.makeText(applicationContext, "Empty field", Toast.LENGTH_SHORT).show()
} else {
// title.add(editText.text.toString())
Toast.makeText(applicationContext, "Added", Toast.LENGTH_SHORT).show()
}
}
d.setContentView(dialogView)
dialog.show()
}
private fun implementAdapterData() {
val title = RecyclerAdapter().titles
val editText: EditText = findViewById(R.id.plain_text_input)
if (editText.text.isEmpty()) {
Toast.makeText(applicationContext, "Empty field", Toast.LENGTH_SHORT).show()
} else {
title.add(editText.text.toString())
Toast.makeText(applicationContext, "Added", Toast.LENGTH_SHORT).show()
}
}
}
override fun onPause() {
dialog?.dismiss()
super.onPause()
}

请注意,为了避免内存泄漏,我们将在onPause()中取消对话框。

接下来,您可能想要使用addVideoUrl()方法创建一个自定义的RecyclerView适配器。在该方法中,在向标题添加新项目后,需要调用notifyItemInserted(),以便RecyclerView知道更新列表。

最新更新