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

วิธีใช้ concat() ใน TextView Android เพื่อต่อข้อความ

ภาพรวม

บทความนี้สาธิตการใช้เมธอด String.concat() เพื่อต่อข้อความ (Concatenate) ใน Android TextView

ขั้นตอนที่ 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"
    android:padding="16dp"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="ป้อนชื่อ"
        android:inputType="text" />

    <Button
        android:id="@+id/click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="ต่อข้อความ" />

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

</LinearLayout>

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

package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private EditText nameEditText;
    private Button concatButton;
    private TextView resultTextView;

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

        nameEditText = findViewById(R.id.name);
        concatButton = findViewById(R.id.click);
        resultTextView = findViewById(R.id.textview);

        concatButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = nameEditText.getText().toString().trim();

                if (input.isEmpty()) {
                    nameEditText.setError("กรุณาป้อนชื่อ");
                    return;
                }

                // ใช้ concat() ต่อข้อความ
                String suffix = " tutorialspoint.com";
                String result = input.concat(suffix);

                resultTextView.setText(result);
            }
        });
    }
}

วิธีต่อข้อความใน Java/Kotlin

วิธีตัวอย่างข้อดี/ข้อเสีย
concat()str.concat(" suffix")Null-safe (throw NPE ถ้า null), Immutable
+ operatorstr + " suffix"สะดวก, Compiler optimize ให้ (StringBuilder)
StringBuildernew StringBuilder().append(str).append(" suffix")ประสิทธิ์ดีสุดสำหรับ loop/หลายครั้ง
String.format()String.format("%s suffix", str)อ่านง่าย สำหรับ format ซับซ้อน
String.join()String.join("", str, " suffix")Java 8+, รวมหลายสตริง

Kotlin Extension (แนะนำ)

// Kotlin
val result = input + " tutorialspoint.com"
// หรือ
val result = input.concat(" tutorialspoint.com")
// String interpolation
val result = "$input tutorialspoint.com"

ทดสอบ

ป้อน "sairam" → แสดง "sairam tutorialspoint.com"
ป้อน "Android" → แสดง "Android tutorialspoint.com"