<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="horizontal">
<Button
android:id="@+id/switch_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="切换"
android:onClick="switchInput"/>
<Button
android:id="@+id/open_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开"
android:onClick="openInput"/>
<Button
android:id="@+id/close_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭"
android:onClick="closeInput"/>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"/>
</LinearLayout>
public class MainActivity extends Activity{
private Button switchButton;
private Button openButton;
private Button closeButton;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switchButton = (Button) findViewById(R.id.switch_input);
openButton = (Button) findViewById(R.id.open_input);
closeButton = (Button) findViewById(R.id.close_input);
editText = (EditText) findViewById(R.id.editText1);
}
public void switchInput(View v){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
public void openInput(View v){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(findViewById(R.id.editText1),InputMethodManager.SHOW_IMPLICIT);
}
public void closeInput(View v){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(),0);
}
}