본문 바로가기

안드로이드

SMS 입력 화면 만들고 글자 수 표현

반응형

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"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="432dp"
        android:ems="10"
        android:gravity="top"
        android:hint="입력"
        android:inputType="textMultiLine" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp"
        android:gravity="center|top"
        android:orientation="horizontal">

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

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="닫기" />
    </LinearLayout>

</LinearLayout>

 

 

MainActivity

package com.example.a09;

import androidx.appcompat.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.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

//    https://www.tabnine.com/code/java/methods/android.widget.TextView/setFilters
//    https://gon109.tistory.com/121

    EditText editText1;
    TextView tv1;
    int maxLength = 80;

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

        editText1 = findViewById(R.id.editText1);
        tv1 = findViewById(R.id.textView1);

        InputFilter[] inputFilters = new InputFilter[1];
        inputFilters[0] = new InputFilter.LengthFilter(maxLength);
        editText1.setFilters(inputFilters);

        tv1.setText("0 / " + maxLength + " 바이트");

        editText1.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) {
                int currentBytes = s.toString().getBytes().length;
                String txt = String.valueOf(currentBytes) + " / " + maxLength + " 바이트";
                tv1.setText(txt);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public void onButton1Clicked(View v){
        Toast.makeText(this, editText1.getText(), Toast.LENGTH_LONG).show();
    }


}
반응형