액티비티의 기본 창이 터치형 소프트 키보드를 포함하는 창과 상호작용하는 방법을 나타냅니다. 이 속성 설정은 다음 두 가지에 영향을 미칩니다.
- 액티비티가 사용자의 관심을 받는 포커스가 될 때 소프트 키보드의 상태(숨김 또는 표시)
- 소프트 키보드를 위한 공간을 확보하도록 창의 크기를 더 작게 조절할지, 소프트 키보드가 창의 일부를 가릴 때 현재 포커스가 표시되도록 콘텐츠를 이동할지 등의 활동의 기본 창 조정
설정은 다음 표에 나열된 값 중 하나이거나 '
state...
' 값 하나와 'adjust...
' 값 하나를 조합한 것이어야 합니다. 한 그룹에 여러 값(예: 여러 'state...
' 값)을 설정하면 정의되지 않은 결과가 발생합니다. 개별 값은 세로 막대(|
)로 구분됩니다.
<activity
android:name=".keyboard.KeyboardMainActivity"
android:exported="true"
android:windowSoftInputMode="stateVisible"> 👈
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
class KeyboardMainActivity : AppCompatActivity() {
private lateinit var binding: ActivityKeyboardBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityKeyboardBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.editText.requestFocus() 👈
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="<http://schemas.android.com/apk/res/android>"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="10dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:background="@drawable/ic_launcher_background"
android:src="@drawable/ic_launcher_foreground" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_hide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hide" />
</androidx.appcompat.widget.LinearLayoutCompat>
editText에 requestFoucs를 준다.
manifest에서는 android:windowSoftInputMode="stateVisible"로 설정한다.
binding.buttonShow.setOnClickListener {
showSoftKeyboard(binding.editText)
}
fun showSoftKeyboard(view: View) {
if (view.requestFocus()) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
}
//mainfest는 따로 지정 x
binding.buttonHide.setOnClickListener {
hideSoftKeyboard(this,binding.editText.windowToken)
binding.editText.clearFocus()
}
fun hideSoftKeyboard(context: Context, windowToken: IBinder) {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(windowToken, 0)
}
//mainfest는 따로 지정 x
<?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>"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<View
android:id="@+id/space"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@color/teal_200"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_hide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hide"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/space" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/button_hide" />
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/purple_200"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<activity
android:name=".keyboard.KeyboardMainActivity"
android:exported="true"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
editText를 가리지 않게 화면이 전체적으로 키보드 위로 올라감.
<activity
android:name=".keyboard.KeyboardMainActivity"
android:exported="true"
android:windowSoftInputMode="stateUnspecified|adjustPan"> 👈
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>