popupwindow

来源:互联网 发布:摩亨佐达罗 知乎 编辑:程序博客网 时间:2024/05/18 01:08

转载:http://houxiyang.com/archives/70/

Android中PopupWindow点击窗口之外和返回键消失,界面锁定的实现

发布时间:2012-12-20    分类:android

这几天做谷居网的android应用到混天暗地的程度了。。

做一个加入灵感集的操作,需要用户注册,弹出这么一个PopupWindow的提示框。

要实现的功能很简单,点击稍后提醒以及键盘的返回键要能退出PopupWindow,为了达到鼓励用户注册的目的,点击其他部分窗口不会消失,毕竟咱的目的是更多的用户注册不是,点击返回键还能使其消失,毕竟返回键离手指头更近。

当然,我会告诉你点击其他部分怎么让它消失,以及怎么锁定后面的界面。往下看吧。

device-2012-12-20-215234.png

 


PopupWindow出现之后,默认的是所有的操作都无效的,除了HOME键。而且是可以操作后面的界面的。

想要锁定后面的界面,很简单,只需要让PopupWindow是focusable的。


popupWindow.setFocusable(true);

这样,显示的时候,popupWindow获取啦焦点,后面的内容为非活动。

但是这样不能实现点击屏幕其他部分使其消失,返回键也不行。

这时候要给popupWindow设置一个一个BackgroundDrawable,如果你已经定义好布局,怕破坏掉样式,只需要设置一个空的Drawable即可。


popupWindow.setBackgroundDrawable(new PaintDrawable());

这样,点击屏幕其他部分和返回键都能实现使其消失的功能了。

 

我的情况和上面都不一样,不能让用户点一下屏幕其他地方就消失了,人家想注册呢,不小心碰到屏幕其他地方了,一下就没了,我可能就少了一个注册用户。

我要实现的仅仅是返回键使popupwindow消失。这里需要重写view的onKeyListener,这个view应该是popwindow的view,当然是最parent的那个view。上代码。


View layout = inflater.inflate(R.layout.account_dialog,(ViewGroup) activity.findViewById(R.id.account_dialog));layout.setFocusable(true); // 这个很重要layout.setFocusableInTouchMode(true);popupWindow = new PopupWindow(layout);popupWindow.setFocusable(true);// 重写onKeyListenerlayout.setOnKeyListener(new OnKeyListener() {@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK) {accountDialog.dismiss();accountDialog = null;return true;}return false;}});

 

看明白了吗?

要让生成popupwindow的那个view也是focusable的,保险起见,设置FocusableInTouchMode也为true。

这样再重写那个view的onKeyListener就行了。




popupwindow,android3.0之后退出的新的弹出是对话框,实际就是继承了alertdialog的对话框,具体用法,看下面代码,实际效果是点击按钮后,弹出我们popupwindow

//----------------------------------------------------------------------------------------------------------------------------

一、弹出式窗口:PopupWindow
1、主布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">


    <TextView
        android:id="@+id/tv_showText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="弹出式窗口"
        android:textSize="22px" />


    <Button
        android:id="@+id/bt_PopupWindow1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="以自己为Anchor,不偏移" />


    <Button
        android:id="@+id/bt_PopupWindow2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="以自己为Anchor,正下方" />
</LinearLayout>


2、窗口布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">


    <TextView
        android:id="@+id/tv_tip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="请输入内容:" />


    <EditText
        android:id="@+id/et_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </EditText>


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal" >


        <Button
            android:id="@+id/bt_ok"
            android:layout_width="100px"
            android:layout_height="50px"
            android:text="确定" />


        <Button
            android:id="@+id/bt_cancle"
            android:layout_width="100px"
            android:layout_height="50px"
            android:text="取消" />
    </LinearLayout>


</LinearLayout>


3、组件实例化
private Button bt_popupWindow1;   
private Button bt_popupWindow2;   


private TextView tv_showText;   


