• Android PopupWindow弹出对话框的实现


    Java代码  收藏代码
    1. package com.easyway.ui.popupWindow;  
    2.   
    3. import android.app.Activity;  
    4. import android.content.Context;  
    5. import android.os.Bundle;  
    6. import android.view.Gravity;  
    7. import android.view.LayoutInflater;  
    8. import android.view.View;  
    9. import android.view.View.OnClickListener;  
    10. import android.widget.Button;  
    11. import android.widget.EditText;  
    12. import android.widget.PopupWindow;  
    13.   
    14. /** 
    15.  * PopupWindow弹出对话框的实现 
    16.  *  
    17.  *              //获取布局管理对象 
    18.  *              LayoutInflater inflater = (LayoutInflater)    
    19.  *                 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
    20.  *              //获取相关的自定义布局视图 
    21.  *              final View vPopupWindow=inflater.inflate(R.layout.popupwindow, null, false); 
    22.  *              //PopupWindow的对话框的弹出 
    23.  *              final PopupWindow pw= new PopupWindow(vPopupWindow,300,300,true); 
    24.  *         
    25.  *         
    26.  *         
    27.  *                //显示popupWindow对话框 
    28.                 pw.showAtLocation(parent, Gravity.CENTER, 0, 0); 
    29.  * @author longgangbai 
    30.  * 
    31.  */  
    32. public class AndroidPopupWindowActivity extends Activity {  
    33.       
    34.             Button btnPopupWindow;  
    35.             /** Called when the activity is first created. */  
    36.             @Override  
    37.             public void onCreate(Bundle savedInstanceState) {  
    38.                 super.onCreate(savedInstanceState);  
    39.                 setContentView(R.layout.main);  
    40.                 //定义按钮  
    41.                 btnPopupWindow=(Button)this.findViewById(R.id.Button01);  
    42.                 btnPopupWindow.setOnClickListener(new ClickEvent());  
    43.             }  
    44.               
    45.               
    46.             //统一处理按键事件  
    47.             class ClickEvent implements OnClickListener{  
    48.   
    49.                 @Override  
    50.                 public void onClick(View v) {  
    51.                     // TODO Auto-generated method stub  
    52.                     if(v==btnPopupWindow)  
    53.                     {  
    54.                         showPopupWindow(AndroidPopupWindowActivity.this,  
    55.                                 AndroidPopupWindowActivity.this.findViewById(R.id.Button01));  
    56.                     }  
    57.                 }  
    58.             }  
    59.   
    60.             /** 
    61.              * 显示相关的PopupWindow对话框 
    62.              * @param context 
    63.              * @param parent 
    64.              */  
    65.             public void showPopupWindow(Context context,View parent){  
    66.                 //获取布局管理对象  
    67.                 LayoutInflater inflater = (LayoutInflater)     
    68.                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);     
    69.                 //获取相关的自定义布局视图  
    70.                 final View vPopupWindow=inflater.inflate(R.layout.popupwindow, null, false);  
    71.                 //PopupWindow的对话框的弹出  
    72.                 final PopupWindow pw= new PopupWindow(vPopupWindow,300,300,true);  
    73.   
    74.                 //OK按钮及其处理事件  
    75.                 Button btnOK=(Button)vPopupWindow.findViewById(R.id.BtnOK);  
    76.                 btnOK.setOnClickListener(new OnClickListener(){  
    77.                     @Override  
    78.                     public void onClick(View v) {  
    79.                         //设置文本框内容  
    80.                         EditText edtUsername=(EditText)vPopupWindow.findViewById(R.id.username_edit);  
    81.                         edtUsername.setText("username");  
    82.                         EditText edtPassword=(EditText)vPopupWindow.findViewById(R.id.password_edit);  
    83.                         edtPassword.setText("password");  
    84.                     }  
    85.                 });  
    86.                   
    87.               //Cancel按钮及其处理事件  
    88.                 Button btnCancel=(Button)vPopupWindow.findViewById(R.id.BtnCancel);  
    89.                 btnCancel.setOnClickListener(new OnClickListener(){  
    90.                     @Override  
    91.                     public void onClick(View v) {  
    92.                         pw.dismiss();//关闭  
    93.                     }  
    94.                 });  
    95.                 //显示popupWindow对话框  
    96.                 pw.showAtLocation(parent, Gravity.CENTER, 0, 0);  
    97.             }  
    98.               
    99.         } 
  • 相关阅读:
    贝叶斯公式推导
    三种常量池
    SpringCloud使用Feign实现服务间通信
    springCloud配置本地配中心SpringCloudConfig
    SpringApplication执行流程
    调用shutdown.sh后出现could not contact localhost8005 tomcat may not be running报错问题
    TCP协议详解
    web.xml配置说明
    第一份offer
    博客CSS
  • 原文地址:https://www.cnblogs.com/shihao/p/2323701.html
Copyright © 2020-2023  润新知