如何在Kotlin中使用TextWatcher类?

How to use the TextWatcher class in kotlin?

此示例演示如何在kotlin中使用TextWatcher类。

步骤1 ? 在Android Studio中创建一个新项目,转到"文件"→"新项目",并填写所有必需的详细信息以创建一个新项目。

第2步 ? 将以下代码添加到res / layout / activity_main.xml。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:padding="12dp"
 tools:context=".MainActivity">
 <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerHorizontal="true"
   android:layout_marginTop="50dp"
   android:text="Tutorials Point"
   android:textAlignment="center"
   android:textColor="@android:color/holo_green_dark"
   android:textSize="32sp"
   android:textStyle="bold" />
 <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_above="@id/etInput"
   android:layout_centerInParent="true"
   android:layout_marginBottom="30dp"
   android:text="Android Text Watcher"
   android:textColor="@android:color/holo_orange_dark"
   android:textSize="24sp"
   android:textStyle="bold" />
 <EditText
   android:id="@+id/etInput"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_centerInParent="true"
   android:hint="Input"
   android:maxLength="15" />
 <TextView
   android:id="@+id/textView"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_below="@id/etInput"
   android:layout_centerHorizontal="true"
   android:layout_marginTop="12dp"
   android:textColor="@android:color/holo_red_dark"
   android:textSize="24sp"
   android:textStyle="bold|italic" />
</RelativeLayout>

第3步? 将以下代码添加到MainActivity.kt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
 lateinit var input: EditText
 lateinit var output: TextView
 override fun onCreate(savedInstanceState: Bundle?) {
   super.onCreate(savedInstanceState)
   setContentView(R.layout.activity_main)
   title ="KotlinApp"
   input = findViewById(R.id.etInput)
   output = findViewById(R.id.textView)
   input.addTextChangedListener(textWatcher)
 }
 private val textWatcher = object : TextWatcher {
   override fun afterTextChanged(s: Editable?) {
    }
    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    }
    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
      output.text = s
      if (start == 12) {
       Toast.makeText(applicationContext,"Maximum Limit Reached", Toast.LENGTH_SHORT)
       .show()
      }
    }
   }
}

第四步 ? 将以下代码添加到androidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="app.com.kotlipapp">
 <application
   android:allowBackup="true"
   android:icon="@mipmap/ic_launcher"
   android:label="@string/app_name"
   android:roundIcon="@mipmap/ic_launcher_round"
   android:supportsRtl="true"
   android:theme="@style/AppTheme">
   
    <intent-filter>
     
      <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
   </activity>
   </application>
</manifest>

让我们尝试运行您的应用程序。 我假设您已将实际的Android Mobile设备与计算机连接。 要从android studio运行该应用,请打开您项目的活动文件之一,然后从工具栏中点击运行图标。 选择您的移动设备作为选项,然后检查将显示默认屏幕的移动设备-

单击此处下载项目代码。