博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 中PopupWindow使用
阅读量:7014 次
发布时间:2019-06-28

本文共 1961 字,大约阅读时间需要 6 分钟。

Android 中PopupWindow使用。

PopupWindow会阻塞对话框,要在外部线程 或者 PopupWindow本身做退出才行。

mypopWindow.xml的Layout设计如下。

<?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:background=
"#259"
    
android:orientation=
"vertical" 
>
 
    
<TextView
        
android:id=
"@+id/textView1"
        
android:layout_width=
"wrap_content"
        
android:layout_height=
"wrap_content"
        
android:text=
"Here is Pop Window" 
/>
 
    
<Button
        
android:id=
"@+id/button1"
        
android:layout_width=
"wrap_content"
        
android:layout_height=
"wrap_content"
        
android:text=
"OK" 
/>
 
    
<Button
        
android:id=
"@+id/button2"
        
android:layout_width=
"wrap_content"
        
android:layout_height=
"wrap_content"
        
android:text=
"Cancle" 
/>
 
</LinearLayout>

 MainActivity.java文件。

 在MainActivity的Button按钮单击,然后显示PopupWindow。

private 
void 
showPopWindow(Context context, View parent)
    
{      
        
LayoutInflater inflater = (LayoutInflater)             
                
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
        
final 
View vPopWindow=inflater.inflate(R.layout.mypopwindow,
null
,
false
); 
        
//宽300 高300           
        
final 
PopupWindow popWindow =
new 
PopupWindow(vPopWindow,
300
,
300
,
true
);
        
Button okButton = (Button)vPopWindow.findViewById(R.id.button1);
        
okButton.setOnClickListener(
new 
View.OnClickListener() {
             
            
@Override
            
public 
void 
onClick(View v) {
                
// TODO Auto-generated method stub
                
Toast.makeText(MainActivity.
this
,
"You click OK"
, Toast.LENGTH_SHORT).show();
            
}
        
});
         
        
Button cancleButton = (Button)vPopWindow.findViewById(R.id.button2);
        
cancleButton.setOnClickListener(
new 
View.OnClickListener() {
             
            
@Override
            
public 
void 
onClick(View v) {
                
// TODO Auto-generated method stub
                
popWindow.dismiss();
//Close the Pop Window
            
}
        
});
         
        
popWindow.showAtLocation(parent, Gravity.CENTER,
0
,
0
);
         
         
    
}

 效果图如下:最后显示是剧中的,这里我只是截取了部分图片。

单击Ok按钮显示You click OK信息。单击取消,则关闭PopupWindow.

本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2013/03/15/2961430.html,如需转载请自行联系原作者

你可能感兴趣的文章
CloudCC:CRM让你与客户的友谊小船永远不翻
查看>>
光伏扶贫财政补贴能持续几年?
查看>>
马云布局健康快乐两年后,阿里体育CEO谈创业:小步快走,不抢“疯 口”
查看>>
如何安全存储 云盘还是实体存储谁靠谱
查看>>
苹果修复了iPhone的自动拨号系统漏洞
查看>>
高热之下有冰点 大数据产业遭遇成长期烦恼
查看>>
大数据时代零售企业如何进行精确营销
查看>>
对冷却系统进行全面分析
查看>>
淘宝造物节,“奇市江湖”里那些脑洞大开的创意产品
查看>>
AMD宣布修复RX480供电Bug 性能还提速3%
查看>>
联想武汉工厂因暴雨断电 每日损失利润百万?
查看>>
云 测试压力
查看>>
报告软件缺陷的基本原则
查看>>
撬动智能家居市场 智慧家庭“最强大脑”被激活
查看>>
走向5G:爱立信携手新加坡电信为物联网部署4G LTE网络
查看>>
DBA必备技能:通过truss跟踪解决监听无法启动案例
查看>>
GNOME 基金会签署用户数据宣言 2.0
查看>>
《产品设计与开发(原书第5版)》——3.4 步骤1:确立章程
查看>>
《Adobe Illustrator CS6中文版经典教程(彩色版)》—第1课1.5节探索“控制面板”...
查看>>
MySQL 问题分析:ERROR 1071 : Specified key was too long;max
查看>>