压缩模式
android:windowSoftInputMode="adjustResize", 那么不管活动主窗口压缩后文本框EditText是否可见(这将于下面一种模式形成对比),
Activity
PS: 上述软键盘的弹出都是点击文本框, 主窗口的大小发生改变
也可以通过代码的方式手动控制软键盘的显示与隐藏(Android2.3.3SDK)
android:windowSoftInputMode="adjustResize", 那么不管活动主窗口压缩后文本框EditText是否可见(这将于下面一种模式形成对比),
当前Activity主窗口顶部保持不变,总是被从下向上压缩,压缩的距离等于软键盘的高度
/**
* 自定义布局计算高度 得到软键盘的隐藏和显示
*
* @author nanjinglong
*
*/
public class CustomRelativeLayout extends RelativeLayout {
private KeyboardChangeListener listener;
public CustomRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
}
/**
* 当前活动主窗口大小改变时调用
*/
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (null != listener) {
listener.onKeyboardChange(w, h, oldw, oldh);
}
}
public void setOnKeyboardChangeListener(KeyboardChangeListener listener) {
this.listener = listener;
}
/**
* Activity主窗口大小改变时的回调接口(本示例中,等价于软键盘显示隐藏时的回调接口)
*
*/
public interface KeyboardChangeListener {
public void onKeyboardChange(int w, int h, int oldw, int oldh);
}
}
布局文件xml
<com.lanyan.drawable.widget.customrelativelayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<textview android:layout_width="fill_parent"
android:layout_height="20dp" android:background="#9999CC"
android:layout_alignParentTop="true" android:text="============= 我在顶部 =============="
android:textColor="#FFFFFF"/>
<edittext android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="220dp" />
<textview android:layout_width="fill_parent"
android:layout_height="20dp" android:background="#9999CC"
android:layout_alignParentBottom="true" android:text="============= 我在底部 =============="
android:textColor="#FFFFFF"/>
Activity
public class EditActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit);
CustomRelativeLayout customRelativeLayout = (CustomRelativeLayout)findViewById(R.id.relativelayout1);
customRelativeLayout.setOnKeyboardChangeListener(new CustomRelativeLayout.KeyboardChangeListener() {
@Override
public void onKeyboardChange(int w, int h, int oldw, int oldh) {
//do your operation
}
});
}
}
PS: 上述软键盘的弹出都是点击文本框, 主窗口的大小发生改变
也可以通过代码的方式手动控制软键盘的显示与隐藏(Android2.3.3SDK)
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
//隐藏软键盘
// imm.hideSoftInputFromWindow(tv.getWindowToken(), 0);
//显示软键盘
// imm.showSoftInputFromInputMethod(tv.getWindowToken(), 0);
//切换软键盘的显示与隐藏
imm.toggleSoftInputFromWindow(tv.getWindowToken(), 0, InputMethodManager.HIDE_NOT_ALWAYS);
//或者
// imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
});