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

Cách vô hiệu hóa sao chép/dán trong EditText trên Android bằng Kotlin

Giới thiệu

Hướng dẫn này minh họa cách vô hiệu hóa chức năng sao chép (copy), cắt (cut) và dán (paste) trên EditText trong ứng dụng Android sử dụng Kotlin. Kỹ thuật này hữu ích khi bạn muốn bảo vệ dữ liệu nhạy cảm như mật khẩu, mã OTP hoặc thông tin thanh toán.

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, điền các thông tin cần thiết để tạo dự án mới.

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"
    android:padding="8dp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="Cut/copy/paste is Disabled"
        android:textColor="@android:color/background_dark"
        android:textSize="22sp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

Bước 3: Xử lý logic (src/MainActivity.kt)

import android.os.Bundle
import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity

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)

        // Vô hiệu hóa ActionMode (menu sao chép/dán/cắt)
        editText.customSelectionActionModeCallback = object : ActionMode.Callback {
            override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean = false
            override fun onDestroyActionMode(mode: ActionMode) {}
            override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean = false
            override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean = false
        }
    }
}

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.q11">

    <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

Kết nối thiết bị Android thật với máy tính qua USB. Trong Android Studio, nhấn biểu tượng Run (mũi tên xanh) trên thanh công cụ, chọn thiết bị của bạn và quan sát kết quả. Khi long-press vào EditText, menu sao chép/dán sẽ không xuất hiện.

Giải thích kỹ thuật

Phương pháp này hoạt động bằng cách gán một ActionMode.Callback tùy chỉnh cho customSelectionActionModeCallback của EditText. Bằng cách trả về false cho onCreateActionMode và onPrepareActionMode, chúng ta ngăn chặn việc hiển thị thanh công cụ contextual action bar (CAB) chứa các tùy chọn Cut/Copy/Paste.

Lưu ý: Cách này không ngăn chặn sao chép/dán qua bàn phím (như Gboard clipboard). Để bảo mật cao hơn, hãy kết hợp với android:importantForAutofill="no" và android:autofillHints="".