Giới thiệu
Ví dụ minh họa cách giới hạn EditText chỉ chấp nhận nhập số (numeric) trên Android bằng Kotlin, cả cách đặt trong XML và thay đổi động trong code.
Các bước thực hiện
Bước 1: Tạo dự án mới
Android Studio > File > New Project.
Bước 2: Layout (activity_main.xml) - Cách XML (Khuyên dùng)
<?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="Đặt inputType số bằng code" />
</RelativeLayout>
Bước 3: MainActivity.kt - Thay đổi động
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(R.id.editText)
button.setOnClickListener {
// Đặt input type chỉ số bằng code
editText.inputType = InputType.TYPE_CLASS_NUMBER
// Hoặc cho phép số thập phân: InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL
// Hoặc số có dấu: InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED
}
}
}
Bước 4: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.numericinput">
<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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Chạy ứng dụng
Chạy app, EditText mặc định đã chỉ cho nhập số (do XML android:inputType="number"). Nhấn nút để thấy việc đặt lại bằng code cũng cho cùng kết quả.
Các loại InputType số phổ biến
| Hằng số / XML | Mô tả | Bàn phím hiển thị |
|---|---|---|
number / TYPE_CLASS_NUMBER | Chỉ số nguyên (0-9) | Numpad |
numberDecimal / TYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_DECIMAL | Số thực (có dấu chấm) | Numpad + dấu chấm |
numberSigned / TYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_SIGNED | Số có dấu âm/dương | Numpad + dấu trừ |
phone / TYPE_CLASS_PHONE | Số điện thoại (*, #, +) | Dialpad |
Kết hợp nhiều flag (XML)
android:inputType="number|numberDecimal|numberSigned"
Kết hợp nhiều flag (Code)
editText.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL or InputType.TYPE_NUMBER_FLAG_SIGNED
Validation nâng cao
inputTypechỉ kiểm soát bàn phím và bộ lọc ký tự cơ bản. Người dùng vẫn có thể paste văn bản không hợp lệ.- Nên thêm
TextWatcherhoặcInputFilterđể validate chặt chẽ:
// Chỉ cho phép số và một dấu chấm
editText.filters = arrayOf(
InputFilter { source, start, end, dest, dstart, dend ->
val result = StringBuilder(dest).replace(dstart, dend, source.subSequence(start, end).toString())
if (result.toString().matches("^\\d*\\.?\\d*$".toRegex())) null else ""
}
)