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

วิธีใช้ StringBuffer ใน Android

ก่อนจะเริ่มตัวอย่าง เราควรเข้าใจก่อนว่า StringBuffer คืออะไร คลาส StringBuffer ใช้สร้างสตริงที่เปลี่ยนแปลงได้ (mutable) และเป็น thread-safe ตัวอย่างนี้สาธิตวิธีการใช้ StringBuffer ใน Android

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

เปิด Android Studio ไปที่ File → New Project แล้วกรอกรายละเอียดที่จำเป็นเพื่อสร้างโปรเจกต์ใหม่

ขั้นตอนที่ 2: ออกแบบหน้าจอ (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">

    <EditText
        android:id="@+id/edit_query"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter string" />

    <Button
        android:id="@+id/click"
        android:layout_marginTop="50dp"
        style="@style/Base.TextAppearance.AppCompat.Widget.Button.Borderless.Colored"
        android:layout_width="wrap_content"
        android:background="#c1c1c1"
        android:textColor="#FFF"
        android:layout_height="wrap_content"
        android:text="Button" />

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

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

ในโค้ดด้านบน เรามี EditText, Button และ TextViews 5 ตัว เมื่อผู้ใช้กดปุ่ม ข้อมูลจาก EditText จะถูกจัดการด้วยเมธอดของ StringBuffer ผลลัพธ์จะแสดงใน TextViews

ขั้นตอนที่ 3: เขียนโค้ด MainActivity.java

package com.example.myapplication;

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

public class MainActivity extends AppCompatActivity {
    EditText edit_query;

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

        edit_query = findViewById(R.id.edit_query);
        final TextView textView = findViewById(R.id.text);
        final TextView textView1 = findViewById(R.id.text1);
        final TextView textView2 = findViewById(R.id.text2);
        final TextView textView3 = findViewById(R.id.text3);
        final TextView textView4 = findViewById(R.id.text4);

        findViewById(R.id.click).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!edit_query.getText().toString().isEmpty()) {
                    StringBuffer stringBuffer = new StringBuffer(edit_query.getText().toString());
                    
                    // 1. แสดงสตริงต้นฉบับ
                    textView.setText("Default String: " + stringBuffer);
                    
                    // 2. insert() - แทรกข้อความที่ตำแหน่ง 0
                    textView1.setText(stringBuffer.insert(0, "insert at 0 " + "tutorialspoint.com"));
                    
                    // 3. capacity() - แสดงความจุปัจจุบัน
                    textView2.setText("Capacity is " + stringBuffer.capacity());
                    
                    // 4. delete() - ลบตัวอักษรตำแหน่ง 1-3
                    textView3.setText("Delete: " + stringBuffer.delete(1, 3));
                    
                    // 5. append() - เพิ่มข้อความท้ายสตริง
                    textView4.setText("Append: " + stringBuffer.append(" appended to string buffer"));
                }
            }
        });
    }
}

การทดสอบแอปพลิเคชัน

เชื่อมต่ออุปกรณ์ Android จริงกับคอมพิวเตอร์ ใน Android Studio เปิดไฟล์ Activity และคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณ แล้วตรวจสอบหน้าจอเริ่มต้น

วิธีใช้ StringBuffer ใน Android

ผลลัพธ์แสดงการจัดการสตริงด้วย StringBuffer (เหมือน StringBuilder แต่ thread-safe):

  • สตริงต้นฉบับ
  • หลัง insert() ที่ตำแหน่ง 0
  • ความจุ (capacity) ของ buffer
  • หลัง delete() ตำแหน่ง 1-3
  • หลัง append() ข้อความใหม่

ความแตกต่าง StringBuilder vs StringBuffer

คุณสมบัติStringBuilderStringBuffer
Thread-safeไม่ใช่ใช่ (synchronized)
ประสิทธิภาพเร็วกว่าช้ากว่าเล็กน้อย
ใช้เมื่อไหร่Single-threadedMulti-threaded