Android软键盘监听KeyboardWatcher
Posted 「已注销」
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android软键盘监听KeyboardWatcher相关的知识,希望对你有一定的参考价值。
转载请标明出处:http://blog.csdn.net/wu_wxc/article/details/53705322
本文出自【吴孝城的CSDN博客】
在如登录界面上当输入框获得焦点时,为了将输入框显示出来,不被软键盘遮住,我们可以监听软键盘的显示与关闭来实现
首先在build.gradle中配置依赖
compile 'com.azimolabs.keyboardwatcher:keyboardwatcher:0.1.3'
布局是一个图片和一个输入框
MainActivity.java
package cn.wuxiaocheng.keyboardwatcher;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import com.azimolabs.keyboardwatcher.KeyboardWatcher;
public class MainActivity extends AppCompatActivity implements KeyboardWatcher.OnKeyboardToggleListener
private ImageView img;
private KeyboardWatcher mKeyboardWatcher;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.img);
initKeyWatch();
// 实现未实现的方法
@Override
public void onKeyboardShown(int keyboardSize)
img.setVisibility(View.GONE);
// 实现未实现的方法
@Override
public void onKeyboardClosed()
img.setVisibility(View.VISIBLE);
// 初始化软键盘监听
private void initKeyWatch()
mKeyboardWatcher = new KeyboardWatcher(this);
mKeyboardWatcher.setListener(this);
然后在AndroidManifest.xml中添加android:windowSoftInputMode=”“配置
<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>
如果没有在AndroidManifest.xml里做相应配置,会报如下错误
Caused by: java.lang.IllegalArgumentException: Activity MainActivity should have windowSoftInputMode=”adjustResize”to make KeyboardWatcher working. You can set it in AndroidManifest.xml
以上是关于Android软键盘监听KeyboardWatcher的主要内容,如果未能解决你的问题,请参考以下文章