android - 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
有没有人知道是什么问题?
解决方案
在这里改变你的界面。
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))
}
}
推荐阅读
- angular - 材质侧导航右侧菜单中组件提供的动态内容
- python - 如何在数据单元中创建具有虚线间距和宽度的线条?
- r - 使用比例数据在 mgcv 中运行二项式 GAM 时出错
- string - 将字符串转换为元组 Haskell
- javascript - 使用 ajax 将数组从 javascript 发布到 symfony 控制器
- jquery - 仅在文件上传开始时显示进度条
- c++ - 字符串未正确复制并显示错误:无法转换 'std::__cxx11::string {aka std::__cxx11::basic_string
}' - c# - 获取用户控件的 ActualWidth
- java - Spring Boot 应用程序中所有请求的超时异常
- performance - 提高ESC/POS Thermal_Printer图像打印速度的方法?