Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีนับจำนวนย่อหน้า (Paragraph) ใน TextView บน Android

การนับจำนวนย่อหน้าใน TextView

ตัวอย่างสาธิตการแยกข้อความด้วย Double Newline (\\r\\n\\r\\n) เพื่อนับย่อหน้า แสดงผลผ่าน Toast

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

Android Studio → File → New Project

ขั้นตอนที่ 2: Layout XML (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"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_marginTop="100dp"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </TextView>

</LinearLayout>

ขั้นตอนที่ 3: โค้ด Java (src/MainActivity.java)

package com.example.myapplication;

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

public class MainActivity extends AppCompatActivity {
    TextView text;

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

        // ข้อความมีหลายย่อหน้า (คั่นด้วย blank line)
        text.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. " +
                "Lorem Ipsum has been the industry's standard dummy text ever since the " +
                "1500s, when an unknown printer took a galley of type and scrambled it to " +
                "make a type specimen book. It has survived not only five centuries, but " +
                "also the leap into electronic typesetting, remaining essentially unchanged." +
                "\n\n" +  // <-- คั่นย่อหน้า
                "It was popularised in the 1960s with the release of Letraset sheets " +
                "containing Lorem Ipsum passages, and more recently with desktop publishing " +
                "software like Aldus PageMaker including versions of Lorem Ipsum.");

        // แยกย่อหน้าด้วย Double Newline
        String[] paragraphs = text.getText().toString().split("\r\n\r\n");

        Toast.makeText(MainActivity.this, "Paragraph count: " + paragraphs.length, Toast.LENGTH_LONG).show();
    }
}

การทดสอบ

รันแอป จะแสดง Toast แจ้งจำนวนย่อหน้า (ในตัวอย่างควรเป็น 2)

วิธีนับจำนวนย่อหน้า (Paragraph) ใน TextView บน Android

รายละเอียด Regular Expression

  • \\r\\n\\r\\n = Windows style line ending (CRLF คู่)
  • \\n\\n = Unix/Linux/Mac style (LF คู่)
  • \\r\\r = Old Mac style (CR คู่)

โค้ดที่รองรับทุก Platform (Cross-platform)

// รองรับ \n\n, \r\n\r\n, \r\r
String[] paragraphs = text.getText().toString().split("(\r\n|\r|\n){2}");

// หรือใช้ Pattern
Pattern p = Pattern.compile("(\r\n|\r|\n){2}");
String[] paragraphs = p.split(text.getText().toString());

ข้อควรระวัง

  • ข้อความจาก EditText มักใช้ \n เดียวสำหรับ New line
  • HTML content ใช้ <p> หรือ <br><br> แทน
  • ควร .trim() ก่อน split เพื่อกัน empty paragraph ต้น/ท้าย

การนับแบบ Real-time (ขณะพิมพ์)

editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String[] paras = s.toString().trim().split("(\r\n|\r|\n){2}");
        tvCount.setText("ย่อหน้า: " + (s.toString().trim().isEmpty() ? 0 : paras.length));
    }
    // ... implement other methods
});