1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context="com.liuzheng.admin.myhidden.MainActivity"> 7 8 <LinearLayout 9 android:layout_width="match_parent" 10 android:layout_height="45dp" 11 android:layout_alignParentBottom="true" 12 android:orientation="horizontal"> 13 14 <Button 15 android:id="@+id/butt1" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:focusable="true" 19 android:focusableInTouchMode="true" 20 android:text="顯示" /> 21 22 <Button 23 android:id="@+id/butt2" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:text="隱藏" /> 27 28 <EditText 29 android:id="@+id/edit_text" 30 android:layout_width="200dp" 31 android:layout_height="wrap_content" /> 32 33 </LinearLayout> 34 35 </RelativeLayout>
1 public class MainActivity extends AppCompatActivity { 2 3 private Button butt1; 4 private Button butt2; 5 private EditText edit; 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_main); 11 edit = (EditText) findViewById(R.id.edit_text); 12 butt1 = (Button) findViewById(R.id.butt1); 13 butt1.setOnClickListener(new View.OnClickListener() { 14 @Override 15 public void onClick(View view) { 16 //綁定軟鍵盤到EditText 17 edit.setFocusable(true); 18 edit.setFocusableInTouchMode(true); 19 edit.requestFocus(); 20 InputMethodManager inputManager = (InputMethodManager) edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 21 inputManager.showSoftInput(edit, 0); 22 } 23 }); 24 butt2 = (Button) findViewById(R.id.butt2); 25 butt2.setOnClickListener(new View.OnClickListener() { 26 @Override 27 public void onClick(View view) { 28 // 去除軟鍵盤顯示 29 edit.clearFocus(); 30 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 31 imm.hideSoftInputFromWindow(edit.getWindowToken(), 0); 32 } 33 }); 34 } 35 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.liuzheng.admin.myhidden"> 4 5 <application 6 android:allowBackup="true" 7 android:icon="@mipmap/ic_launcher" 8 android:label="@string/app_name" 9 android:supportsRtl="true" 10 android:theme="@style/AppTheme"> 11 <activity android:name=".MainActivity" 12 android:windowSoftInputMode="adjustResize"> 13 <intent-filter> 14 <action android:name="android.intent.action.MAIN" /> 15 16 <category android:name="android.intent.category.LAUNCHER" /> 17 </intent-filter> 18 </activity> 19 </application> 20 21 </manifest>
在 項目的AndroidManifest.xml文件中界面對應的<activity>里加入
android:windowSoftInputMode="adjustResize"
各值的含義: 【A】stateUnspecified:軟鍵盤的狀態並沒有指定,系統將選擇一個合適的狀態或依賴於主題的設置 【B】stateUnchanged:當這個activity出現時,軟鍵盤將一直保持在上一個activity里的狀態,無論是隱藏還是顯示 【C】stateHidden:用戶選擇activity時,軟鍵盤總是被隱藏 【D】stateAlwaysHidden:當該Activity主窗口獲取焦點時,軟鍵盤也總是被隱藏的 【E】stateVisible:軟鍵盤通常是可見的 【F】stateAlwaysVisible:用戶選擇activity時,軟鍵盤總是顯示的狀態 【G】adjustUnspecified:默認設置,通常由系統自行決定是隱藏還是顯示 【H】adjustResize:該Activity總是調整屏幕的大小以便留出軟鍵盤的空間 【I】adjustPan:當前窗口的內容將自動移動以便當前焦點從不被鍵盤覆蓋和用戶能總是看到輸入內容的部分