Android: EditText trong Android

Các khóa học qua video:
Python SQL Server PHP C# Lập trình C Java HTML5-CSS3-JavaScript
Học trên YouTube <76K/tháng. Đăng ký Hội viên
Viết nhanh hơn - Học tốt hơn
Giải phóng thời gian, khai phóng năng lực

EditText là TextView có thể chỉnh sửa, điền được nội dung. Nó là một lớp phụ của TextView mà bao gồm các khả năng chỉnh sửa đa dạng.

edit text trong Android
Một số style trong EditText

Các thuộc tính của EditText trong Android

Bảng dưới liệt kê một số thuộc tính quan trọng liên quan tới EditText Control.

Kế thừa từ lớp android.widget.TextView:

Thuộc tính Miêu tả
android:autoText Nếu được thiết lập, xác định rằng TextView này có một phương thức đầu vào thuần văn bản và tự động sửa một số lỗi chính tả phổ biến
android:drawableBottom Đây là drawable để được vẽ dưới text
android:drawableRight Đây là drawable để được vẽ sang bên phải của text
android:editable Nếu được thiết lập, xác định rằng TextView này có một phương thức đầu vào
android:text Đây là Text để hiển thị

Kế thừa từ lớp android.view.View:

Thuộc tính Miêu tả
android:background Đây là drawable để sử dụng như một background
android:contentMiêu tả Định nghĩa text mà miêu tả ngắn gọn nội dung của view
android:id Cung cấp một tên định danh cho view này
android:onClick Đây là tên phương thức trong ngữ cảnh của View để triệu hồi khi view được click
android:visibility Điều khiển tính nhìn thấy ban đầu của view

Ví dụ

Ví dụ sau đưa minh họa cách sử dụng EditText.

Sau đây là nội dung của file MainActivity.java:

package v1study.com.edittextv1study;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

  EditText eText;
  Button btn;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    eText = findViewById(R.id.editText);
    btn = findViewById(R.id.btnButton);
    
    btn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        String str = eText.getText().toString();
        Toast msg = Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG);
        msg.show();
      }
    });
  }
}

Sau đây là nội dung của file res/layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://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:background="#CDDC39"
    android:text="@string/demo_edittext"
    android:textColor="#009688"
    android:textFontWeight="@android:integer/status_bar_notification_info_maxnum"
    android:textSize="24sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.12"/>

  <Button
    android:id="@+id/btnButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/show_the_text"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.244"/>

  <EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:width="255dp"
    android:autofillHints=""
    android:inputType=""
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.39"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Sau đây là nội dung của res/values/strings.xml:

<resources>
  <string name="app_name">EditTextV1Study</string>
  <string name="demo_edittext">Demo EditText</string>
  <string name="show_the_text">Show the text</string>
  <string name="enter_text">Enter text</string>
</resources>

Chạy ứng dụng, bạn điền vào phần EditText và nhấn nút "Show the text" sẽ được kết quả như hình sau:

EditText trong Android

» Tiếp: AutoCompleteTextView trong Android
« Trước: TextView trong Android
Các khóa học qua video:
Python SQL Server PHP C# Lập trình C Java HTML5-CSS3-JavaScript
Học trên YouTube <76K/tháng. Đăng ký Hội viên
Viết nhanh hơn - Học tốt hơn
Giải phóng thời gian, khai phóng năng lực
Copied !!!