首页 > 解决方案 > Kotlin:单击时如何更改按钮的文本和颜色?

问题描述

我想在单击按钮时更改按钮的颜色和文本,而不使用 ToogleButton。我像这样设置xml文件。

<Button
            android:id="@+id/downloadButton"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="Download"
            android:textAllCaps="false"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

我在主Activity中做了一个代码是,

 override fun onDownloadClick(item: ProjectListDataModel, position: Int) {
       downloadButton.setOnClickListener {
            if (downloadButton.getText() == "Download") {
                downloadButton.setText("Downloaded")
                downloadButton.setBackgroundColor(ContextCompat.getColor(this,R.color.blue))
            } else if (downloadButton.getText() == "Downloaded") {
                downloadButton.setText("Download")
                downloadButton.setBackgroundColor(ContextCompat.getColor(this,R.color.gray))
            }
        }
    }

这就是采纳者

class ServerProjectsRecyclerAdapter(
    val list:List<ProjectListDataModel>,
    var serverClickListener: ServerProjectListActivity) : RecyclerView.Adapter<ServerProjectRecyclerViewHolder>() {

    interface OnProjectListClickListener {
        fun onServerProjectListClick(item: ProjectListDataModel, position: Int)
        fun onDownloadClick(item: ProjectListDataModel, position: Int)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ServerProjectRecyclerViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_cardview_server, parent, false)
        return ServerProjectRecyclerViewHolder(view)

    }

    override fun getItemCount(): Int {
        return list.count()
    }

    override fun onBindViewHolder(holder: ServerProjectRecyclerViewHolder, position: Int) {
        holder.initialize(list.get(position), serverClickListener)
    }
}

class ServerProjectRecyclerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    fun initialize(
        item: ProjectListDataModel,
        action: ServerProjectListActivity
    ) {
        itemView.projectListView.text = item.name
        itemView.modifiedTimeText.text = item.modTime
        //itemView.descriptionText.text = item.description
        itemView.setOnClickListener {
            action.onServerProjectListClick(item, adapterPosition)
        }
        itemView.downloadButton.setOnClickListener {
            action.onDownloadClick(item, adapterPosition)
        }
    }
}

似乎没有错误,但不起作用。有一些使用 java 的例子,但不是 Kotlin。

这是我想做的java教程。

https://www.youtube.com/watch?v=OVmLLet9aAQ

有没有人知道是什么问题?

标签: androidkotlinbuttononclick

解决方案


在这里改变你的界面。

interface OnProjectListClickListener {
  fun onServerProjectListClick(item: ProjectListDataModel, position: Int)
  fun onDownloadClick(button:Button,item: ProjectListDataModel, position: Int)
}

viewholder在课堂上更改以下内容

itemView.downloadButton.setOnClickListener {
            action.onDownloadClick(it,item, adapterPosition)
        }

最后在您的活动中执行以下操作。

 override fun onDownloadClick(button:Button,item: ProjectListDataModel, position: Int) {

    if (button.getText() == "Download") {
      button.setText("Downloaded")
      button.setBackgroundColor(ContextCompat.getColor(this,R.color.blue))
    } else if (button.getText() == "Downloaded") {
      button.setText("Download")
      button.setBackgroundColor(ContextCompat.getColor(this,R.color.gray))
    }
}

推荐阅读