How to access values from strings.xml

前端 未结 4 852
再見小時候
再見小時候 2020-12-09 15:18

How can I access values from strings.xml in kotlin android?

class MainActivity : AppCompatActivity(), View.OnClickListener {
    override fun onClick(p0: Vie         


        
4条回答
  •  星月不相逢
    2020-12-09 15:34

    Accessing string values in Kotlin works exactly the same as it does in Java, just with Kotlin syntax:

    val string: String = getString(R.string.your_string_id)
    

    (This assumes that your code is inside an Android Activity, or a similar class that inherits from Context. If not, you need to get a Context instance from somewhere, and change it to myContext.getString(...).)

提交回复
热议问题