android Popupwindow被弹出软键盘挡住

之前做过一个页面包含有PopupWindow,PopupWindow中有EditText输入框,点击EditText输入内容PopupWindow从屏幕底部弹出,软键盘直接把PopupWindow给挡住了。

当输入内容的时候我们希望软键盘不会把PopupWindow给挡住,不影响用户的正常输入,该怎么办呢,现在说一下方法,在设置PopupWindow的时候加上如下两行代码

popupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

这两行代码的先后顺序不能变,否则不能解决问题,是不是很另类,修改后的结果如下图,是不是发现软键盘把PopupWindow整个往上推上去了,完全不影响用户的正常输入

以上示例图片为本人实测的截图,现贴出所有代码,想看具体效果的 同学可以自己测试一下

[java] view plain copy
import android.app.Activity;  
import android.graphics.Color;  
import android.graphics.drawable.ColorDrawable;  
import android.os.Bundle;  
import android.view.Gravity;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.ViewGroup.LayoutParams;  
import android.view.WindowManager;  
import android.widget.Button;  
import android.widget.PopupWindow;  

import com.example.mydemo.R;  

public class TestPopWindow extends Activity {  

    private PopupWindow popview;  
    private Button btn_open;  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);  
        this.setContentView(R.layout.test_popwindow);  

        btn_open = (Button) this.findViewById(R.id.btn_open);  
        btn_open.setOnClickListener(new OnClickListener() {  

            @Override  
            public void onClick(View arg0) {  
                showPopWindow();  
            }  
        });  

    }  

    private void showPopWindow() {  
        View view = LayoutInflater.from(this).inflate(R.layout.view_pop, null);  
        popview = new PopupWindow(view,  
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,  
                LayoutParams.WRAP_CONTENT, true);  
        popview.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));  
        popview.setFocusable(true);  
        // 设置点击其他地方就消失  
        popview.setOutsideTouchable(true);  
        popview.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);  
        popview.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);  
        popview.showAtLocation(findViewById(R.id.btn_open), Gravity.BOTTOM  
                | Gravity.CENTER_VERTICAL, 0, 0);  

    }  
}  


test_popwindow.xml布局文件
[java] view plain copy
<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:background="#ffffff"  
    android:orientation="vertical" >  

    <Button  
        android:id="@+id/btn_open"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentTop="true"  
        android:layout_centerHorizontal="true"  
        android:text="open" />  

</RelativeLayout>  


view_pop.xml布局文件
[java] view plain copy
<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="wrap_content"  
    android:orientation="vertical" >  

    <EditText  
        android:id="@+id/et_input"  
        android:layout_width="fill_parent"  
        android:layout_height="40dip"  
        android:singleLine="true"  
        android:textSize="16sp" >  
    </EditText>  

    <LinearLayout  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_below="@+id/et_input"  
        android:layout_marginTop="20dip"  
        android:gravity="center_horizontal"  
        android:orientation="horizontal" >  

        <Button  
            android:id="@+id/confirm"  
            android:layout_width="80dip"  
            android:layout_height="wrap_content"  
            android:layout_marginRight="30dip"  
            android:text="确定" />  

        <Button  
            android:id="@+id/cancel"  
            android:layout_width="80dip"  
            android:layout_height="wrap_content"  
            android:layout_marginLeft="30dip"  
            android:text="取消" />  
    </LinearLayout>  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值