반응형
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();
}
}
반응형
'안드로이드' 카테고리의 다른 글
제스처 이벤트 (0) | 2021.10.11 |
---|---|
터치 이벤트 (0) | 2021.10.11 |
두 개의 이미지뷰에 이미지 번갈아 보여주기 (0) | 2021.10.11 |
셰이프 드로어블 - 투명 배경 버튼 (0) | 2021.10.08 |
셰이프 드로어블 - 그라디언트 (0) | 2021.10.08 |