Android设置输入框和软键盘动态悬浮

 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:当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分

  



 

posted @ 2016-09-29 17:11  IT人生(正)  阅读(6922)  评论(0编辑  收藏  举报