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

时间:2023-03-09 05:39:45
Android设置输入框和软键盘动态悬浮
 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.liuzheng.admin.myhidden.MainActivity"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"> <Button
android:id="@+id/butt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="显示" /> <Button
android:id="@+id/butt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隐藏" /> <EditText
android:id="@+id/edit_text"
android:layout_width="200dp"
android:layout_height="wrap_content" /> </LinearLayout> </RelativeLayout>
 public class MainActivity extends AppCompatActivity {

     private Button butt1;
private Button butt2;
private EditText edit; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.edit_text);
butt1 = (Button) findViewById(R.id.butt1);
butt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//绑定软键盘到EditText
edit.setFocusable(true);
edit.setFocusableInTouchMode(true);
edit.requestFocus();
InputMethodManager inputManager = (InputMethodManager) edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(edit, 0);
}
});
butt2 = (Button) findViewById(R.id.butt2);
butt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 去除软键盘显示
edit.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit.getWindowToken(), 0);
}
});
}
}
 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.liuzheng.admin.myhidden"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </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:当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分