我正在Kotlin中开发Android项目。
我有我的工具栏:
@BindView(R.id.tool_bar)
lateinit var toolbar: Toolbar
我想设置标题:
// student.id is a int type
toolbar.title = student.id
学生ID是Int
类型。 toolbar.title的期望值为CharSequence
类型。如何将我的ID分配给工具栏的标题?我的意思是如何将student.id
转换为CharSequence
,以便可以将其分配给工具栏标题?
您是否可以为您的案件尝试kotlin字符串模板
toolbar.title = "${student.id}"
这是将任何类型的数字(int,float,double)转换为字符串的最佳方法:-
toolbar.title = "" + student.id
记住一件事,""
这必须在数字之前。之后,输入+
,然后输入任何类型的数字。Java,Kotlin,Javascipt等许多编程语言都支持此技术。
用途:
toolbar.title = student.id.toString()