Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

재훈재훈

SMS 문자 전송 화면 만들기 예제 ver.1 - 글자 길이 제한 기능 본문

Computer Engineering/Android

SMS 문자 전송 화면 만들기 예제 ver.1 - 글자 길이 제한 기능

jaehoonx2 2018. 4. 7. 17:50

/*

SMS 문자 전송 화면 구성

- 화면 상단에 텍스트 입력 상자, 아래쪽에 전송, 취소 버튼 위치

텍스트 입력상자 밑에 '00/00 바이트표시하여  바이트수 제한(아직 미완성, 바이트 대신 글자 수 제한 - 표시는 아직 바이트로ㅠㅠ

- 전송 버튼 누를 시 토스트로 글자 내용 알림

- 취소 버튼 누를 시 앱 종료

*/

 

MainActivity.java

package com.tistory.uluroo.doitmission04;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
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 {

    EditText editText;      //
메세지 입력 뷰
   
TextView textView;      // 작성 글자 수 나타내는 뷰
   
Button button;          // 작성 버튼

   
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = (EditText) findViewById(R.id.editText);            //
뷰 지정
       
textView = (TextView) findViewById(R.id.textView);
        button = (Button) findViewById(R.id.button);

        editText.addTextChangedListener(new TextWatcher() {     // addTextChangedListener
텍스트가 입력에 따라 변경될 때마다 확인하는 기능
                                                                                   // TextWatcher
텍스트가 변경될 때마다 발생하는 이벤트 처리하는 인터페이스  
           
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                InputFilter[] filter = new InputFilter[1];
                filter[0] = new InputFilter.LengthFilter(40);       //
쓸 수 있는 글자 수 최대 40자로 제한
               
editText.setFilters(filter);

                int currentBytes = s.toString().getBytes().length;        //
텍스트 내용을 받아와서 바이트 수를 가져온다.
               
String txt = String.valueOf(currentBytes) + " / 80 바이트";
                textView.setText(txt);                                           //
텍스트뷰에 현재 바이트수 표시
           
}

            public void afterTextChanged(Editable s) {}
        });
    }

    public void onButton1Clicked(View v) {
        Toast.makeText(getApplicationContext(), (CharSequence) editText.getText(), Toast.LENGTH_LONG).show();
    }   //
전송 버튼을 클릭하면 작성된 메시지를 토스트로 띄어준다.
        // editText.getText()
의 반환형은 editable 이므로 CharSequence 타입으로 형변환 해준다.

   
public void onButton2Clicked(View v) {
        finish();
    }
}

 

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:orientation="vertical"
    >

    <EditText
        android:id="@+id/editText"
        android:layout_width="350dp"
        android:layout_height="350dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:background="@android:color/transparent"
        android:ems="10"
        android:hint="
메세지 입력"
        android:inputType="textMultiLine"
        android:lineSpacingExtra="18sp"
        android:textSize="47sp"
/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:text="0 / 80
글자"
        android:textAlignment="viewEnd"
        android:textColor="@android:color/black"
        android:textSize="20sp"
/>

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

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

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onButton2Clicked"
            android:text="
취소"
/>
    </LinearLayout>
</LinearLayout>

 

실행 화면

 

초기 화면


작성 시 화면, 왜 한글이 3바이트씩 잡히는지 모르겠다.


전송 버튼 누를 시 토스트로 작성 내용 보여주는 기능