我试图在EditText中设置文本,但它说:
Type mismatch.
Required: Editable
Found: String
我的代码如下:
String name = "Paramjeet"
val nametxt = findViewById (R.id.nametxt) as EditText
nametxt.text = name
不要说使用setText,因为我使用的是kotlin,而不是Java。
我试图在EditText中设置文本,但它说:
Type mismatch.
Required: Editable
Found: String
我的代码如下:
String name = "Paramjeet"
val nametxt = findViewById (R.id.nametxt) as EditText
nametxt.text = name
不要说使用setText,因为我使用的是kotlin,而不是Java。
当前回答
像这样使用:
edtTitle.setText(intent.getStringExtra(EXTRA_TITLE))
edtDesc.setText(intent.getStringExtra(EXTRA_DESC))
其他回答
使用setText(String),因为editText。text需要一个可编辑的,而不是字符串。
var name:String = "hello world"
textview.text = name
(or)
var editText:String = "hello world"
textview.setText(name)
简单的解决方案
只需使用edittext. settext (yourdata)而不是edittext。因为EditText是可编辑的,EditText。text用于TextView
例如:
var name:String = "Muzammil"
edittext.setText(name)
这就是我的工作。
像这样使用:
edtTitle.setText(intent.getStringExtra(EXTRA_TITLE))
edtDesc.setText(intent.getStringExtra(EXTRA_DESC))
我在我的项目中也有同样的问题,我给你一个例子,展示了如何检索和设置Kotlin布局中的数据: 有一个按钮save_button和两个文本编辑字段edit_name和edit_password。
//when cliquing on the button 'save_button'
save_button.setOnClickListener {
// geting the value from the two fields by using .text.toString()
val email = edit_name.text.toString()
val password = edit_password.text.toString()
// showing the result on the systeme's log
Log.d("Main activity","your email is " + email )
Log.d("Main activity", "your password is $password" )
// Then shows these values into the text view palete using .setText()
text_view.setText("$email " + "$password")
}