个性化阅读
专注于IT技术分析

Kotlin Android TextView和EditText

点击下载

Android TextView是一个向用户显示文本的用户界面。

下面显示了布局中TextView的简单XML代码。

<TextView
        android:id="@+id/text_view_id"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="hello" />

我们可以获取和修改在Kotlin类文件的XML布局中定义的文本视图的内容, 如下所示:

val textView = findViewById<TextView>(R.id.text_view_id)
textView.setText("string").toString()
val textViewValue = textView.text

EditText是一个用于输入和更改文本的用户界面。在XML布局中使用编辑文本时, 我们必须指定其inputType属性, 该属性根据提及的输入类型来配置键盘。

布局中的EditText的简单XML代码如下所示。

<EditText
        android:id="@+id/editText_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="" />

我们可以按以下方式获取和修改在Kotlin类文件的XML布局中定义的编辑文本的内容:

val editText = findViewById<EditText>(R.id.editText_id) 
val editTextValue = editText.text

Kotlin Android TextView和ExitText示例

在此示例中, 我们在ExitText中输入文本值, 并在单击Button时将其值显示在TextView中。

我们还将观察使用addTextChangedListener()方法和TextWatcher接口在EditText上所做的更改。

activity_main.xml

在activity_main.xml文件中, 添加以下代码。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="example.srcmini02.com.kotlintextviewedittext.MainActivity">


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="12dp"
        android:text="TextView and EditText"
        android:gravity="center"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="90dp"
        android:layout_marginLeft="20dp"
        android:layout_marginStart="20dp"
        android:textSize="16sp"
        android:text="Provide Input" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="20dp"
        android:layout_marginStart="20dp"
        android:textSize="16sp"
        android:text="Display Output" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginEnd="21dp"
        android:layout_marginRight="21dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText"
        android:layout_alignStart="@+id/editText"
        android:layout_alignTop="@+id/textView3"
        android:textSize="16sp"
        android:text="TextView" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView4"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="112dp"
        android:text="Click Button" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/editText"
        android:layout_alignRight="@+id/editText"
        android:layout_centerVertical="true"
        android:text="reset output text" />
</RelativeLayout>

MainActivity.kt

在MainActivity.kt类中添加以下代码。在此类中, 我们将获取编辑文本的值, 并通过单击按钮将其显示在文本视图中。同时, 我们还正在监视使用addTextChangedListener()方法和TextWatcher接口在EditText上所做的更改。要了解有关TextWatcher的更多信息, 请参阅https://www.srcmini02.com/android-edittext-with-textwatcher

package example.srcmini02.com.kotlintextviewedittext

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.TextView
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button.setOnClickListener(){
            val inputValue: String = editText.text.toString()
            if (inputValue == null || inputValue.trim()==""){
                Toast.makeText(this, "please input data, edit text cannot be blank", Toast.LENGTH_LONG).show()
            }else{
                textView4.setText(inputValue).toString()
            }
        }
        textView5.setOnClickListener(){
            if (textView4.text.toString() == null || textView4.text.toString().trim()==""){
                Toast.makeText(this, "clicked on reset textView, \n output textView already reset", Toast.LENGTH_LONG).show()
            }else{
                textView4.setText("").toString()
            }
        }
        editText.addTextChangedListener(object: TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
              //  TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                Toast.makeText(applicationContext, "executed before making any change over EditText", Toast.LENGTH_SHORT).show()
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
              //  TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                Toast.makeText(applicationContext, "executed while making any change over EditText", Toast.LENGTH_SHORT).show()
            }
            override fun afterTextChanged(p0: Editable?) {
              //  TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
                Toast.makeText(applicationContext, "executed after change made over EditText", Toast.LENGTH_SHORT).show()
            }
        })
    }
}

输出:

Kotlin Android TextView和EditText
Kotlin Android TextView和EditText
Kotlin Android TextView和EditText
赞(0)
未经允许不得转载:srcmini » Kotlin Android TextView和EditText

评论 抢沙发

评论前必须登录!