Giới thiệu
Ví dụ minh họa cách thiết lập kiểu nhập liệu (InputType) cho EditText bằng code Kotlin thay vì trong XML. Cách này hữu ích khi cần thay đổi động theo điều kiện.
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)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp">
<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="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_centerInParent="true"
android:onClick="setInputTypeNumber"
android:text="Đặt kiểu nhập Số" />
</RelativeLayout>
Bước 3: MainActivity.kt
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.InputType
import android.view.View
import android.widget.EditText
class MainActivity : AppCompatActivity() {
private lateinit var editText: EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
editText = findViewById(R.id.editText)
}
fun setInputTypeNumber(view: View) {
editText.inputType = InputType.TYPE_CLASS_NUMBER
}
}
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.inputtype">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
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 ban đầu chấp nhận mọi ký tự. Nhấn nút "Đặt kiểu nhập Số", bàn phím sẽ chuyển sang numpad, chỉ cho nhập số.
Các hằng số InputType phổ biến
| Hằng số (Code) | Giá trị XML (android:inputType) | Mô tả |
|---|---|---|
TYPE_CLASS_TEXT | text | Văn bản thường |
TYPE_CLASS_NUMBER | number | Chỉ số nguyên |
TYPE_CLASS_PHONE | phone | Số điện thoại |
TYPE_CLASS_DATETIME | date|time | Ngày/Giờ |
TYPE_TEXT_VARIATION_EMAIL_ADDRESS | textEmailAddress | Email (có @, .com) |
TYPE_TEXT_VARIATION_PASSWORD | textPassword | Mật khẩu (ẩn chữ) |
TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | textVisiblePassword | Mật khẩu (hiện chữ) |
TYPE_TEXT_FLAG_MULTI_LINE | textMultiLine | Nhiều dòng |
TYPE_TEXT_FLAG_CAP_SENTENCES | textCapSentences | Viết hoa đầu câu |
TYPE_TEXT_FLAG_NO_SUGGESTIONS | textNoSuggestions | Tắt gợi ý từ điển |
Kết hợp nhiều flag (Bitwise OR)
// Số có dấu và thập phân editText.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED or InputType.TYPE_NUMBER_FLAG_DECIMAL // Email + không gợi ý editText.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS or InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS // Mật khẩu nhiều dòng (hiếm) editText.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_TEXT_FLAG_MULTI_LINE
Lưu ý quan trọng
- Thay đổi
inputTypeở runtime có thể khiến bàn phím ẩn/hiện lại (re-focus). - Nên thiết lập trong XML nếu không thay đổi động để tránh giật lag.
inputTypecũng ảnh hưởng đến hành vi tự động điền (autofill), kiểm tra chính tả, v.v.- Để giới chế ký tự chặt chẽ (ví dụ: chỉ số, max 2 chữ số thập phân), dùng
InputFilterkết hợp.