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

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

  hot3.png

源码下载:

运行效果:

193247_L10o_1471708.png

193247_WCgW_1471708.png

按了返回后:

211955_7OXU_1471708.png

java代码:

package com.example.cest;import android.app.Activity;import android.graphics.drawable.PaintDrawable;import android.os.Bundle;import android.view.Gravity;import android.view.KeyEvent;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.PopupWindow;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {	PopupWindow popupWindow,popupWindoweExit;	Button bu_shi,bu_fou;	TextView view1;	TextView view2;	Button button;	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);		button=(Button) findViewById(R.id.bu);		button.setOnClickListener(new OnClickListener() {			@Override			public void onClick(View arg0) {				// TODO Auto-generated method stub				popinit();			}		});	}	protected void popinit() {		// TODO Auto-generated method stub		LayoutInflater inflater = LayoutInflater.from(this);		View pop_view = inflater.inflate(R.layout.bujv, null);		view1=(TextView) pop_view.findViewById(R.id.tv1);		view2=(TextView) pop_view.findViewById(R.id.tv2);		                                      //设置下拉按钮父布局的高和宽		popupWindow = new PopupWindow(pop_view,ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);		popupWindow.setFocusable(true);//没这行按钮下拉了,按手机返回会直接退出//		popupWindow.setOutsideTouchable(true);		popupWindow.setBackgroundDrawable(new PaintDrawable());//没这行按钮下拉了不会消失		popupWindow.showAtLocation(pop_view, Gravity.BOTTOM, 0, 0);//PopupWindow显示的位置		view1.setOnClickListener(new OnClickListener() {			@Override			public void onClick(View v) {				// TODO Auto-generated method stub				Toast.makeText(MainActivity.this, "这是注册",Toast.LENGTH_LONG).show();			}		});		view2.setOnClickListener(new OnClickListener() {			@Override			public void onClick(View v) {				// TODO Auto-generated method stub				Toast.makeText(MainActivity.this, "这是登录",Toast.LENGTH_LONG).show();			}		});	}	/*按返回弹出是否退出画面	 * 	 * */	@Override	public boolean onKeyDown(int keyCode, KeyEvent event) {		// TODO Auto-generated method stub		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {			popExit();		}		return false;	}	/*用PopupWindow做的退出	 * */	private void popExit() {		// TODO Auto-generated method stub		LayoutInflater inflater = LayoutInflater.from(this);		View pop_view = inflater.inflate(R.layout.exit, null);		bu_shi=(Button) pop_view.findViewById(R.id.bu_exit_shi);		bu_fou=(Button) pop_view.findViewById(R.id.bu_exit_fou);		popupWindoweExit = new PopupWindow(pop_view,200,ViewGroup.LayoutParams.WRAP_CONTENT);		popupWindoweExit.setFocusable(true);		popupWindoweExit.setOutsideTouchable(true);//		popupWindow.setBackgroundDrawable(new PaintDrawable());		popupWindoweExit.showAtLocation(pop_view, Gravity.BOTTOM|Gravity.CENTER_VERTICAL, 0, 150);		bu_shi.setOnClickListener(new OnClickListener() {						@Override			public void onClick(View v) {				// TODO Auto-generated method stub				MainActivity.this.finish();			}		});		bu_fou.setOnClickListener(new OnClickListener() {						@Override			public void onClick(View v) {				// TODO Auto-generated method stub				popupWindoweExit.dismiss();			}		});	}}

点击按钮显示的布局文件bujv.xml:

    
    

按返回按钮弹出退出的xml:

    
    
     
    
 
    

转载于:https://my.oschina.net/jjtm/blog/228651

你可能感兴趣的文章
.net 中主框架的搭建(2种方式)
查看>>
viewPager
查看>>
朴素贝叶斯python小样本实例
查看>>
[转]How to write dll
查看>>
C# For Bot Framework
查看>>
react native 增加react-native-camera
查看>>
陶哲轩实分析 习题 13.4.8
查看>>
线程和多线程(二)
查看>>
vmware共享文件夹
查看>>
关于wp-autopost不能连续采集的问题,这里有解决方案
查看>>
3.Appnium的安装
查看>>
python基础-函数02
查看>>
ListView图标大小的设置
查看>>
个人-GIT使用方法
查看>>
带缩略图焦点图插件
查看>>
T4模版引擎之生成数据库实体类
查看>>
java properties读取与设值
查看>>
IO流的登录与注册
查看>>
《JavaScript高级程序设计》笔记(1):<script>元素
查看>>
图标字体 VS 雪碧图——图标字体应用实践
查看>>