【问题标题】:Kotlin: How to get and set a text to TextView in Android using Kotlin?Kotlin:如何使用 Kotlin 在 Android 中获取和设置文本到 TextView?
【发布时间】:2017-05-21 12:26:41
【问题描述】:

我是新手。

我想更改TextView的文本,同时点击它。

我的代码:

val text: TextView = findViewById(R.id.android_text) as TextView
    text.setOnClickListener {
        text.setText(getString(R.string.name))
    }

输出:我得到了输出,但显示使用属性访问语法

谁能告诉我怎么做?

提前致谢。

【问题讨论】:

    标签: android textview kotlin


    【解决方案1】:

    在 kotlin 中不要像在 java 中那样使用 getter 和 setter。kotlin 的正确格式如下所示。

    val textView: TextView = findViewById(R.id.android_text) as TextView
    textView.setOnClickListener {
        textView.text = getString(R.string.name)
    }
    

    要从Textview 中获取值,我们必须使用此方法

     val str: String = textView.text.toString()
    
     println("the value is $str")
    

    【讨论】:

    • 作为附加提示,当出现警告时,您可以在 Android Studio 中按 Alt+Enter 以获取修复它的快速操作。
    • 类型也会被推断出来,所以val text: TextViewval str: String不是绝对必要的。
    • 我对Android很陌生,但我不得不使用resources.getString(R.string.name)
    • 可以设置没有ID的文字吗? My Question link
    【解决方案2】:

    只需添加以下行并直接访问 xml 对象

    import kotlinx.android.synthetic.main.activity_main.*
    
    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            txt_HelloWorld.text = "abc"
        }
    

    根据你的 XML 名称替换 activity_main

    【讨论】:

      【解决方案3】:

      在 kotlin 中设置文本

      textview.text = "write here"
      

      【讨论】:

        【解决方案4】:

        从布局中查找文本视图。

        val textView : TextView = findViewById(R.id.android_text) as TextView
        

        在 textview 上设置 onClickListener。

        textview.setOnClickListener(object: View.OnClickListener {
            override fun onClick(view: View): Unit {
                // Code here.
                textView.text = getString(R.string.name)
            }
        })
        

        如果我们传递单个函数文字参数,则可以从 View.setOnClickListener 中省略参数括号。因此,简化的代码将是:

        textview.setOnClickListener {
            // Code here.
            textView.text = getString(R.string.name)
        }
        

        【讨论】:

          【解决方案5】:

          使用textView.text 作为getter 和setter,例如:

          val textView = findViewById<TextView>(R.id.textView)
          // Set text
          textView.text = "Hello World!"
          // Get text
          val textViewString = textView.text.toString()
          

          【讨论】:

            【解决方案6】:

            是的,它已经晚了 - 但可能会对参考的人有所帮助

            带有EditTextButtonTextView

            的xml Button 上的

            onClick 会将值从 EditText 更新为 TextView

                    <EditText
                        android:id="@+id/et_id"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
            
                    <Button
                        android:id="@+id/btn_submit_id"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
            
                    <TextView
                        android:id="@+id/txt_id"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
            

            查看代码在你的类中执行操作

            不需要像 Java 那样初始化组件的 id。您可以通过他们的 xml Id 来做到这一点

            override fun onCreate(savedInstanceState: Bundle?) {
                    super.onCreate(savedInstanceState)
                    setContentView(R.layout.activity_sample)
            
                    btn_submit_id.setOnClickListener {
                        txt_id.setText(et_id.text);
                    }
                }
            

            您也可以在 TextView 中设置值,例如,

            textview.text = "your value"
            

            【讨论】:

              【解决方案7】:
              import kotlinx.android.synthetic.main.MainActivity.*
              
              class Mainactivity : AppCompatActivity() {
              
              
                  override fun onCreate(savedInstanceState: Bundle?) {
                      super.onCreate(savedInstanceState)
                      setContentView(R.layout.MainActivity)
              
                      txt.setText("hello Kotlin")
              
                  }
              
              }
              

              【讨论】:

              • 你在哪里初始化txt??
              • 不需要初始化只需添加kotlin插件
              【解决方案8】:
                <TextView
                      android:id="@+id/usage"
                      android:layout_marginTop="220dip"
                      android:layout_marginLeft="45dip"
                      android:layout_marginRight="15dip"
                      android:typeface="serif"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Google "
                      android:textColor="#030900"/>
              
              
              usage.text="hello world"
              

              【讨论】:

                【解决方案9】:

                findViewById(R.id.android_text) 不需要类型转换。

                【讨论】:

                  【解决方案10】:
                  • 转到 build.gradle(:app) 文件

                  添加依赖{

                   apply plugin: 'kotlin-android'
                      apply plugin: 'kotlin-android-extensions'
                  
                  • 然后转到您的 Activity 文件并写下这一行

                  导入 kotlinx.android.synthetic.main.activity_main.*

                  完成!!

                  现在您可以使用他们的 id 直接找到您的小部件。

                  谢谢

                  【讨论】:

                  【解决方案11】:

                  顶部帖子的末尾附加了“as TextView”。如果您保留它,您可能会遇到其他编译器错误。以下应该没问题。

                  val text: TextView = findViewById(R.id.android_text) as TextView
                  

                  'android_text' 是你的 textView 的 ID

                  【讨论】:

                    【解决方案12】:

                    这个程序对我有用:

                    转到 build.gradle(Module: -> dependencies { 之后 添加这些行:

                    apply plugin: 'kotlin-android'
                    apply plugin: 'kotlin-android-extensions'
                    

                    你可以在这个link观看视频。

                    【讨论】:

                      猜你喜欢
                      • 2019-12-31
                      • 1970-01-01
                      • 2021-06-28
                      • 1970-01-01
                      • 1970-01-01
                      • 2018-11-29
                      • 2018-02-08
                      • 2018-12-29
                      • 2017-11-13
                      相关资源
                      最近更新 更多