화면의 위쪽에 텍스트 입력 상자, 아래쪽에 [전송]과 [닫기] 버튼을 수평으로 배치하세요.
// activity_main.xml <?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" android:gravity="center" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal" android:layout_weight="0.3" android:layout_margin="20dp"> <EditText android:id="@+id/edtMessage" android:layout_width="300dp" android:layout_height="300dp" android:padding="10dp" android:gravity="start" android:textSize="38sp" android:lineSpacingExtra="1dp" android:inputType="textMultiLine" android:background="@drawable/message_border" android:hint="메세지를 입력하세요."/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="right" android:paddingRight="50dp" android:layout_weight="0.5" android:orientation="horizontal"> <TextView android:id="@+id/txtCount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="0" android:textSize="23sp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right" android:text="/ 80 바이트" android:textSize="23sp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="100" android:gravity="center_horizontal"> <Button android:id="@+id/btnSend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="전송"/> <Button android:id="@+id/btnCancle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:text="취소"/> </LinearLayout> </LinearLayout>
// MainActivity.java package com.study.doit; 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.ImageView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { EditText edtMessage; Button btnSend, btnCancle; TextView txtCount; String content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edtMessage = findViewById(R.id.edtMessage); txtCount = findViewById(R.id.txtCount); btnSend = findViewById(R.id.btnSend); btnCancle = findViewById(R.id.btnCancle); edtMessage.addTextChangedListener(new TextWatcher() { String temp = ""; @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { //입력란에 변화가 생길 있을 시 조치 } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { //입력이 끝났을 때 조치 } @Override public void afterTextChanged(Editable editable) { //입력하기 전 조치 txtCount.setText(editable.length()+""); if(editable.length() > 80) { Toast.makeText(getApplicationContext(), "글은 80자 까지 입력 가능합니다.", Toast.LENGTH_SHORT).show(); edtMessage.setText(temp); } else if(editable.length() == 80) { txtCount.setTextColor(Color.RED); } else { temp = editable.toString(); txtCount.setTextColor(Color.BLUE); } content = editable.toString(); } }); btnSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(getApplicationContext(), content+" 메시지가 전송되었습니다.", Toast.LENGTH_SHORT).show(); edtMessage.setText(""); } }); btnCancle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } }); } }
80개가 되면 시각적으로 오류를 더 잘 나타내기 위해 byte를 나타내는 TextView
가 빨간색으로 바뀌며, 80개 아래로 내려가면 다시 파랑색으로 바뀌게 기능을 추가했습니다.