728x90
※ 해당 포스팅의 내용은 아래 Setting이 적용된 상태에서 진행되었습니다.
Setting#01. [Android] ViewBinding 사용하기
입력창을 키보드위에 위치시키기 샘플코드
# 소스코드
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent">
<EditText
android:id="@+id/edit_write"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:hint="메시지를 입력하세요"
android:textColor="#000000">
</EditText>
<Button
android:id="@+id/btn_submit"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="SUBMIT">
</Button>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package org.keyboard.input;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;
import org.keyboard.input.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), binding.editWrite.getText(), Toast.LENGTH_SHORT).show();
// 에디트 텍스트 초기화
binding.editWrite.setText("");
// 키보드 내리기
InputMethodManager manager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
if(MainActivity.this.getCurrentFocus() != null) {
manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
});
}
}
# 출력결과
728x90
'Android > Java Code' 카테고리의 다른 글
[Android] WebView를 이용한 Hybrid App 만들기 (0) | 2024.07.29 |
---|---|
[Android] Retrofit2를 사용한 API 통신 설정 및 Data 송수신 (0) | 2023.02.07 |
[Android] 출력 위치를 확인하는 Custom Log Message 제작 (0) | 2023.02.02 |
[Android] FCM 메시지 송수신 Application 제작 (0) | 2023.02.01 |
[Android] Activity에서 Dark Theme 비활성화기 (0) | 2023.01.04 |