android使用不同的方式弹出窗口
来源:互联网 发布:女孩子做淘宝客服好吗 编辑:程序博客网 时间:2024/05/15 17:42
一.使用PopupWindow弹出窗口
使用PopupWindow创建对话框风格的窗口需要进行如下两步
(一)调用PopupWindwos的构造器创建一个PopupWindow对象
Create a new empty, non focusable popup window of dimension (0,0).
Create a new empty, non focusable popup window of dimension (0,0).
Create a new empty, non focusable popup window of dimension (0,0).
Create a new empty, non focusable popup window of dimension (0,0).
Create a new non focusable popup window which can display thecontentView.
Create a new empty, non focusable popup window.
Create a new non focusable popup window which can display thecontentView.
Create a new popup window which can display the contentView.
(二)调用PopupWindow的showAsDropDown(View v)将PopupWindow作为v组件的下拉组件显示出来,或者,调用PopupWindow的showAtLocation方法将PopupWindow在指定位置显示出来.如果想让窗口消失,直接调用dismiss()方法即可.
Display the content view in a popup window anchored to the bottom-left corner of the anchor view offset by the specified x and y coordinates.
Display the content view in a popup window anchored to the bottom-left corner of the anchor view.
Dispose of the popup window.
以下以一个简单的工程来显示效果.两个布局文件 main.xml和 widows.xml.其中main.xml只有一个按键btn,所以就不再给出了,no_device_controled_help_x,.xml文件则是你要弹出来的窗口的布局文件.
no_device_controled_help_x,.xml
<?xml version="1.0" encoding=utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"android:background="#ff333311"><Button android:id="@+id/connection_help_ok_button" android:background="@drawable/caption_bar_button_send_x" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/connection_help_ok" android:layout_alignParentBottom="true" /><ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@id/connection_help_ok_button"><TextView android:textSize="20.0sp" android:textColor="#ffffffff" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/connection_help_text"/></ScrollView></RelativeLayout>
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //加载R.layout.no_device_controled_help_x View help_window=this.getLayoutInflater().inflate(R.layout.no_device_controled_help_x, null); //创建PopupWindow对象 popup=new PopupWindow(help_window,480,320); //获取popup的关闭按钮 help_window.findViewById(R.id.connection_help_ok_button).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {popup.dismiss();} }); }
二.使用Activity的界面跳转,AndroidManifest.xml注册所要跳转到的界面的弹出风格.
如下所示,AddActivity是另一界面,使用 android:theme="@android:style/Theme.Dialog"使其以弹出对话框方式跳转.
<activity
android:name="mars.addressbook.AddActivity"
android:label="添加联系人"
android:theme="@android:style/Theme.Dialog" />
然后在工程里面用以下方式进行跳转
iintent=new Intent();intent.setClass(AddressBook.this, AddActivity.class),AddressBook.this.startActivity(intent);
注:•android:theme="@android:style/Theme.NoTitleBar" 不显示应用程序标题栏 •android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 不显示应用程序标题栏,并全屏 •android:theme="@android:style/Theme.Light" 背景为白色 •android:theme="@android:style/Theme.Light.NoTitleBar" 白色背景并无标题栏 •android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" 白色背景,无标题栏,全屏 •android:theme="@android:style/Theme.Black" 背景黑色 •android:theme="@android:style/Theme.Black.NoTitleBar" 黑色背景并无标题栏 •android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" 黑色背景,无标题栏,全屏 •android:theme="@android:style/Theme.Wallpaper" 用系统桌面为应用程序背景 •android:theme="@android:style/Theme.Wallpaper.NoTitleBar" 用系统桌面为应用程序背景,且无标题栏 •android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen" 用系统桌面为应用程序背景,无标题栏,全屏 •android:theme="@android:style/Translucent" 半透明效果 •android:theme="@android:style/Theme.Translucent.NoTitleBar" 半透明并无标题栏 •android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" 半透明效果,无标题栏,全屏 •android:theme="@android:style/Theme.Panel" •android:theme="@android:style/Theme.Light.Panel"
三.使用AlertDialog创建弹出窗口
使用AlterDialog创建对话框大致按如下步骤进行:
(一)创建AltrtDialog.Builder对象,该对象是AlterDialog的创建器
AlertDialog
it creates.AlertDialog
it creates.(二)调用 AltrtDialog.Builder的方法为对话框设定图标,标题,内容等.
Drawable
to be used in the title.Drawable
to be used in the title.Dialog
.(三)调用 AltrtDialog.Builder的create()方法创建对话框
AlertDialog
with the arguments supplied to this builder.(四)调用 AltrtDialog.Builder的show()方法显示对话框
AlertDialog
with the arguments supplied to this builder and show()
's the dialog.比如,在实现如下功能,在你按下返回键时,询问你是否真正要退出程序,则代码可以如下编写:
创建dialog对话框方法代码如下:protected void dialog() { AlertDialog.Builder builder = new Builder(Main.this); builder.setMessage("确认退出吗?"); builder.setTitle("提示"); builder.setPositiveButton("确认", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); Main.this.finish(); } }); builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); }在onKeyDown(int keyCode, KeyEvent event)方法中调用此方法 public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{ dialog(); } return false; }注:event.getRepeatCount() == 0是为了防止按得太快,连续误按太多次了
- android使用不同的方式弹出窗口
- android-弹出窗口的使用(1)
- android-弹出窗口的使用(1)
- 弹出窗口的使用
- 使用POST方式弹出窗口的两种方法
- Android PopupWindow的使用技巧 Android上自定义弹出窗口
- android弹出窗口的实现
- Android的弹出窗口设置
- 弹出窗口zDialog的使用
- 网页方式实现MSN的弹出窗口
- js弹出窗口的三种方式
- Flex弹出窗口的三种方式
- C#中三种弹出信息窗口的方式
- android 弹出窗口 - 正常的Dialog弹出窗口
- gridview中弹窗口(根据不同的条件,弹出不同的窗口)
- 根据弹出窗口的返回值执行不同的事件
- android应用的ACTIVITY当使用EditText控件时会默认弹出输入法窗口,默认不弹出窗口方法
- ActiveX调用组件时,使用静默执行的方式,不弹出控制台窗口
- 使用Spring3.x需要的jar包及作用总结
- Linux awk 命令 说明
- 两个字符串相乘
- php实现自动获取生成文章主题关键词功能-简易分词
- tizen开发(1)- app卸载,启动与源码
- android使用不同的方式弹出窗口
- jdk与tomcat环境变量配置
- 如何安装Windows版Memcache
- Let’s Play Games!
- 2012年10月9号阿里巴巴笔试(c++)(修改)
- ImageButton 屏蔽掉父类click消息
- 一些缓存及用法
- jQuery乱谈(三)
- Apple 企业开发者账号&邓白氏码申请记录