SMS 입력 화면 만들고 글자의 수 표시하기

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.example.hello;
 
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.media.Image;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
 
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
 
 
public class MainActivity extends AppCompatActivity {
    @BindView(R.id.main_et)
    EditText et;
    @BindView(R.id.main_tv)
    TextView tv;
    @BindView(R.id.main_send)
    Button send;
    @BindView(R.id.main_close)
    Button close;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        et.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) {

InputFilter[] filter=new InputFilter[1];
filter[0]=new InputFilter.LengthFilter(80);
et.setFilters(filter); 입력될 문자열의 길이 값을 설정할 수 있음

                int currentBytes=s.toString().getBytes().length;
                String txt=String.valueOf(currentBytes)+" /80 바이트";
                tv.setText(txt);
            }
            @Override
            public void afterTextChanged(Editable s) {}
        });
    }
    @OnClick({R.id.main_send})
    public  void onClick(View v){
        if(v.equals(send)) {
            Toast.makeText(getApplicationContext(), et.getText(), Toast.LENGTH_LONG).show();
        }
    }
    @OnClick({R.id.main_close})
    public  void onClose(View v){
        if(v.equals(close)){
            finish();
        }
    }
}
 
cs

메인 엑티비티


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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">
 
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical"
       android:padding="15dp"
       >
       <EditText
           android:id="@+id/main_et"
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1"
           android:ems="10"
           android:inputType="textMultiLine"
           android:textSize="70dp"
           android:gravity="top"
           android:hint="입력 상자"
           android:maxLength="80"
           />
       <TextView
           android:id="@+id/main_tv"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_marginTop="10dp"
           android:layout_marginBottom="20dp"
           android:textSize="25dp"
           android:gravity="right"
           />
       <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       >
           <LinearLayout
               android:layout_width="0dp"
               android:layout_height="match_parent"
               android:layout_weight="1"
               ></LinearLayout>
           <Button
               android:id="@+id/main_send"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_weight="0.3"
               android:text="전송"
               android:textSize="30dp"
               android:layout_marginRight="25dp"
               />
           <Button
               android:id="@+id/main_close"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_weight="0.3"
               android:text="닫기"
               android:textSize="30dp"
               />
           <LinearLayout
               android:layout_width="0dp"
               android:layout_height="match_parent"
               android:layout_weight="1"
               ></LinearLayout>
 
       </LinearLayout>
 
 
   </LinearLayout>
 
</android.support.constraint.ConstraintLayout>
cs

xml


현재 글자 수를 어떻게 표시해야되나 고민하다가 구글 뒤적거리니까 액티비티에서 구현하는 방법이 있네요. 

역시 컴공은 구글의 학문입니다. 

+ Recent posts