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

  

				
时间: 2024-08-27 00:44:32

Android设置输入框和软键盘动态悬浮的相关文章

android 弹出的软键盘遮挡住EditText文本框的解决方案

1.android 弹出的软键盘遮挡住EditText文本框的解决方案:把Activit对应的布局文件filename.xml文件里的控件用比重设置布局.(例如:android:layout_weight="31")并且尽可能把高度设置成自适应的:android:layout_height="wrap_content",也就是没有设置高度的控件可压缩度的总和,如果比软键盘的高度要大,在EditText文本输入的时候,弹出的软键盘就不会遮挡住文本输入框.2.设置默认软

Android使用xml自定义软键盘效果(附源码)

Android使用xml自定义软键盘效果原理: 1,软键盘其实是个控件,使用android.inputmethodserver.KeyboardView类定义. 2,主布局中使用帧布局,当我们需要显示软键盘时设置为可见,不需要时设置为不可见. 3,编写xml文件,定义键盘内容.使用xml文件填充KeyBoardView布局 4,设置EditText的监听事件. 完成键盘开发. 上效果图: 1,源码研究android.inputmethodserver.KeyboardView: /* * Cop

Android App监听软键盘按键的三种方式(转)

最近有类似需求,在csdn上刚好发现,粘贴过来,以防止忘记喽 前言: 我们在android手机上面有时候会遇到监听手机软键盘按键的时候,例如:我们在浏览器输入url完毕后可以点击软键盘右下角的“GO”按键加载url页面:在点击搜索框的时候,点击右下角的search符号键可以进行搜索:或者在全部数据输入完毕后,点击右下角的"done"就马上进行下一步操作. 效果图: function 1: 重写Activity的dispatchKeyEvent(KeyEvent event)方法,在其中

Android App监听软键盘按键的三种方式

前言: 我们在android手机上面有时候会遇到监听手机软键盘按键的时候,例如:我们在浏览器输入url完毕后可以点击软键盘右下角的"GO"按键加载url页面:在点击搜索框的时候,点击右下角的search符号键可以进行搜索:或者在全部数据输入完毕后,点击右下角的"done"就马上进行下一步操作. 效果图: function 1: 重写Activity的dispatchKeyEvent(KeyEvent event)方法,在其中监听KeyEventKey.KEYCODE

Android App监听软键盘按键的三种方式与改变软键盘右下角确定键样式

actionNone : 回车键,按下后光标到下一行actionGo : Go,actionSearch : 放大镜actionSend : SendactionNext : NextactionDone : Done,确定/完成,隐藏软键盘,即使不是最后一个文本输入框 android:singleline="true" android:imeoptions="actionSearch" EditText.setOnEditorActionListener设置监听

android中界面与软键盘的交互

一.在清单中配置 <activity android:windowSoftInputMode="stateVisible|adjustResize"/ > 各值的含义: [1]stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置 [2]stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activity里的状态,无论是隐藏还是显示 [3]stateHidden:用户选择activity时,软键盘

android:windowSoftInputMode属性使用 软键盘

android:windowSoftInputMode="adjustResize|stateHidden" windowSoftInputMode属性设置值说明. <activity android:windowSoftInputMode=["stateUnspecified", "stateUnchanged", "stateHidden", "stateAlwaysHidden", "

android隐藏输入法隐藏软键盘全解

一些项目中需要隐藏输入法,比如登录页面,登录成功后需要隐藏输入法,还有比如发布某个帖子,或者消息是 发布成功后需要隐藏输入法,再比如一个页面中既有EditText 又有Spinner等控件,当你输入完就去点击 Spinner控件是软键盘是没有被隐藏的,这是是影响用户体验的. 这里我写了一个静态的方法,有需要的可以放在Util包中,需要用的时候直接调用就ok /** * 隐藏输入法面板 * * @param activity */ public static void hideInputMetho

android显示和隐藏软键盘

显示键盘: EditText editText.setFocusable(true); editText.setFocusableInTouchMode(true); editText.requestFocus(); InputMethodManager inputManager = (InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.show