iphone/android比较学习之──提示框的使用

来源:互联网 发布:java awt源码 编辑:程序博客网 时间:2024/04/29 08:37


一、iPhone中的UIActionSheet与UIAlterView

首先,在.h文件中添加Protocol,(Protocol相当于Java中的interface)

@interfaceActionSheetViewController : UIViewController

<UIActionSheetDelegate>

{

...

...

}

-(IBAction)showActionSheetButtonPressed:(id) sender;

-(IBAction)showAlterViewButtonPressed:(id) sender;

 

在.m文件中实现showActionSheetButtonPressed 方法。

-(IBAction)showActionSheetButtonPressed:(id) sender

{  

UIActionSheet*actionSheet = [[UIActionSheet alloc]

  initWithTitle:@"Title"

  delegate:self

  cancelButtonTitle:@"Cancel!"

destructiveButtonTitle:@"OK!"

  otherButtonTitles:nil];

[actionSheetshowInView:self.view];//参数指显示UIActionSheet的parent。

[actionSheetrelease];

}

一个IUActionSheet在用户点击Button的时调用,但是当用户选择了destructiveButton或者cancelButton后,如何处理想对应的事件呢?

在UIActionSheetDelegate中,有一个方法,

-(void)actionSheet :(UIActionSheet *) actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;

我们需要实现这个方法就可以了。

 

-(void)actionSheet :(UIActionSheet *) actionSheetdidDismissWithButtonIndex:(NSInteger) buttonIndex

{

//当用户按下cancel按钮

if(buttonIndex == [actionSheet cancelButtonIndex])

{

//DoSomething here.

}

//当用户按下OK按钮

if(buttonIndex == [actionSheet destructiveButtonIndex])

{

//DoSomething here.

}

}

 

UIAlertView:

-(IBAction)showAlterViewButtonPressed:(id) sender

{

UIAlertView*alertView = [[UIAlertView alloc]

  initWithTitle:@"AlertTitle"

  message:@"AlterMessage Content"

  delegate:self

  cancelButtonTitle:@"OK"

  otherButtonTitles:nil];

[alertViewshow];

[alertViewrelease];

}

 

UIAlterView相对于UIActionSheet简单,以为它只是一个提示用户的View而已。可以在 UIActionSheet的的处理事件 -(void) actionSheet ...中来调用UIAlterView.

 

二、Android Dialog用法总结

Android,Dialog, 用法

1、AlertDialog.Builder

Android中的alertDialog的创建一般是通过其内嵌类AlertDialog.Builder来实现的。所以首先浏览一下这个builder所提供的方法:

 

setTitle():给对话框设置title.

 

setIcon():给对话框设置图标。

 

setMessage():设置对话框的提示信息

 

setItems():设置对话框要显示的一个list,一般用于要显示几个命令时

 

setSingleChoiceItems():设置对话框显示一个单选的List

 

setMultiChoiceItems():用来设置对话框显示一系列的复选框。

 

setPositiveButton():给对话框添加”Yes”按钮。

 

setNegativeButton():给对话框添加”No”按钮。

 

2、常见对话框:

在了解完这几个常用的方法之后,看一个小例子,创建一个用来提示的对话框:

Dialogdialog = new AlertDialog.Builder(AlertDialogSamples.this)

              .setIcon(R.drawable.alert_dialog_icon)

               .setTitle(“title”)

               .setMessage(“这里是提示信息语句”)

              .setPositiveButton(“Ok”, new DialogInterface.OnClickListener() {

                  public void onClick(DialogInterface dialog, int whichButton){

  

                      /* User clicked OK so do some stuff */

                  }

               })

              .setNeutralButton(“Cancel”, new DialogInterface.OnClickListener() {

                  public void onClick(DialogInterface dialog, int whichButton){

 

                      /* User clicked Something so do some stuff */

                  }

               })

              .setNegativeButton(R.string.alert_dialog_cancel,newDialogInterface.OnClickListener() {

                  public void onClick(DialogInterface dialog, int whichButton){

 

                      /* User clicked Cancel so do some stuff */

                  }

               })

               .create();

dialog.show();//如果要显示对话框,一定要加上这句