Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีตั้งค่า EditText ให้รับเฉพาะตัวเลขใน Android ด้วย Kotlin

ภาพรวม

ตัวอย่างนี้สาธิตวิธีการจำกัด EditText ให้รับเฉพาะตัวเลข (Numeric Input) ใน Android โดยใช้ Kotlin ทั้งใน XML และโดยโปรแกรม

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

สร้างโปรเจกต์ Android ใหม่ใน Android Studio

ขั้นตอนที่ 2: จัดรูปแบบหน้าจอหลัก (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:background="#008080"
        android:padding="5dp"
        android:text="TutorialsPoint"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />
    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:inputType="number"
        android:ems="10" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="Click Here to set numeric value for EditText" />
</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ด MainActivity.kt

วิธีที่ 1: ตั้งค่าใน XML (แนะนำ) - ใช้ `android:inputType="number"`

วิธีที่ 2: ตั้งค่าโดยโปรแกรม (Programmatically):

import android.os.Bundle
import android.text.InputType
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"

        val button: Button = findViewById(R.id.button)
        val editText = findViewById<EditText>(R.id.editText)

        button.setOnClickListener {
            // ตั้งค่า Input Type เป็นตัวเลข
            editText.inputType = InputType.TYPE_CLASS_NUMBER
            // หรือรวมกับตัวเลือกอื่นๆ เช่น:
            // editText.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL (ทศนิยม)
            // editText.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED (ลบได้)
        }
    }
}

ขั้นตอนที่ 4: ตั้งค่า AndroidManifest.xml

ใช้การตั้งค่ามาตรฐาน

ทดสอบการทำงาน

รันแอป EditText จะแสดงคีย์บอร์ดตัวเลขเท่านั้น

InputType ตัวเลือกที่เกี่ยวข้องกับตัวเลข

Constantคำอธิบาย
TYPE_CLASS_NUMBERตัวเลขเท่านั้น
TYPE_NUMBER_FLAG_DECIMALอนุญาตจุดทศนิยม
TYPE_NUMBER_FLAG_SIGNEDอนุญาตเครื่องหมายลบ
TYPE_CLASS_PHONEคีย์บอร์ดโทรศัพท์ (มี *, #)
TYPE_CLASS_DATETIMEวันที่/เวลา

รวมได้ด้วยตัวดำเนินการ OR: `InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL`