SoFunction
Updated on 2025-03-01

An example of Android soft keyboard status and hiding input method

An example of Android soft keyboard status and hiding input method

1 Switching of soft keyboard status

2 Force hide input method keyboard

MainActivity is as follows:

package ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
/**
  * Demo description:
  * 1 Switching of soft keyboard status
  * 2 Forced to hide input method keyboard
  */ 
public class MainActivity extends Activity { 
  private EditText mEditText; 
  private Button mButton; 
  private Context mContext; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    (savedInstanceState); 
    setContentView(); 
    init(); 
  } 
   
  private void init(){ 
    mContext=this; 
    mEditText=(EditText) findViewById(); 
    mButton=(Button) findViewById(); 
    (new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        //toggleInput(mContext); 
        hideInput(mContext,mEditText); 
      } 
    }); 
  } 
   
  /**
    * Switch the state of the soft keyboard
    * If the current is closed and turns into pop-up, if the current is pop-up, turns into pop-up
    */ 
  private void toggleInput(Context context){ 
    InputMethodManager inputMethodManager = 
    (InputMethodManager)(Context.INPUT_METHOD_SERVICE); 
    (0, InputMethodManager.HIDE_NOT_ALWAYS); 
  } 
   
  /**
    * Force hide input method keyboard
    */ 
  private void hideInput(Context context,View view){ 
    InputMethodManager inputMethodManager = 
    (InputMethodManager)(Context.INPUT_METHOD_SERVICE); 
    ((), 0); 
  } 
 
 
} 

as follows:

<RelativeLayout xmlns:andro 
  xmlns:tools="/tools" 
  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=".MainActivity" > 
 
  <EditText 
    android: 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:hint="Input here" /> 
 
  <Button 
    android: 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/editText" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="15dp" 
    android:hint="Button" /> 
 
</RelativeLayout> 

If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!