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

วิธีใช้เมธอด getChars() ใน TextView ของ Android

ภาพรวม

ตัวอย่างนี้สาธิตการใช้เมธอด getChars() ของคลาส String เพื่อคัดลอกอักขระจาก String ไปยัง Array ของ char ตั้งแตตำแหน่งเริ่มต้นจนถึงตำแหน่งสิ้นสุด

ขั้นตอนการสร้างโปรเจกต์

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

Android Studio → File → New Project → Empty Activity

ขั้นตอนที่ 2: Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="ป้อนข้อความ" />

    <Button
        android:id="@+id/click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ดึงอักขระ" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp" />

</LinearLayout>

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

package com.example.myapplication;

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

public class MainActivity extends AppCompatActivity {

    EditText name;
    Button button;
    TextView text;

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

        name = findViewById(R.id.name);
        button = findViewById(R.id.click);
        text = findViewById(R.id.textview);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = name.getText().toString();
                
                if (input.isEmpty()) {
                    name.setError("กรุณาป้อนข้อความ");
                    return;
                }

                // ดึงอักขระตั้งแต่ตำแหน่งที่ 9 จนถึงสุดสตริง
                int start = 9;
                int end = input.length();
                
                if (start < end) {
                    char[] chars = new char[end - start];
                    input.getChars(start, end, chars, 0);
                    text.setText("อักขระจากตำแหน่ง " + start + ": " + new String(chars));
                } else {
                    text.setText("ข้อความสั้นเกินไป (ต้องมากกว่า 9 ตัวอักษร)");
                }
            }
        });
    }
}

ไวยากรณ์ของ getChars()

public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
  • srcBegin: ตำแหน่งเริ่มต้นใน String ต้นทาง (0-based)
  • srcEnd: ตำแหน่งสิ้นสุด (ไม่รวม) ใน String ต้นทาง
  • dst: Array char ปลายทางสำหรับรับอักขระ
  • dstBegin: ตำแหน่งเริ่มต้นใน Array ปลายทาง

ตัวอย่างผลลัพธ์

ข้อความที่ป้อนstart=9, end=lengthผลลัพธ์
"tutorialspoint"ตำแหน่ง 9-13"point"
"Hello World!"ตำแหน่ง 9-12"ld!"
"Short"-Error: ข้อความสั้นเกินไป

กรณีการใช้งาน

  • แยกส่วนของสตริงเพื่อประมวลผลต่อ (Parsing)
  • ดึงข้อมูลจากรูปแบบคงที่ เช่น วันที่ รหัสสินค้า
  • การจัดการข้อความระดับต่ำ (Low-level string manipulation)