如何验证在Tab切换制表输入数据? [英] How to validate input data in a Tab on Tab Change?

查看:172
本文介绍了如何验证在Tab切换制表输入数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果我有一个应用程序的结构是这样的:结果
结果背景:的结果父活动#1只是抱着标签主机,部件标签,也设置了3个标签有自己的内容设置为3片活动(TAB1,TAB2,TAB3活动)。结果


  

更新:我试着打电话给我的验证方法里面
  onTabChangedListener在父活动#1,但我得到了一个空指针
  例外。不能真正究任何地方。我做了
  评论/删除冲突的code,但还是我没有得到的
  瓶颈。堆栈跟踪(引擎收录链接)。
  code为父活动#1 ,的标签#1
  活动


搜索结果
问题:的结果我想验证在各个选项卡活动的表单字段(s)的用户输入的数据onTabChanged事件的,但我​​无法设置比了单 setOnTabChangedListener 。结果我失去了一些东西在这里?结果监听器(S)在自己的标签#活动项下的OnCreate 的方法进行设置。搜索结果除了尝试上述技术,我曾试图在主父活动#1建立监听器 onResume()。但上的继续()方法决不会被调用。我得到一个空指针异常了。

验证暂时落后理念:我想,虽然用户更改标签,数据应验证之前,他可以跳过一个标签。所以,我ineffect需要标签#1类似于 onTabChanged 如果标签#2 /标签#3中选择一个事件来验证数据。结果此外,这将适用如果当前标签#2被选中,用户选择标签#1 /标签#3

任何意见,将AP preciated ..结果感谢您阅读。


解决方案

  

我要验证的用户在表单字段(S)输入的数据
  个人标签活动onTabChanged事件,但我无法设置
  多单setOnTabChangedListener。


有没有必要进行第二次 OnTabChangeListener 键,即使你可以设置它不会帮你。当你构建了code,你需要访问的子活动。您可以通过在<一个使用其中一个答案做到这一点href=\"http://stackoverflow.com/questions/5399324/how-to-reference-child-activity-from-tabhost-to-call-a-public-function\">this问题。的问题是,这些答案,除了接受之一,使用德precated方法

我的方法,我在评论中提出的是有一个静态布尔型字段中的每个用作选项卡中的子活动,让所有的活动更新那布尔标志每当有在这些意见状态的变化活动(如果你检查复选框,输入在的EditText 等东西)。然后,你可以简单地检查该标志在 OnTabChangeListener 所需的子活动。我的方法应该工作,但你的code是一个有点乱,所以你将不得不修改了不少。


  

我曾试图在主要建立在onResume()监听器
  父活动#1。但是,在resume()方法从未调用。一世
  得到了一个空指针异常了。


这是正常的,你收到了 NullPointerException异常与code,因为我还没有看到,你初始化为您在<使用子活动引用code> OnTabChangeListener 。

还有:

不要使用 TabActivity 。它一直赞成片段框架,更灵活的pcated德$ P $。这些片段可以帮助你,因为,我觉得你要停止改变标签如果当前页面的验证失败和 OnTabChangeListener 可能会来得晚了一点为(但我可能被误认为你想要什么)。

作为一个方面说明,使用等于在code测试字符串的平等,而不是 ==


I have an app structure like this:

Background:
The Parent activity #1 is just holding the tab host, tab widget and is also setting up the 3 tabs to have their content set to the 3 tab activities (tab1, tab2, tab3 activities).

Update: I tried calling my validation method inside onTabChangedListener in Parent Activity #1 but I got a Null Pointer Exception. Couldn't really trace it to anywhere. I did commented/deleted conflicting code but still I am not getting the bottleneck.StackTrace(PasteBin Link). Code for Parent Activity #1,Tab#1 Activity



Problem:
I want to validate data entered by user in the form field(s) in the individual tab activities onTabChanged event but I am unable to set more than a single setOnTabChangedListener.
Am I missing something here?
The listener(s) are set in their own tab# activities under oncreate method.

Apart from trying the above technique, I had tried setting up the listener in onResume() under the main Parent activity #1. But the on Resume() method was never invoked. I got a null pointer exception too.

Idea behind validation being: I want that while the user is changing tabs, the data should be validated before he can skip over a tab. So, ineffect I would require tab#1 to validate data in a event similar to onTabChanged if tab#2/tab#3 is selected.
Also, this would apply if current tab#2 is selected and user selects tab#1/tab#3

Any advice will be appreciated..
Thanks for reading..

解决方案

I want to validate data entered by user in the form field(s) in the individual tab activities onTabChanged event but I am unable to set more than a single setOnTabChangedListener.

There is no need for a second OnTabChangeListener and even if you could set it it wouldn't help you. As you constructed the code you need access to the child activities. You can do this by using one of the answers in this question. The problem is, that those answers, except the accepted one, use deprecated methods.

My method, that I proposed in the comments is to have a static boolean field in each of the child activities used as tabs and let all of your activities update that boolean flag whenever there is a change of state of the views in those activities(if you check a CheckBox, enter something in an EditText etc). Then you can simply check the flag for the desired child activity in the OnTabChangeListener. My method should work but your code is a bit messy so you would have to modify it quite a bit.

I had tried setting up the listener in onResume() under the main Parent activity #1. But the on Resume() method was never invoked. I got a null pointer exception too.

It's normal that you get a NullPointerException with your code as I haven't seen where you initialize the references to the child activities that you use in the OnTabChangeListener.

Also:

Don't use TabActivity. It's been deprecated in favor of the Fragments framework which is more flexible. Those fragments could help you because, I think you want to stop changing the tabs if the validation of the current page fails and the OnTabChangeListener might come a bit late for that(but I may be mistaken about what you want).

As a side note, use equals in your code to test String equality and not ==.

这篇关于如何验证在Tab切换制表输入数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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