본문 바로가기
JAVA

[Android/Java] Do it 도전 04 문제

by nyang2 2023. 12. 10.

도전 04. 문제. SMS 입력 화면 만들고 글자의 수 표시하기

 

1. SMS로 문자를 전송하는 화면은 위쪽에 텍스트 입력상자, 아래쪽에 [전송] 과 [닫기] 버튼을 수평으로 배치하도록 구성합니다.

2. 텍스트 입력상자 바로 아래에 입력되는 글자의 바이트 수를 '10/80 바이트'와 같은 포맷으로 표시하되 우측 정렬로 하도록 하고 색상을 눈에 잘 띄는 다른 색으로 설정합니다.

3. 텍스트 입력상자에 입력되는 글자의 크기와 줄 간격을 조정하여 한 줄에 한글 8글자가 들어가도록 만들어 봅니다.

4. [전송] 버튼을 누르면 입력된 글자를 화면에 토스트로 표시하여 내용을 확인할 수 있도록 합니다.

 


04. 프로젝트

xml file >
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edit_sms"
        android:layout_width="match_parent"
        android:layout_height="491dp"
        android:layout_weight="1"
        android:ems="10"
        android:gravity="center"
        android:hint="입력상자"
        android:inputType="textMultiLine"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/textByte"
        android:layout_width="match_parent"
        android:layout_height="58dp"
        android:gravity="right"
        android:text="0 / 80 바이트"
        android:textSize="20dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="전송"
            android:textSize="20dp" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="닫기"
            android:textSize="20dp" />
    </LinearLayout>

</LinearLayout>​
java file >
package org.techtown.doit_04;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button sendButton;
    EditText smsInput;
    TextView byteView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sendButton = (Button)findViewById(R.id.button);     // 전송 버튼 불러오기
        smsInput = (EditText)findViewById(R.id.edit_sms);   // 텍스트 입력상자 불러오기
        byteView = (TextView)findViewById(R.id.textByte);   // 글자 바이트 수 텍스트 불러오기

        smsInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                changeText(s);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    public void onSendButtonClicked(View v){
        String message = smsInput.getText().toString();
        showToast(message);
    }

    public void showToast(String str){
        Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show(); // 토스트 메세지 띄우기
    }

    public void changeText(CharSequence s){
        byteView.setText(""+ s.toString().getBytes().length + " / 80 바이트"); // textView 변경
    }
}​

'JAVA' 카테고리의 다른 글

[Android/Java] Do it 도전 03 문제  (0) 2023.12.08
[Android/Java] Do it 도전 01, 02 문제  (0) 2023.12.07
[백준/Java] 2083번 - 럭비 클럽  (0) 2023.09.14
Java 기초 용어  (1) 2023.09.14