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 |