Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách đặt EditText chỉ chấp nhận giá trị số trong Android

Giới thiệu

Hướng dẫn này trình bày cách cấu hình EditText chỉ cho phép nhập số (numeric input) trong Android, cả qua XML và qua code Java.

Các bước thực hiện

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.

Bước 2: Thiết kế giao diện (res/layout/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">
    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        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="26dp"
        android:text="Đặt chế độ chỉ nhập số"/>
</RelativeLayout>

Bước 3: Viết logic xử lý (src/MainActivity.java)

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    Button button;
    EditText editText;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        button = findViewById(R.id.button);
        editText = findViewById(R.id.editText);
        
        button.setOnClickListener(v -> {
            // Đặt input type chỉ chấp nhận số
            editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        });
    }
}

Bước 4: Cấu hình Manifest (androidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">
    <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>

Các loại InputType số phổ biến

InputType Mô tả
TYPE_CLASS_NUMBER Chỉ số nguyên (0-9), có dấu âm
TYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_DECIMAL Số thực (cho phép dấu chấm thập phân)
TYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_SIGNED Số có dấu (cho phép dấu +/-)
TYPE_CLASS_PHONE Số điện thoại (*, #, pause, wait)

Cách tốt hơn: Thiết lập ngay trong XML

<EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"           // Chỉ số nguyên
    android:inputType="numberDecimal"    // Số thập phân
    android:inputType="phone"            // Số điện thoại
    android:digits="0123456789"          // Chỉ cho phép các ký tự liệt kê
    android:maxLength="10"               // Giới hạn độ dài
/>

Chạy ứng dụng

Kết nối thiết bị Android, nhấn Run trong Android Studio. Ban đầu EditText cho phép nhập chữ, sau khi nhấn nút sẽ chuyển sang chỉ nhập số.

Cách đặt EditText chỉ chấp nhận giá trị số trong Android

Cách đặt EditText chỉ chấp nhận giá trị số trong Android

Cách đặt EditText chỉ chấp nhận giá trị số trong Android