Package com. example. dd; Import android. app. Activity; Import android. content. Context; Import android. OS. Bundle; Import android. view. Menu; Import android. view. View; Import android. view. View. OnClickListener; Import android. view. inputmethod. InputMethodManager; Import android. widget. Button; Import android. widget. EditText; Public class MainActivity extends Activity { @ Override Protected void onCreate (Bundle savedInstanceState ){ Super. onCreate (savedInstanceState ); SetContentView (R. layout. activity_main ); Final EditText ed2 = (EditText) findViewById (R. id. editText2 ); Button b1 = (Button) findViewById (R. id. button1 ); B1.setOnClickListener (new OnClickListener (){ @ Override Public void onClick (View v ){ Ed2.requestFocus (); Show (ed2 ); } }); Button b2 = (Button) findViewById (R. id. button2 ); B2.setOnClickListener (new OnClickListener (){ @ Override Public void onClick (View v ){ Toggle (); } }); } Private void show (EditText ed2 ){ InputMethodManager imm = (InputMethodManager) getSystemService (Context. INPUT_METHOD_SERVICE ); Imm. showSoftInput (ed2, InputMethodManager. SHOW_IMPLICIT ); } Private void toggle (){ InputMethodManager imm = (InputMethodManager) getSystemService (Context. INPUT_METHOD_SERVICE ); Imm. toggleSoftInput (0, 0 ); } @ Override Public boolean onCreateOptionsMenu (Menu menu ){ // Inflate the menu; this adds items to the action bar if it is present. GetMenuInflater (). inflate (R. menu. main, menu ); Return true; } } |