您的位置:首页 > 移动开发 > Android开发

Android监听软键盘的弹起和隐藏

2015-12-02 19:00 549 查看
做安卓键盘功能的时候,要是需求不单单是输入文字,比如要输入表情,图片等的时候,就需要监听键盘的弹起和隐藏。在实际的项目中也遇到了这样的需求。接下来就写个博客,记录一些方法。

实现原理就是监听自定义控件的高度变换得键盘的弹起和隐藏状态。

都是程序员都心急。直接上代码:

MainActivity:

package com.example.testsoftinput;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import com.example.testsoftinput.ListenerInputView.OnKeyBoardStateChangeListener;

public class MainActivity extends Activity {

private ListenerInputView mListenerInputView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mListenerInputView=(ListenerInputView) findViewById(R.id.mListenerInputView);

mListenerInputView.setOnKeyBoardStateChangeListener(new OnKeyBoardStateChangeListener() {

@Override
public void OnKeyBoardState(int state) {
// TODO Auto-generated method stub
switch (state) {
//开启
case 1:
Toast.makeText(getApplicationContext(), "输入法显示了.", 1).show();
break;
//关闭
case 0:
Toast.makeText(getApplicationContext(), "变化为正常状态(输入法关闭).", 1).show();
break;

default:
break;
}

}
});
}
}


activity_main.xml:

<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="${packageName}.${activityClass}" >

<com.example.testsoftinput.ListenerInputView
android:id="@+id/mListenerInputView"
android:layout_width="2dip"
android:layout_height="match_parent" >
</com.example.testsoftinput.ListenerInputView>

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#22ff0000"
android:gravity="center"
android:text="Android技术交流群 :110520459" />

<EditText
android:layout_width="match_parent"
android:layout_height="50dip"
android:hint="监听软键盘的弹起和隐藏" />

</RelativeLayout>


ListenerInputView:

package com.example.testsoftinput;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class ListenerInputView extends View {
private static final boolean DEBUG = true;
private static final String TAG = "softinput";
private Context mContext;
private int softKeyboardHeight = 0;
public ListenerInputView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
init(context);
}

public ListenerInputView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}

public ListenerInputView(Context context) {

super(context);
init(context);

}

private void init(Context context) {
this.mContext = context;
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {

super.onSizeChanged(w, h, oldw, oldh);

if (DEBUG) {

Log.i(TAG, "screenHeight=" + 0 + ";w=" + w + ";h=" + h + ";oldw="
+ oldw + ";oldh=" + oldh);

}

if (Math.abs(h - oldh) > softKeyboardHeight) {

if (h >= oldh) {

if (DEBUG) {

if (listener != null) {
listener.OnKeyBoardState(0);
}
Log.i(TAG, "变化为正常状态(输入法关闭).");

}

} else {

if (DEBUG) {
if (listener != null) {
listener.OnKeyBoardState(1);
}
Log.i(TAG, "输入法显示了.");
}

}

} else {

if (oldh != 0) {

if (DEBUG) {
Log.i(TAG, "变化为正常状态.(全屏关闭)");
if (listener != null) {
listener.OnKeyBoardState(0);
}
}

} else {

if (DEBUG) {
Log.i(TAG, "初始化,当前为正常状态.");

}

}

}

}

/**
* 监听键盘变化 state 1 开启软键盘 0关闭软键盘
*
* @author haoweilai
*
*/
public interface OnKeyBoardStateChangeListener {

void OnKeyBoardState(int state);
}

private OnKeyBoardStateChangeListener listener;

public void setOnKeyBoardStateChangeListener(
OnKeyBoardStateChangeListener listener) {
this.listener = listener;
}
}


看到这里的朋友明白了,其实很简单。战友们我们一起加油!

原创链接:http://blog.csdn.net/u010052279/article/details/50151177

最后附上demo下载地址:http://download.csdn.net/detail/u010052279/9320537
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: