Android
 Computer >> コンピューター >  >> プログラミング >> Android

AndroidのTextViewでconcat()を使って文字列を連結する方法

このチュートリアルでは、Androidアプリ開発においてconcat()メソッドを使って文字列を連結し、TextViewに表示する方法を解説します。具体的には、EditTextに入力された名前と「tutorialspoint.com」という文字列を連結して表示するサンプルアプリを作成します。

ステップ1:新規プロジェクトの作成

Android Studioで新しいプロジェクトを作成します。メニューから「File → New Project」を選択し、必要な項目をすべて入力してプロジェクトを作成しましょう。

ステップ2:レイアウトファイル(activity_main.xml)の編集

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:hint="Enter name"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/click"
        android:text="Click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:textSize="25sp"
        android:layout_height="wrap_content" />
</LinearLayout>

上記のコードでは、名前を入力するためのEditText、クリック用のButton、結果を表示するTextViewを、縦方向のLinearLayoutの中に配置しています。ボタンがクリックされると、EditTextに入力されたデータを取得し、「tutorialspoint.com」という文字列と連結(concat)してTextViewに表示します。

ステップ3:MainActivity.javaの編集

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) {
                if (!name.getText().toString().isEmpty()) {
                    if (name.getText().toString().length() >=0) {
                        text.setText(String.valueOf(name.getText().toString().concat(" tutorialspoint.com")));
                    }
                } else {
                    name.setError("Plz enter name");
                }
            }
        });
   }
}

このコードでは、ボタンのクリックリスナーを設定し、EditTextが空でない場合に入力値と「 tutorialspoint.com」をconcat()で連結してTextViewにセットしています。入力が空の場合は「Plz enter name(名前を入力してください)」というエラーメッセージを表示します。

アプリの実行

それでは、アプリケーションを実行してみましょう。ここでは、実際のAndroid端末がパソコンに接続されているものとします。Android Studioからアプリを実行するには、プロジェクト内のアクティビティファイルを開き、ツールバーの実行(Run)アイコンをクリックします。実行先のデバイスとして自分のモバイルデバイスを選択すると、端末に以下のような画面が表示されます。

AndroidのTextViewでconcat()を使って文字列を連結する方法

実行結果の確認

上記の例では、EditTextに「sairam」と入力してボタンをタップしています。その結果、画面には「sairam tutorialspoint.com」と表示されます。これは、入力されたEditTextの値に対して「tutorialspoint.com」をconcat()メソッドで連結しているためです。

このように、Stringクラスのconcat()メソッドを使えば、既存の文字列に別の文字列を簡単に連結でき、TextViewへの動的なテキスト表示にも活用できます。

  1. AndroidのTextViewでstartsWith()メソッドを使う方法

    AndroidのTextViewでstartsWith()メソッドを使う方法この記事では、Androidアプリ開発においてstartsWith()メソッドを使用し、入力された文字列が特定の接頭辞(プレフィックス)で始まるかどうかを判定して、その結果をTextViewに表示する方法を解説します。startsWith()はJavaのStringクラスが提供するメソッドの一つで、対象の文字列が引数に指定した文字列で始まっていればtrueを、そうでなければfalseを返します。ユーザー入力のバリデーションや文字列の絞り込み処理など、さまざまな場面で活用できる便利なメソッドです。手順1:新規プロジェクト

  2. Android TextViewでlastIndexOf()を使う方法をわかりやすく解説

    この記事では、Androidアプリ開発においてTextViewと組み合わせてlastIndexOf()メソッドを使用する方法を、具体的なサンプルコードとともに解説します。 lastIndexOf()メソッドとは lastIndexOf()は、JavaのStringクラスが提供するメソッドの一つで、指定した文字列(または文字)が対象の文字列内で最後に出現する位置(インデックス)を返します。先頭から検索するindexOf()とは異なり、末尾側から検索を行うのが特徴です。なお、文字列が見つからなかった場合は「-1」が返されます。 作成するサンプルアプリの概要 今回作成するアプリは、以下のような流