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
"1.0" encoding="utf-8"?>  
"http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:background="#ffffff"  
    android:orientation="vertical" >  

    

你可能感兴趣的:(Android——积累小知识点)