1.android中windowSoftInputMode属性:
android:windowSoftInputMode="stateVisible|adjustResize". . . >
在这设置的值(除"stateUnspecified"和"adjustUnspecified"以外)将覆盖在主题中设置的值各值的含义:
【A】stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置
【B】stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activity里的状态,无论是隐藏还是显示
【C】stateHidden:用户选择activity时,软键盘总是被隐藏
【D】stateAlwaysHidden:当该Activity主窗口获取焦点时,软键盘也总是被隐藏的
【E】stateVisible:软键盘通常是可见的
【F】stateAlwaysVisible:用户选择activity时,软键盘总是显示的状态
【G】adjustUnspecified:默认设置,通常由系统自行决定是隐藏还是显示
【H】adjustResize:该Activity总是调整屏幕的大小以便留出软键盘的空间
【I】adjustPan:当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分
2.android监听键盘View改变大小:
(1)Activity设置WindowSoftInputMode中含有adjustResize属性(2)自动一个View(SoftKeyBoardSatusView),重写onSizeChanged(),计算更改大小,在向外提供一个接口(SoftkeyBoardListener)。
package com.example.androidforge;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.LinearLayout;
/**
* 该监听在类容不能滚动的情况下无效
*/
public class SoftKeyBoardSatusView extends LinearLayout {
private final int CHANGE_SIZE = 100;
public SoftKeyBoardSatusView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public SoftKeyBoardSatusView(Context context) {
super(context);
init();
}
private void init() {
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// TODO Auto-generated method stub
super.onSizeChanged(w, h, oldw, oldh);
Log.i("demo", "w :" + w);
Log.i("demo", "h :" + h);
Log.i("demo", "oldw :" + oldw);
Log.i("demo", "oldh :" + oldh);
if (oldw == 0 || oldh == 0)
return;
if (boardListener != null) {
boardListener.keyBoardStatus(w, h, oldw, oldh);
if (oldw != 0 && h - oldh < -CHANGE_SIZE) {
boardListener.keyBoardVisable(oldh - h);
}
if (oldw != 0 && h - oldh > CHANGE_SIZE) {
boardListener.keyBoardInvisable(oldh - h);
}
}
}
public interface SoftkeyBoardListener {
public void keyBoardStatus(int w, int h, int oldw, int oldh);
public void keyBoardVisable(int move);
public void keyBoardInvisable(int move);
}
SoftkeyBoardListener boardListener;
public void setSoftKeyBoardListener(SoftkeyBoardListener boardListener) {
this.boardListener = boardListener;
}
}
(3)在Activity的布局中加入SoftKeyBoardSatusView,设置宽高属性为match_parent
<com.example.androidforge.SoftKeyBoardSatusView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/status"
android:layout_width="match_parent"
android:layout_height="match_parent"
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.androidforge.MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/test"
android:padding="20dp"
android:text="@string/hello_world" />
</com.example.androidforge.SoftKeyBoardSatusView>
(4)在Activity中注册SoftkeyBoardListener时间
public class MainActivity extends ActionBarActivity implements
SoftkeyBoardListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("Tag", "onCreate " + getClass().getSimpleName());
SoftKeyBoardSatusView statusView = (SoftKeyBoardSatusView) findViewById(R.id.status);
statusView.setSoftKeyBoardListener(this);
}
需要实现的接口:
@Override
public void keyBoardStatus(int w, int h, int oldw, int oldh) {
// TODO Auto-generated method stub
}
@Override
public void keyBoardVisable(int move) {
// TODO Auto-generated method stub
}
@Override
public void keyBoardInvisable(int move) {
// TODO Auto-generated method stub
}
}
其他策略:
其他方案
android点击列表后弹出输入框,所点击项目自动滚动到输入框上方