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

Cách xóa khoảng trắng trong TextView Android

Giới thiệu

Ví dụ này minh họa cách loại bỏ tất cả khoảng trắng (white spaces) từ chuỗi nhập vào EditText và hiển thị kết quả lên TextView trong Android.

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

Mở Android Studio, chọn File → New Project và điền đầy đủ thông tin để tạo dự án.

Bước 2: Thiết kế giao diện (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:hint="Nhập tên"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/save"
            android:text="Lưu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Trong giao diện này, người dùng nhập tên vào EditText. Khi nhấn nút "Lưu", ứng dụng sẽ xóa tất cả khoảng trắng và hiển thị chuỗi kết quả lên TextView.

Bước 3: Code MainActivity.java

package com.example.andy.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText name;
    TextView textview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        name = findViewById(R.id.name);
        textview = findViewById(R.id.textview);

        findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = name.getText().toString();
                if (!input.isEmpty()) {
                    // Sử dụng regex \\s+ để khớp tất cả khoảng trắng (space, tab, newline)
                    String result = input.replaceAll("\\s+", "");
                    textview.setText(result);
                    Toast.makeText(MainActivity.this, "Đã xử lý", Toast.LENGTH_LONG).show();
                } else {
                    name.setError("Vui lòng nhập tên");
                }
            }
        });
    }
}

Chạy ứng dụng

Kết nối thiết bị Android thật, nhấn Run trong Android Studio và chọn thiết bị để kiểm tra.

Cách xóa khoảng trắng trong TextView Android

Kết quả: TextView hiển thị chuỗi đã nhập nhưng không còn bất kỳ khoảng trắng nào.