android使用不同的方式弹出窗口

来源:互联网 发布:女孩子做淘宝客服好吗 编辑:程序博客网 时间:2024/05/15 17:42

一.使用PopupWindow弹出窗口

使用PopupWindow创建对话框风格的窗口需要进行如下两步

(一)调用PopupWindwos的构造器创建一个PopupWindow对象

Public ConstructorsPopupWindow(Context context)

Create a new empty, non focusable popup window of dimension (0,0).

PopupWindow(Context context, AttributeSet attrs)

Create a new empty, non focusable popup window of dimension (0,0).

PopupWindow(Context context, AttributeSet attrs, int defStyle)

Create a new empty, non focusable popup window of dimension (0,0).

PopupWindow()

Create a new empty, non focusable popup window of dimension (0,0).

PopupWindow(View contentView)

Create a new non focusable popup window which can display thecontentView.

PopupWindow(int width, int height)

Create a new empty, non focusable popup window.

PopupWindow(View contentView, int width, int height)

Create a new non focusable popup window which can display thecontentView.

PopupWindow(View contentView, int width, int height, boolean focusable)

Create a new popup window which can display the contentView.

(二)调用PopupWindow的showAsDropDown(View v)将PopupWindow作为v组件的下拉组件显示出来,或者,调用PopupWindow的showAtLocation方法将PopupWindow在指定位置显示出来.如果想让窗口消失,直接调用dismiss()方法即可.

voidshowAsDropDown(View anchor, int xoff, int yoff)

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.

voidshowAsDropDown(View anchor)

Display the content view in a popup window anchored to the bottom-left corner of the anchor view.

voiddismiss()

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的创建器

Public ConstructorsAlertDialog.Builder(Context context)

Constructor using a context for this builder and the AlertDialog it creates.
AlertDialog.Builder(Context context, int theme)
Constructor using a context and theme for this builder and the AlertDialog it creates.

(二)调用 AltrtDialog.Builder的方法为对话框设定图标,标题,内容等.

AlertDialog.BuildersetIcon(Drawable icon)

Set the Drawable to be used in the title.
AlertDialog.BuildersetIcon(int iconId)
Set the resource id of the Drawable to be used in the title.
AlertDialog.BuildersetMessage(CharSequence message)
Set the message to display.
AlertDialog.BuildersetMessage(int messageId)
Set the message to display using the given resource id.
AlertDialog.BuildersetOnKeyListener(DialogInterface.OnKeyListener onKeyListener)
Sets the callback that will be called if a key is dispatched to the dialog.
AlertDialog.BuildersetPositiveButton(int textId, DialogInterface.OnClickListener listener)
Set a listener to be invoked when the positive button of the dialog is pressed.
AlertDialog.BuildersetPositiveButton(CharSequence text, DialogInterface.OnClickListener listener)
Set a listener to be invoked when the positive button of the dialog is pressed.
AlertDialog.BuildersetTitle(CharSequence title)
Set the title displayed in the Dialog.
AlertDialog.BuildersetTitle(int titleId)
Set the title using the given resource id.
AlertDialog.BuildersetView(View view)
Set a custom view to be the contents of the Dialog.

(三)调用 AltrtDialog.Builder的create()方法创建对话框

AlertDialogcreate()

Creates a AlertDialog with the arguments supplied to this builder.

(四)调用 AltrtDialog.Builder的show()方法显示对话框

AlertDialogshow()

Creates a 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是为了防止按得太快,连续误按太多次了




        
   


原创粉丝点击