zoukankan      html  css  js  c++  java
  • Android 使用 popupWindow实现弹层并操作弹层元素

    需求:

    点页面,出现弹层,弹层包含EditText,Button等,点击Button实现提交操作;

    最终代码:

    private PopupWindow popupWindow ;
    private EditText gfName;
    private View popView;
    private Button addGf;
    
    protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
       // 
       popView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.popup_add_gf, null); 
           
           gfName = (EditText)popView.findViewById(R.id.gfname);
           addGf = (Button)popView.findViewById(R.id.addgf);
           
           // 创建PopupWindow对象
           popupWindow = new PopupWindow(popView, 600, 800); //指定高度宽度
     //        popupWindow = new PopupWindow(popView,LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT, true);//充满屏幕
           
           // 为弹出框设定自定义的布局
           popupWindow.setContentView(popView);
            // 使其聚焦
           popupWindow.setFocusable(true);  
           // 设置允许在外点击消失  
           popupWindow.setOutsideTouchable(true);  
           // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景  
           popupWindow.setBackgroundDrawable(new BitmapDrawable());
    addGf.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i(TAG,"Button onClick");
                }
    
    gfName.setText(addressName);        

    遇到的问题及解决:

    1、操作编辑框setText 及按钮setOnClickListener 时报错,空指针。

    解决:初始化元素时要指定view,如:

    gfName = (EditText)popView.findViewById(R.id.gfname);

    不要这样:

    mapView = (MapView) findViewById(R.id.map);

    接下来就是对弹层的美化了,参考:

    兑现半透明的popupwindow的源码

    http://www.myexception.cn/mobile/620613.html

    展示PopupWindow

    http://www.myexception.cn/mobile/430920.html

    Android popupwindow 实现登录账户选择

    http://hi.baidu.com/guoxiaoming/item/5b0b165d017efa3e33e0a9f2

    android(九种对话框)的实现方式

    http://www.2cto.com/kf/201204/128926.html

  • 相关阅读:
    轻量级的Web服务器Nginx0.9.0 开发版发布 狼人:
    微软发布Silverlight 5 Beta新特性 狼人:
    TechCrunch新闻评论:Flash耗电问题严重 狼人:
    IE9是最佳浏览器? 狼人:
    Silverlight面向客户端,HTML5面向Web 狼人:
    Silverlight 结构分析 狼人:
    HTML5是否已经准备好了?仍在W3C层层审核当中 狼人:
    Adobe驳斥Flash过度耗电论 称HTML5更耗电 狼人:
    Silverlight 5即将来临 狼人:
    运行控制[置顶] 有趣的编程控制自己电脑的CPU
  • 原文地址:https://www.cnblogs.com/sudawei/p/android.html
Copyright © 2011-2022 走看看