//弹出式窗口
private PopupWindow popupWindow; 


//屏幕宽度  
private int screenWidth;   
//屏幕高度
private int screenHeight;   


//弹出式窗口宽度
private int dialgoWidth;   
//弹出式窗口高度
private int dialgoheight; 


public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bt_popupWindow1 = (Button)findViewById(R.id.bt_PopupWindow1);   
       bt_popupWindow2 = (Button)findViewById(R.id.bt_PopupWindow2);   
       
       tv_showText = (TextView)findViewById(R.id.tv_showText);   
              
        bt_popupWindow1.setOnClickListener(new ClickEvent());   
        bt_popupWindow2.setOnClickListener(new ClickEvent());   
        bt_popupWindow3.setOnClickListener(new ClickEvent());   
        bt_popupWindow4.setOnClickListener(new ClickEvent());  
    }


private class ClickEvent implements OnClickListener { 
    @Override 
    public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch(v.getId()) { 
    //以自己为Anchor,不偏移 
    case R.id.bt_PopupWindow1: 
   
    break; 
    //以自己为Anchor,
//偏移(screenWidth-dialgoWidth)/2, 0)--按钮正下方 
   
    break; 
   
    default: 
    break; 
   
   


    }






4、绘制弹出式窗口PopupWindow
/** 
* 创建PopupWindow 
*/ 
protected void initPopuptWindow() { 
// TODO Auto-generated method stub 
View popupWindow_view = 
getLayoutInflater().inflate( //获取自定义布局文件dialog.xml的视图 
R.layout.dialog, null,false); 
popupWindow = 
new PopupWindow(popupWindow_view, 
200, 150, true);
 
//创建PopupWindow实例 
Button bt_ok = (Button)popupWindow_view.findViewById(R.id.bt_ok); 
//dialog.xml视图里面的控件 
Button bt_cancle = (Button)popupWindow_view.findViewById(R.id.bt_cancle); 
final EditText et_text = (EditText)popupWindow_view.findViewById(R.id.et_text); 
bt_ok.setOnClickListener(new OnClickListener() { 


@Override 
public void onClick(View v) { 
// TODO Auto-generated method stub 
tv_showText.setText(et_text.getText()); //在标签里显示内容 
popupWindow.dismiss(); //对话框消失 

}); 


bt_cancle.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
// TODO Auto-generated method stub 
popupWindow.dismiss(); 

}); 
//获取屏幕和对话框各自高宽 
screenWidth = 
PopupWindowTest.this
.getWindowManager().getDefaultDisplay().getWidth(); 
screenHeight = PopupWindowTest.this
.getWindowManager().getDefaultDisplay().getHeight(); 
dialgoWidth = popupWindow.getWidth(); 
dialgoheight = popupWindow.getHeight(); 





 /* 
    * 获取PopupWindow实例 
    */ 
    private void getPopupWindow() { 
    if(null != popupWindow) { 
    popupWindow.dismiss(); 
    return; 
    }else { 
    initPopuptWindow(); 
    } 
    } 


5、完成窗口的定位


switch(v.getId()) { 
    //以自己为Anchor,不偏移 
    case R.id.bt_PopupWindow1: 
    getPopupWindow(); 
    popupWindow.showAsDropDown(v); 
    break; 
    //以自己为Anchor,偏移(screenWidth-dialgoWidth)/2, 0)--按钮正下方 
    case R.id.bt_PopupWindow2: 
    getPopupWindow(); 
    popupWindow.showAsDropDown
    (v, (screenWidth-dialgoWidth)/2, 0); 
    break; 
    case R.id.bt_PopupWindow3:
    //以屏幕中心为参照,不偏移
    getPopupWindow(); 
    popupWindow.showAtLocation
    (findViewById(R.id.layout), Gravity.CENTER, 0, 0); 
    break; 




响应返回按钮:
popupWindow
//.setBackgroundDrawable(new BitmapDrawable());


0 0
原创粉丝点击