public class misson4 extends AppCompatActivity {
EditText edt_message;
TextView txt_size;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mission4);
edt_message = findViewById(R.id.edt_message);
txt_size = findViewById(R.id.txt_size);
Button sendButton = findViewById(R.id.btn_send);
sendButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String message = edt_message.getText().toString(); // editText메세지 받아오기
Toast.makeText(getApplicationContext(), "전송할 메시지\\n\\n" + message, Toast.LENGTH_LONG).show(); // 토스트 표시
}
});
Button closeButton = findViewById(R.id.btn_close);
closeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish(); // 액티비티 끝내기
}
});
TextWatcher watcher = new TextWatcher() { // 변화 감지
public void onTextChanged(CharSequence str, int start, int before, int count) { // 텍스타가 바뀌면
byte[] bytes = null;
try {
bytes = str.toString().getBytes("KSC5601");
int strCount = bytes.length; // 바이트 길이
txt_size.setText(strCount + " / 80바이트"); // Set textView
} catch(UnsupportedEncodingException ex) {
ex.printStackTrace();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void afterTextChanged(Editable strEditable) {
String str = strEditable.toString();
try {
byte[] strBytes = str.getBytes("KSC5601");
if(strBytes.length > 80) {
strEditable.delete(strEditable.length()-2, strEditable.length()-1);
}
} catch(Exception ex) {
ex.printStackTrace();
}
}
};
edt_message.addTextChangedListener(watcher); // editText에 watcher추가
}
}
TextWatcher쪽을 공부할 필요가 있을듯.
<?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:orientation="vertical">
<EditText
android:id="@+id/edt_message"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_margin="10dp"
android:singleLine="false"
android:maxLength="80"
android:gravity="top"
android:textSize="36sp"
android:hint="입력상자"
android:inputType="textPersonName" />
<TextView
android:id="@+id/txt_size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0/80 바이트"
android:gravity="right"
android:layout_marginRight="10dp"/>
<LinearLayout
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="전송"/>
<Button
android:id="@+id/btn_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="닫기"/>
</LinearLayout>
</LinearLayout>