Android的"最佳实践"从对话框返回值 [英] Android "Best Practice" returning values from a dialog

查看:366
本文介绍了Android的"最佳实践"从对话框返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是正确的方式,将值从一个复杂的自定义对话框返回到调用活动 - 说,文本字段,日期或时间选择器,一堆单选按钮等,再加上一个保存和取消按钮?

有些我已经看到了网站上的技术包括:

  • 在对话框派生类的公共数据成员可以通过该活动来读

  • 公开得到访问。 。 。 。。 。

  • 有意向启动的对话框(相对于的显示()的),加上内搭投入的各种控制和捆绑起来,在对话框类处理程序传递回活动所以当听者点击保存包被传递回使用的 ReturnIntent()

  • 听众在活动过程中其投入是在对话框如控制,所以TimePicker或的DatePicker的听众是真心的活动。在这个方案中几乎所有的工作都是在活动完成

  • 一位听众在活动的保存按钮,然后在活动直接询问对话框中的控制;活动关闭对话框。

...加上更多的,我已经忘了。

有一个被认为该标准地正确或最佳实践方法的特定技术?

解决方案

我用下面的方法:

  1. 在我所有的活动都有一个相同的父活动(比方说ControlActivity)。 ControlActivity有私人挥发性捆绑controlBundle; 有适当的getter / setter
  2. 当我开始对话,我习惯叫对话框通过我自己的方法:

     公共无效showMyDialog(INT ID,束束)
    {
        this.controlBundle =束;
        this.showDialog(ID,束);
    }
     

所以每次我知道发送到对话框参数时

<醇开始=3>
  • 在当前对话框即将完成,我在对话框正在形成另一个捆绑必要的值,然后把它们经过我的活动捆绑二传手:
  •  
    ((ControlActivity)this.getOwnerActivity).setControlBundle(包);
     

    所以,最后,当对话结束,我知道值从对话框中的返回。我知道,它不喜欢 INT RET code = this.showMyDialog(); 这是一个有点复杂,但它是可行的。

    What is the "correct" way to return the values to the calling activity from a complex custom dialog - say, text fields, date or time picker, a bunch of radio buttons, etc, plus a "Save" and "Cancel" button?

    Some of the techniques I've seen on the web include:

    • public data members in the Dialog-derived class which can be read by the Activity

    • public "get" accessors . . . " . . " . . "

    • Launching the dialog with an Intent (as opposed to show() ) plus handlers in the Dialog class which take input from the various controls and bundle them up to be passed back to the Activity so when listener hits "Save" the bundle is passed back using ReturnIntent()

    • Listeners in the Activity which process input from the controls that are in the dialog e.g., so the TimePicker or DatePicker's listeners are really in the Activity. In this scheme practically all the work is done in the Activity

    • One Listener in the Activity for the "Save" button and then the Activity directly interrogates the controls in the dialog; the Activity dismisses the dialog.

    ...plus more that I've already forgotten.

    Is there a particular technique that's considered the canonically correct or "best practice" method?

    解决方案

    I'm using following way:

    1. All my activities has one and the same parent Activity (let's say ControlActivity). ControlActivity has private volatile Bundle controlBundle; with appropriate getter/setter
    2. When I start dialog, I used to call dialog thru my own method:

      public void showMyDialog(int id, Bundle bundle)
      {
          this.controlBundle=bundle;
          this.showDialog(id, bundle);
      }
      

    So each time I know parameters sent to dialog

    1. When dialog is about to complete, I'm forming in dialog another Bundle with necessary values and then put them thru my Activity bundle setter:

    
    ((ControlActivity )this.getOwnerActivity).setControlBundle(bundle);
    

    So in the end when dialog finishes I know value "returned" from dialog. I know that it's not like int retCode=this.showMyDialog(); it's a bit more complex, but it's workable.

    这篇关于Android的&QUOT;最佳实践&QUOT;从对话框返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