📜  Kotlin中的TextView

📅  最后修改于: 2021-05-13 16:52:44             🧑  作者: Mango

Android TextView只是一个视图,用于向用户显示文本,并可以选择允许我们对其进行修改或编辑。首先,在Android Studio中打开Kotlin项目。
以下步骤用于在Kotlin中创建TextView

  1. 在LinearLayout内的activity_main.xml文件中添加一个TextView。
  2. 在activity_main.xml文件中添加诸如text,textColor,textSize,textStyle之类的属性。
  3. 打开MainActivity.kt文件,并为TextView设置OnClickListner以显示Toast消息。

TextView小部件的不同属性–

Attributes Description
android:text Sets text of the Textview
android:id Gives a unique ID to the Textview
android:cursorVisible Use this attribute to make cursor visible or invisible. Default value is visible.
android:drawableBottom Sets images or other graphic assets to below of the Textview.
android:drawableEnd Sets images or other graphic assets to end of Textview.
android:drawableLeft Sets images or other graphic assets to left of Textview.
android:drawablePadding Sets padding to the drawable(images or other graphic assets) in the Textview. android:autoLink This attribute is used to automatically detect url or emails and show it as clickable link.
android:autoText Automatically correct spelling errors in text of the Textview.
android:capitalize It automatically capitalize whatever the user types in the Textview.
android:drawableRight Sets drawables to right of text in the Textview.
android:drawableStart Sets drawables to start of text in the Textview.
android:drawableTop Sets drawables to top of text in the Textview.
android:ellipsize Use this attribute when you want text to be ellipsized if it is longer than the Textview width.
android:ems Sets width of the Textview in ems.
android:gravity We can align text of the Textview vertically or horizontally or both.
android:height Use to set height of the Textview.
android:hint Use to show hint when there is no text.
android:inputType Use to set input type of the Textview. It can be Number, Password, Phone etc.
android:lines Use to set height of the Textview by number of lines.
android:maxHeight Sets maximum height of the Textview.
android:minHeight Sets minimum height of the Textview.
android:maxLength Sets maximum character length of the Textview.
android:maxLines Sets maximum lines Textview can have.
android:minLines Sets minimum lines Textview can have.
android:maxWidth Sets maximum width Textview can have.
android:minWidth Sets minimum lines Textview can have.
android:textAllCaps Show all texts of the Textview in capital letters.
android:textColor Sets color of the text.
android:textSize Sets font size of the text.
android:textStyle Sets style of the text. For example, bold, italic, bolditalic.
android:typeface Sets typeface or font of the text. For example, normal, sans, serif etc
android:width Sets width of the TextView.

修改字符串.xml文件

我们可以在字符串.xml文件中添加字符串,并通过使用其名称来调用它们来轻松地在其他文件中使用。


    TextViewInKotlin
    GeeksForGeeks
    COMPUTER SCIENCE PORTAL

activity_main.xml文件

打开activity_main.xml文件,并使用id textView创建一个TextView。



  
    
  
    

打开MainActivity.kt文件并获取在布局文件中定义的TextView的引用。

// finding the textView
 val textView = findViewById(R.id.text_view_id) as TextView

将点击监听器设置为按钮

textView?.setOnClickListener{ Toast.makeText(this@MainActivity,
                "COMPUTER SCIENCE PORTAL", Toast.LENGTH_LONG).show() }

MainActivity.kt文件

打开app / src / main / Java/ yourPackageName /MainActivity.kt以获取TextView的引用。

package com.geeksforgeeks.myfirstkotlinapp
  
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
  
class MainActivity : AppCompatActivity() {
  
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
  
            //accessing our textview from layout
            val textView = findViewById(R.id.text_view_id) as TextView
            textView?.setOnClickListener{ Toast.makeText(this@MainActivity,
                R.string.text_on_click, Toast.LENGTH_LONG).show() }
        }
  
}

AndroidManifest.xml文件

我们还将看到main / AndroidManifest.xml文件中的代码。



  
    
        
            
                
  
                
            
        
    
  

作为仿真器运行以输出:

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!