Computer >> 컴퓨터 >  >> 프로그래밍 >> Android

Android에서 TextView 문자열의 모든 모음을 제거하는 방법

이 예제는 Android에서 TextView 문자열로부터 모든 모음(vowel)을 제거하는 방법을 보여줍니다.

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"
   tools:context = ".MainActivity"
   android:orientation = "vertical">
   <EditText
      android:id = "@+id/name"
      android:layout_width = "match_parent"
      android:hint = "Enter Name"
      android:layout_height = "wrap_content" />
   <LinearLayout
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content">
      <Button
         android:id = "@+id/save"
         android:text = "Save"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content" />
   </LinearLayout>
   <TextView
      android:id = "@+id/textview"
      android:layout_width = "match_parent"
      android:layout_height = "match_parent" />
</LinearLayout>

위 코드에서는 이름을 입력받는 EditText와 저장 버튼, 결과를 표시할 TextView를 배치했습니다. 사용자가 버튼을 클릭하면 EditText에 입력된 값에서 모음이 제거된 문자열이 화면에 출력됩니다.

3단계 — MainActivity 코드 작성

src/MainActivity.java 파일에 아래 코드를 추가합니다.

package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
   EditText name;
   HashMap<Character, Integer> charCountMap;
   TextView textview;
   @Override
   protected void onCreate(Bundle readdInstanceState) {
      super.onCreate(readdInstanceState);
      setContentView(R.layout.activity_main);
      name = findViewById(R.id.name);
      textview = findViewById(R.id.textview);
      charCountMap = new HashMap<>();
      findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            if (!name.getText().toString().isEmpty()) {
               String removed = name.getText().toString().replaceAll("[AEIOUaeiou]", "");
               textview.setText(removed);
               Toast.makeText(MainActivity.this, "Inserted", Toast.LENGTH_LONG).show();
            } else {
               name.setError("Enter NAME");
            }
         }
      });
   }
}

핵심 로직 설명

모음 제거의 핵심은 replaceAll() 메서드입니다. 정규 표현식 [AEIOUaeiou]를 사용하면 대문자와 소문자의 모든 영어 모음(A, E, I, O, U)을 한 번에 찾아 빈 문자열("")로 대체할 수 있습니다. 또한 입력값이 비어 있는지 확인한 후, 비어 있으면 setError()를 통해 사용자에게 이름을 입력하도록 안내합니다.

애플리케이션 실행

애플리케이션을 실행해 보겠습니다. 실제 Android 모바일 기기가 컴퓨터에 연결되어 있다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤, 툴바에서 Run 아이콘을 클릭합니다. 실행 기기 목록에서 자신의 모바일 기기를 선택하면, 모바일 화면에 기본 화면이 표시됩니다.

Android에서 TextView 문자열의 모든 모음을 제거하는 방법

위 실행 결과에서 볼 수 있듯이, 이름을 입력하고 버튼을 누르면 TextView에는 모음이 제거된 텍스트만 출력되는 것을 확인할 수 있습니다.