Android 监听键盘的弹起与收缩
2015-12-18 10:18
507 查看
Android 监听键盘的弹起与收缩
由于android不存在该监听的API 所以需要自己去处理 先上代码
清单文件中需要改一下属性
由于android不存在该监听的API 所以需要自己去处理 先上代码
/* android:windowSoftInputMode="stateAlwaysHidden|adjustPan" 的值填写 【A】stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置 【B】stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activity里的状态,无论是隐藏还是显示 【C】stateHidden:用户选择activity时,软键盘总是被隐藏 【D】stateAlwaysHidden:当该Activity主窗口获取焦点时,软键盘也总是被隐藏的 【E】stateVisible:软键盘通常是可见的 【F】stateAlwaysVisible:用户选择activity时,软键盘总是显示的状态 【G】adjustUnspecified:默认设置,通常由系统自行决定是隐藏还是显示 【H】adjustResize:该Activity总是调整屏幕的大小以便留出软键盘的空间 【I】adjustPan:当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分 * */ public class MainActivity extends Activity implements View.OnLayoutChangeListener { // Activity最外层的Layout视图 private View activityRootView; // 屏幕高度 private int screenHeight = 0; // 软件盘弹起后所占高度阀值 private int keyHeight = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); activityRootView = findViewById(R.id.root_layout); // 获取屏幕高度 screenHeight = this.getWindowManager().getDefaultDisplay().getHeight(); // 阀值设置为屏幕高度的1/3 keyHeight = screenHeight / 3; activityRootView.addOnLayoutChangeListener(this); } @Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { // old是改变前的左上右下坐标点值,没有old的是改变后的左上右下坐标点值 // System.out.println(oldLeft + " " + oldTop +" " + oldRight + " " + // oldBottom); // System.out.println(left + " " + top +" " + right + " " + bottom); // 现在认为只要控件将Activity向上推的高度超过了1/3屏幕高,就认为软键盘弹起 if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > keyHeight)) { Toast.makeText(MainActivity.this, "监听到软键盘弹起...", Toast.LENGTH_SHORT).show(); } else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > keyHeight)) { Toast.makeText(MainActivity.this, "监听到软件盘关闭...", Toast.LENGTH_SHORT).show(); } } }
<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" android:id="@+id/root_layout" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.softkeyboard.MainActivity" > <EditText android:id="@+id/test" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="test" /> </RelativeLayout>
清单文件中需要改一下属性
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.softkeyboard" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:windowSoftInputMode="stateAlwaysHidden|adjustResize" android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
相关文章推荐
- Android ListView下拉/上拉刷新:设计原理与实现
- (亲测有效)Android SDK Manager国内无法更新的解决方案
- android---------Android中通过注解代替findViewById方法
- Android中如何实现textView图文混排
- android6.0功能添加,QC3.0
- Android编程实现手绘及保存为图片的方法(附demo源码下载)
- 安卓分享
- Android之ListView实现图文信息混排(客户端)(二)
- Android消息机制---handle
- android在SD卡保存文件和文件夹
- android studio Your app has more methods references than can fit in a single dex file解决思路与方法
- android开发游记:DrawerLayout 实现抽屉效果的导航菜单
- Android之Intent
- Android实用的任务管理器—tractor
- Android源码分析—带你认识不一样的AsyncTask(串并行)
- Android Support包里究竟有什么
- android 解决ScrollView嵌套ListView的问题,不能全屏,全屏不能显示下面控件
- Android Studio Monkey测试
- Android 中 LayoutInflater 的用法小结
- Android 小球碰撞检测技术