2장. 뷰와 레이아웃

2023. 11. 7. 20:51·안드로이드/연습 코드

1. TextView 전송

1-1. activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:id="@+id/inputlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/buttonLayout"
        android:layout_alignParentTop="true">

        <EditText
            android:id="@+id/inputMessage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/inputCount"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:cacheColorHint="#00000000"
            android:gravity="top"
            android:listSelector="#00000000"
            android:maxLength="80"
            android:textSize="48sp" />

        <!--  글자 수를 표시하는 TextView -->
        <TextView
            android:id="@+id/inputCount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:text="0 / 80 bytes" />
    </RelativeLayout>

    <!-- 여기에 전송, 닫기 버튼 포함하는 LinearLayout 작성 -->
    <LinearLayout
        android:id="@+id/buttonLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="21dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/sendButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="50dp"
            android:layout_weight="1"
            android:text="Send" />

        <Button
            android:id="@+id/closeButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="50dp"
            android:layout_weight="1"
            android:text="Close" />
    </LinearLayout>
</RelativeLayout>

 

1-2. MainActivity.java

public class MainActivity extends AppCompatActivity {
    EditText inputMessage;
    TextView inputCount;

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

        inputMessage = findViewById(R.id.inputMessage);
        inputCount = findViewById(R.id.inputCount);


        Button sendButton = findViewById(R.id.sendButton);
        sendButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String message = inputMessage.getText().toString();
                Toast.makeText(getApplicationContext(), "전송할 메시지\n\n" + message, Toast.LENGTH_LONG).show();
            }
        });

        Button closeButton = findViewById(R.id.closeButton);
        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;
                    inputCount.setText(strCount + " / 80바이트");
                } 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();
                }
            }
        };

        inputMessage.addTextChangedListener(watcher);
    }
}

 

'안드로이드 > 연습 코드' 카테고리의 다른 글

6장. 서비스와 브로드캐스트 리시버  (0) 2023.11.07
5장. Fragment  (0) 2023.11.07
4장. Activity와 Intent  (0) 2023.11.07
3장. 위젯  (0) 2023.11.07
1장. URL 버튼 만들기  (0) 2023.11.07
'안드로이드/연습 코드' 카테고리의 다른 글
  • 5장. Fragment
  • 4장. Activity와 Intent
  • 3장. 위젯
  • 1장. URL 버튼 만들기
달거달거
달거달거
개발자를 꿈꿉니다
  • 달거달거
    SWEE IT
    달거달거
  • 전체
    오늘
    어제
    • 분류 전체보기 (288)
      • 개발 환경 (5)
        • VSCode (1)
        • 파이썬 (Anaconda) (1)
        • Git (1)
        • Flutter (0)
        • Kotlin (1)
      • Spring (5)
        • 스프링 부트와 JPA 실무 완전 정복 로드맵 (2)
        • 스프링 부트와 AWS로 구현하는 웹 서비스 (1)
        • 채쌤의 스프링 부트 프로젝트 (1)
      • 알고리즘과 코딩 테스트 (16)
        • 파이썬 문법 (2)
        • 백준 25단계 (10)
        • 프로그래머스 코딩 테스트 고득점 Kit (1)
        • 코틀린 문법 (1)
      • 요리 (236)
      • 데이터베이스 (2)
        • MySQL (2)
      • 안드로이드 (11)
        • 연습 코드 (6)
        • 도서 내용 정리 (4)
      • Dart와 Flutter (5)
        • 도서 내용 정리 (4)
        • Flutter 위젯 정리 (1)
        • 15개 프로젝트 (2)
      • 피그마 (0)
        • 도서 내용 정리 (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    문법
    파이썬
    코틀린
    코딩 테스트
    데이터베이스
    알고리즘
    c++
    DART
    주석
    Flutter
    아나콘다
    spring
    node.js
    mysql
    오블완
    백준
    머신러닝
    피그마
    git
    AWS
    자취요리
    vscode
    docker
    안드로이드
    프로그래머스
    C
    JPA
    티스토리챌린지
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
달거달거
2장. 뷰와 레이아웃
상단으로

티스토리툴바