티스토리 뷰

기차표 예약 화면

동작개요

Spinner로 아이템 선택 시 TextView에 선택한 아이템으로 목적지 변경

EditText로 값 입력 시 TextView에 입력한 값으로 승차인원 변경

RadioButton 선택 시 TextView의 선택한 라디오 버튼으로 좌석 변경

CheckBox 체크 시 TextView에 체크한 체크박스 값 출력

버튼 클릭 시 TextView의 텍스트를 토스트 메시지로 출력 (커스텀 토스트가 아니므로 글자가 잘릴 수 있음)

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="30dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="30dp"
        android:text="기차표 예약"
        android:textSize="30sp" />

    <Spinner
        android:id="@+id/s"
        style="@style/Widget.AppCompat.Spinner.Underlined"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp" />

    <EditText
        android:id="@+id/e"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="인원수"
        android:textSize="25sp"
        android:inputType="number"
        android:padding="10dp"
        android:gravity="center" />

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/r1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="일반실"
            android:textSize="20sp" />

        <RadioButton
            android:id="@+id/r2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="특실"
            android:textSize="20sp" />

    </RadioGroup>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/c1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="창측 선호"
            android:textSize="20sp" />
        <CheckBox
            android:id="@+id/c2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="복도측 선호"
            android:textSize="20sp" />

    </LinearLayout>

    <TextView
        android:id="@+id/t"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="25dp"
        android:textSize="20sp"
        android:background="#C8DCE6" />

    <Button
        android:id="@+id/b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:text="제출"
        android:textSize="20sp" />

</LinearLayout>

 

MainActivity.java

package com.example.test;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    String city[] = {"서울", "부산", "인천", "울산", "광주", "대전", "대구"};
    String p1 = "";
    String p2 = "";
    String p3;

    RadioButton r1;
    RadioButton r2;
    CheckBox c1;
    CheckBox c2;
    TextView t;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Spinner s = findViewById(R.id.s);
        ArrayAdapter a = new ArrayAdapter(this, android.R.layout.simple_spinner_item, city);
        s.setAdapter(a);

        EditText e = findViewById(R.id.e);
        r1 = findViewById(R.id.r1);
        r2 = findViewById(R.id.r2);
        c1 = findViewById(R.id.c1);
        c2 = findViewById(R.id.c2);
        t = findViewById(R.id.t);
        Button b = findViewById(R.id.b);

        s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                p1 = city[i];
                update();
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });

        e.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void afterTextChanged(Editable editable) {
                p2 = e.getText()+"";
                update();
            }
        });

        r1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                p3 = r1.getText()+"";
                update();
            }
        });
        r2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                p3 = r2.getText()+"";
                update();
            }
        });

        c1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                update();
            }
        });
        c2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                update();
            }
        });
        b.setOnClickListener((v) -> { Toast.makeText(MainActivity.this, "<제출>\n"+t.getText(), Toast.LENGTH_LONG).show(); });
    }
    private void update() {
        StringBuilder p = new StringBuilder("");

        p.append("목적지: "+p1);
        p.append("\n승차인원: "+p2+"(명)");
        p3 = r1.isChecked() ? r1.getText()+"": r2.isChecked() ? r2.getText()+"" : "선택 없음";
        p.append("\n좌석: "+p3);
        if(c1.isChecked()) p.append("\n"+c1.getText());
        if(c2.isChecked()) p.append("\n"+c2.getText());

        t.setText(p);
    }
}

실행 시 첫 화면
spinner, edittext, radiobutton 값 입력 및 선택 화면
checkbox 선택 화면
제출 버튼 클릭 시 화면

 

공지사항
링크