2012-04-27 62 views
10

我是Robotium的新手。我使用对话框构建器创建了一个警告对话框,并使用show命令调用它。我可以使用Robotium默认触发'ok'按钮,我无法为'取消'按钮做同样的操作。由于对话框没有与id关联,我不确定如何获取按钮的id。这里是我的对话框如何选择哪个按钮在Robotium上单击以提醒对话框?

alertDialogBuilder 
.setMessage("Please enter only numbers without any spaces") 
.setCancelable(true) 
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int id) { 
dialog.cancel(); 
} 
}) 
.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int id) { 
dialog.cancel(); 
} 
}); 

代码我用来触发测试类中的“确定”按钮的代码是

solo.getCurrentActivity().runOnUiThread(new Runnable() { 
public void run() { 
solo.getCurrentActivity().getCurrentFocus().requestFocus(); 
} 
}); 
this.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER); 

如何做同样的“取消”按钮?提前致谢。

回答

9

只要使用solo.clickOnButton( “取消”);

+0

ya。修好了。感谢Renas – 2012-05-16 01:06:07

22

其实,我建议你做solo.clickOnView(solo.getView(buttonId))在“积极”按钮android.R.id.button1,“否定”按钮android.R.id.button2和“中性”是android.R.id.button3

+3

对于我来说,这比我接受的答案更加可靠。谢谢。 – Eoin 2013-08-08 12:23:53

+2

如果我们有自定义对话框怎么办? – DJhon 2014-08-20 10:16:51

相关问题