Android 自定义RelativeLayout活动主窗口大小改变 得到软键盘的显示和隐藏

压缩模式
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);
            }
        });





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值