이 예제는 안드로이드 앱에서 EditText(입력 필드)에 입력된 값을 가져오는 방법을 보여줍니다. 사용자가 버튼을 클릭하면 EditText에 입력한 텍스트를 읽어와 화면의 TextView에 그대로 출력하는 간단한 데모입니다.
1단계 — 새 프로젝트 만들기
Android Studio에서 File → New Project 메뉴로 이동한 뒤, 새 프로젝트 생성에 필요한 정보를 모두 입력하여 프로젝트를 만듭니다.
2단계 — 레이아웃 XML 작성
res/layout/activity_main.xml 파일에 다음 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <EditText android:id="@+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/button" android:layout_centerInParent="true"/> <Button android:id="@+id/button" android:text="Get value of " android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true"/> <TextView android:id="@+id/textView" android:textSize="16sp" android:textStyle="bold" android:layout_centerInParent="true" android:layout_below="@id/button" android:layout_marginTop="8dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
위 레이아웃은 화면 중앙에 EditText, Button, TextView를 세로로 배치합니다. EditText에는 사용자가 텍스트를 입력하고, 버튼을 누르면 그 값이 바로 아래 TextView에 표시되는 구조입니다.
3단계 — MainActivity 자바 코드 작성
src/MainActivity.java 파일에 다음 코드를 추가합니다.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button button;
EditText editText;
String string;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
editText = findViewById(R.id.editText);
textView = findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
string = editText.getText().toString();
textView.setText(string);
}
});
}
}
핵심은 editText.getText().toString() 부분입니다. getText()가 반환하는 Editable 객체를 toString()으로 문자열로 변환한 뒤, 이 값을 textView.setText()에 전달하여 화면에 출력합니다. 이 한 줄이 EditText의 값을 읽어오는 핵심 로직입니다.
4단계 — AndroidManifest.xml 설정
androidManifest.xml 파일에 다음 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
앱 실행 및 결과 확인
이제 애플리케이션을 실행해 보겠습니다. 실제 안드로이드 기기를 컴퓨터에 연결했다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤, 툴바의 Run(실행) 아이콘을 클릭하세요. 기기 목록에서 본인의 모바일 기기를 선택하면, 기기 화면에 아래와 같은 기본 화면이 나타납니다.
