How can i get the status of check box

99封情书 提交于 2020-01-16 18:30:41

问题


I am trying to get the status of Checkbox using androidviewclient

lock = vc.findViewWithText('Lock SIM card')
if (lock.isChecked()):
    print "Enabled"
else:
    print "Disabled"

this always returns False .

can anybody tell me how to get the status of checkbox


回答1:


That's because lock is not the ckeckbox. Lock is the text view containing "Lock SIM card". You need to use hierarchyviewer or uiautomatorviewer to see the layout of the UI.

The UI looks something like:

(0) Linear layout
    (1) Relative layout
        (0) TextView "Lock SIM card"
        (1) TextView "Require PIN to use phone"
    (2) Linear layout
        (0) CheckBox

So you need to do something like:

linear_layout = lock.parent.parent
check_box = linear_layout.children[2].children[0]

It's not straight forward, but I used this in the past and worked. Please note that the ViewClient version that I used was 2.3. Now it has evolved to 4+.




回答2:


This might work:

 vc.dump()
 lock = vc.findViewWithText('Lock SIM card').getParent().getChildren()                                                                                                                                                   [2].getChecked()
    if lock != True:
       print 'Lock SIM card not enabled...'
       vc.dump()
       vc.findViewWithText('Lock SIM card').touch()
    else:
       print 'Lock SIM card already enabled...'


来源:https://stackoverflow.com/questions/19510146/how-can-i-get-the-status-of-check-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!