popupwindow中EditText获取焦点后自动弹出软键盘

关于popupwindow中EditText获取焦点后自动弹出软键盘的问题,玩过手机qq或空间的童鞋应该知道,再点击评论时会弹出一个编辑框,并且伴随软键盘一起弹出是不是很方便啊,下面我们就来讲一下实现方法,先看效果: 


实现过程其实就是在listview的适配器Adapter中给"评论"这个所在的这个空间设置一个监听,当点击评论时,弹出popup,并异步弹出软键盘,看一下我的适配器中的代码片段:
//评论设置监听
		holder.pinglun.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				showPopup(holder.pinglun);
				popupInputMethodWindow();
				//Toast.makeText(activity, "评论", Toast.LENGTH_SHORT).show();
			}
		});

接下来看showPopup弹出popupwindow具体实现方法:
private void showPopup(View parent){
		if (popWindow == null) {		
			LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View view = layoutInflater.inflate(R.layout.popwindow_pinglun,null);
			// 创建一个PopuWidow对象
			popWindow = new PopupWindow(view,LinearLayout.LayoutParams.FILL_PARENT,100,true);
		}	
//popupwindow弹出时的动画		popWindow.setAnimationStyle(R.style.popupWindowAnimation);
		// 使其聚集 ,要想监听菜单里控件的事件就必须要调用此方法
		popWindow.setFocusable(true);
		// 设置允许在外点击消失
		popWindow.setOutsideTouchable(false);
		// 设置背景,这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景
		popWindow.setBackgroundDrawable(new BitmapDrawable());	
		//软键盘不会挡着popupwindow
		popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
		//设置菜单显示的位置
		popWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
		//监听菜单的关闭事件
		popWindow.setOnDismissListener(new OnDismissListener() {			
			@Override
			public void onDismiss() {
							}
		});
		//监听触屏事件
		popWindow.setTouchInterceptor(new OnTouchListener() {
			public boolean onTouch(View view, MotionEvent event) {	
				return false;								
			}
		});
	}

popupwindow布局如下: 
先看效果: 


再看代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/title_background" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:padding="5dp" >

        <TextView
            android:id="@+id/at"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:background="@drawable/qz_icon_at_normal" />

        <TextView
            android:id="@+id/biaoqing"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/at"
            android:background="@drawable/qz_icon_expression_normal" />

        <EditText
            android:id="@+id/pinglun"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp"
            android:layout_toLeftOf="@+id/fabiao"
            android:layout_toRightOf="@+id/biaoqing"
            android:background="@drawable/edit_bg_all"
            android:focusable="true"
            android:hint="来说一句吧..." />

        <Button
            android:id="@+id/fabiao"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:background="@drawable/title_write_btn_normal"
            android:padding="8dp"
            android:text="发表"
            android:textColor="#FFFFFF"
            android:textSize="14sp" />
    </RelativeLayout>

</RelativeLayout>

其中的图片什么的我就不贴了,大家可以自己找一下,然后看弹出软键盘的实现方法,注意这个需要异步操作:
private void popupInputMethodWindow() {  
	    handler.postDelayed(new Runnable() {  
	        @Override  
	        public void run() {  
	            imm = (InputMethodManager) pinglun.getContext().getSystemService(Service.INPUT_METHOD_SERVICE);  
	            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);  
	        }  
	    }, 0);  
	}  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值