Kotlin: 如何改变按钮的文字和颜色?

问题描述 投票:0回答:1

我想在不使用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.comwatch?v=OVmLLet9aAQ

有谁知道问题出在哪里?

android kotlin button onclick
1个回答
1
投票

在这里改变你的界面与此。

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))
    }
}

0
投票

这是你应该使用的属性。

android:backgroundTint="@android:color/white"

把你的onClickListener改成下面的样子

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_server_project_list)

    downloadButton?.setOnClickListener {
        if (downloadButton.getText() == "Download") {
            downloadButton.setText("Downloaded")
            downloadButton.setBackgroundTint(android.R.color.black)
        } else if (downloadButton.getText() == "Downloaded") {
            downloadButton.setText("Download")
            downloadButton.setBackgroundTint(android.R.color.holo_red_dark)
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.