InputMethodManager দিয়ে কীবোর্ড হাইড করা
এডিটটেক্সট ফোকাস পেলে কীবোর্ড আসে। নির্দিষ্ট অ্যাকশনে (যেমন বাটন ক্লিক) কীবোর্ড লুকাতে InputMethodManager ব্যবহার করুন।
লেআউট (activity_main.xml)
<EditText
android:id="@+id/etInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type something..." />
<Button
android:id="@+id/btnHideKeyboard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hide Keyboard"
android:layout_gravity="center"
android:layout_marginTop="16dp" />
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText etInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etInput = findViewById(R.id.etInput);
Button btn = findViewById(R.id.btnHideKeyboard);
btn.setOnClickListener(this);
// অটো ফোকাস ও কীবোর্ড見せ (ঐচ্ছিক)
etInput.requestFocus();
showKeyboard(etInput);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnHideKeyboard) {
hideKeyboard(v);
}
}
private void hideKeyboard(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
private void showKeyboard(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
}
Manifest-এ অটো শো (ঐচ্ছিক)
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateAlwaysVisible">
<!-- অথবা stateVisible, adjustResize, adjustPan -->
</activity>
সাধারণ সমস্যা ও সমাধান
- getWindowToken() নাল: ভিউ অ্যাটাচড নেই।
view.getWindowToken()বাactivity.getCurrentFocus().getWindowToken()ব্যবহার করুন। - ফ্র্যাগমেন্টে:
requireActivity().getSystemService(...)ব্যবহার করুন। - Jetpack Compose:
LocalSoftwareKeyboardController.current?.hide()।