android PopupWindow 下拉菜单

一、弹出PopupWindow 

 Button but=(Button)findViewById(R.id.but_driver);

     
but.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
initPopMenu();
mPop.showAsDropDown(v);
}
});


prvate  PopupWindow mPop;
private void initPopMenu() {
if (mPop == null) {
mPop = new PopupWindow(getLayoutInflater().inflate(R.layout.pop, null),
300,300);
//下面的四个是几点其他地方隐藏
mPop.setFocusable(true); 
mPop.setOutsideTouchable(true); 
mPop.update(); 
mPop.setBackgroundDrawable(new BitmapDrawable()); 
}
if (mPop.isShowing()) {
mPop.dismiss();
}

}


activity代码:plaincopy

  1. package cn.com.chenzheng_java;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.Color;  
  5. import android.os.Bundle;  
  6. import android.text.InputType;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.view.WindowManager.LayoutParams;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13. import android.widget.PopupWindow;  
  14. import android.widget.TextView;  
  15.   
  16. public class Test1 extends Activity {  
  17.     TextView textView;  
  18.     Button button;  
  19.   
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.         button = (Button) findViewById(R.id.button1);  
  25.         textView = (TextView) findViewById(R.id.textView1);  
  26.         button.setOnClickListener(new OnClickListener() {  
  27.   
  28.             @Override  
  29.             public void onClick(View v) {  
  30.                 initPopWindow();  
  31.   
  32.             }  
  33.         });  
  34.     }  
  35.   
  36.     /** 
  37.      * 新建一个popupWindow弹出框 popupWindow是一个阻塞式的弹出框,这就意味着在我们退出这个弹出框之前,程序会一直等待, 
  38.      * 这和AlertDialog不同哦,AlertDialog是非阻塞式弹出框,AlertDialog弹出的时候,后台可是还可以做其他事情的哦。 
  39.      */  
  40.     private void initPopWindow() {  
  41.         // 加载popupWindow的布局文件  
  42.         View contentView = LayoutInflater.from(getApplicationContext())  
  43.                 .inflate(R.layout.popup, null);  
  44.         // 设置popupWindow的背景颜色  
  45.         contentView.setBackgroundColor(Color.RED);  
  46.         // 声明一个弹出框  
  47.         final PopupWindow popupWindow = new PopupWindow(  
  48.                 findViewById(R.id.mainlayout), 200300);  
  49.         // 为弹出框设定自定义的布局  
  50.         popupWindow.setContentView(contentView);  
  51.   
  52.         final EditText editText = (EditText) contentView.findViewById(R.id.editText1);  
  53.         // 设定当你点击editText时,弹出的输入框是啥样子的。这里设置默认为数字输入哦,这时候你会发现你输入非数字的东西是不行的哦  
  54.         editText.setInputType(InputType.TYPE_CLASS_NUMBER);  
  55.         /* 
  56.          * 这个popupWindow.setFocusable(true);非常重要,如果不在弹出之前加上这条语句,你会很悲剧的发现,你是无法在 
  57.          * editText中输入任何东西的。该方法可以设定popupWindow获取焦点的能力。当设置为true时,系统会捕获到焦点给popupWindow 
  58.          * 上的组件。默认为false哦.该方法一定要在弹出对话框之前进行调用。 
  59.          */  
  60.         popupWindow.setFocusable(true);  
  61.         /* 
  62.          * popupWindow.showAsDropDown(View view)弹出对话框,位置在紧挨着view组件 
  63.          * showAsDropDown(View anchor, int xoff, int yoff)弹出对话框,位置在紧挨着view组件,x y 代表着偏移量 
  64.          * showAtLocation(View parent, int gravity, int x, int y)弹出对话框 
  65.          * parent 父布局 gravity 依靠父布局的位置如Gravity.CENTER  x y 坐标值 
  66.          */  
  67.         popupWindow.showAsDropDown(button);  
  68.   
  69.           
  70.         Button button_sure = (Button) contentView.findViewById(R.id.button1_sure);  
  71.         button_sure.setOnClickListener(new OnClickListener() {  
  72.             @Override  
  73.             public void onClick(View v) {  
  74.                 popupWindow.dismiss();  
  75.                 textView.setText("展示信息:"+editText.getText());  
  76.             }  
  77.         });  
  78.           
  79.         Button button_cancel = (Button) contentView.findViewById(R.id.button2_cancel);  
  80.         button_cancel.setOnClickListener(new OnClickListener() {  
  81.             @Override  
  82.             public void onClick(View v) {  
  83.                 popupWindow.dismiss();  
  84.             }  
  85.         });  
  86.           
  87.     }  
  88.   
  89. }  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值