版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010214991/article/details/48345437
转载请注明出处:http://blog.csdn.net/u010214991/article/details/48345437
最近在做类似于QQ空间和微信朋友圈的评论界面,顺便研究了下Android软键盘的一些使用方法,在网上收集了大量资料后自己进行了一些总结,并且通过亲测可用,希望能给广大读者带来用处。好了下面开始正题。
为了让大家更容易看懂,我贴出了一个小例子,在这个例子中有主要的活动界面MainActivity,有主要用于监听软键盘显示与隐藏的KeyboardListenRelativeLayout,在这里值得提出的是,由于软键盘并没有显示或隐藏的监听事件,因此我们只能通过判断当前布局的高度变化来进行软键盘的显示与隐藏(软键盘在弹出或收起时所在布局高度会改变)。而为了能够让整个界面能够自动调整高度,我们需要在配置的当前MainActivity中添加:android:windowSoftInputMode="adjustResize|stateHidden"。下面贴代码:
1.MainActivity.class
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnClickListener {
private Button button;
private LinearLayout ll;
InputMethodManager imm;
private KeyboardListenRelativeLayout relativeLayout;
private int oldHeight;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
ll = (LinearLayout) findViewById(R.id.line);
relativeLayout = (KeyboardListenRelativeLayout) findViewById(R.id.mainlayout);
view = findViewById(R.id.xian);
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
button.setOnClickListener(this);
relativeLayout
.setOnKeyboardStateChangedListener(new KeyboardListenRelativeLayout.IOnKeyboardStateChangedListener() {
@Override
public void onKeyboardStateChanged(int state) {
// TODO Auto-generated method stub
switch (state) {
case KeyboardListenRelativeLayout.KEYBOARD_STATE_HIDE:
ll.setVisibility(View.GONE);
view.setVisibility(View.GONE);
break;
default:
break;
}
}
});
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
ll.requestFocus();
ll.setVisibility(View.VISIBLE);
view.setVisibility(View.VISIBLE);
}
@Override
public boolean onTouchEvent(MotionEvent event) {//该方法用于点击编辑框以外画面时隐藏软键盘,适用于简单的布局,复杂的可能无效
// TODO Auto-generated method stub
if (getCurrentFocus() != null
&& getCurrentFocus().getWindowToken() != null) {
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);//隐藏软键盘
}
return super.onTouchEvent(event);
}
}
2.KeyboardListenRelativeLayout.class
import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
public class KeyboardListenRelativeLayout extends RelativeLayout {
private static final String TAG = KeyboardListenRelativeLayout.class.getSimpleName();
public static final byte KEYBOARD_STATE_SHOW = -3;
public static final byte KEYBOARD_STATE_HIDE = -2;
public static final byte KEYBOARD_STATE_INIT = -1;
private boolean mHasInit = false;
private boolean mHasKeyboard = false;
private int mHeight;
private IOnKeyboardStateChangedListener onKeyboardStateChangedListener;
public KeyboardListenRelativeLayout(Context context) {
super(context);
}
public KeyboardListenRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public KeyboardListenRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setOnKeyboardStateChangedListener(IOnKeyboardStateChangedListener onKeyboardStateChangedListener) {
this.onKeyboardStateChangedListener = onKeyboardStateChangedListener;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if(!mHasInit) {
mHasInit = true;
mHeight = b;
if(onKeyboardStateChangedListener != null) {
onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_INIT);
}
} else {
mHeight = mHeight < b ? b : mHeight;
}
if(mHasInit && mHeight > b) {
mHasKeyboard = true;
if(onKeyboardStateChangedListener != null) {
onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_SHOW);
}
}
if(mHasInit && mHasKeyboard && mHeight == b) {
mHasKeyboard = false;
if(onKeyboardStateChangedListener != null) {
onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_HIDE);
}
}
}
public interface IOnKeyboardStateChangedListener {
public void onKeyboardStateChanged(int state);
}
}
3.MainActivity的布局文件(activity_main.xml)
<com.example.test.KeyboardListenRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="101dp"
android:layout_marginTop="75dp"
android:text="发表" />
<LinearLayout
android:id="@+id/line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:gravity="center_vertical"
android:visibility="gone"
>
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:hint="说点什么吧..."
android:background="@null"
android:layout_weight="10"
/>
<Button
android:layout_width="70dp"
android:layout_height="42dp"
android:background="#cfcad2"
android:text="发送" />
</LinearLayout>
<View
android:id="@+id/xian"
android:layout_width="match_parent"
android:layout_height="0.8dp"
android:layout_alignTop="@id/line"
android:background="#cfcad2"
android:visibility="gone"
/>
</com.example.test.KeyboardListenRelativeLayout>
4.配置文件,其实就只是在当前活动中添加android:windowSoftInputMode="adjustResize|stateHidden"这句而已
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.test.MainActivity"
android:windowSoftInputMode="adjustResize|stateHidden"
android:screenOrientation="portrait"
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>
最后附上演示例子供下载: