activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="org.androidtown.a2_2.MainActivity">

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/bytenumber"
android:layout_alignParentTop="true"
android:layout_margin="15dp"
android:background="#88000000"
android:gravity="left|top"
android:textSize="25dp" />

<TextView
android:id="@+id/bytenumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btgroup"
android:layout_gravity="right"
android:layout_marginTop="10dp"
android:gravity="right"
android:textSize="18dp"/>

<LinearLayout
android:id="@+id/btgroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >

<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="전송"/>

<Button
android:id="@+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="닫기" />

</LinearLayout>
</RelativeLayout>




MainActivity.java

package org.androidtown.a2_2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.UnsupportedEncodingException;

public class MainActivity extends AppCompatActivity {

private final int LIMIT = 80;
private EditText editText;
private TextView byteLabel;

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

editText = (EditText)findViewById(R.id.editText);
byteLabel = (TextView)findViewById(R.id.bytenumber);
Button send = (Button)findViewById(R.id.send);
Button exit = (Button)findViewById(R.id.exit);

editText.addTextChangedListener(new TextWatcher(){
String beforeText;

@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after){
beforeText = charSequence.toString();
}

@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count){

}

@Override

public void afterTextChanged(Editable editable){
int length = showBytes();
if(length > LIMIT)
editText.setText(beforeText);
}

});

send.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
Toast.makeText(getApplicationContext(), "전송되었습니다.", Toast.LENGTH_LONG).show();
}
});

exit.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
MainActivity.this.finish();
}
});
showBytes();
}
private int showBytes(){
try{
int length = editText.getText().toString().getBytes("EUC-KR").length;
byteLabel.setText(length + " / 80 바이트");
return length;
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}
return -1;
}
}


+ Recent posts