Android中的PopUpWindow

PopUpWindow在显示的时候有点像是个widget,是一个独立的程序块。或者说是像个AlertDialog

但是注意了:popupWindow是一个阻塞式的弹出框,这就意味着在我们退出这个弹出框之前,程序会一直等待。

                 AlertDialog是非阻塞式弹出框,AlertDialog弹出的时候,后台可是还可以做其他事情的哦。 

主要的工作步骤:

   1 加载popupWindow的布局文件,这和widget的方法差不多。

       1)先顶一个画布contentView

       2)popupWindow布局view加载到contentVew中

  2  .setFocusable(true)很重要,是popupwindow获得焦点的;.showAsDropDown(View view)是针对一些有事件的view

         //ContentView是一个当前上下文
View contentView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.popup, null); // 声明一个弹出框 final PopupWindow popupWindow = new PopupWindow(findViewById(R.id.mainlayout), 200, 300); // 为弹出框设定自定义的布局 popupWindow.setContentView(contentView); final EditText editText = (EditText) contentView.findViewById(R.id.editText1); // 设定当你点击editText时,弹出的输入框是啥样子的。这里设置默认为数字输入哦,这时候你会发现你输入非数字的东西是不行的哦 editText.setInputType(InputType.TYPE_CLASS_NUMBER); popupWindow.setFocusable(true); popupWindow.showAsDropDown(button); Button button_sure = (Button) contentView.findViewById(R.id.button1_sure); button_sure.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); //popupwindow的消失 textView.setText("展示信息:"+editText.getText()); } });

显示效果:

posted @ 2012-08-15 16:36  Tammie-锴  阅读(346)  评论(0编辑  收藏  举报