Robotium:如何使用clickOnButton(<name>)进行自定义按钮</name>

时间:2011-08-29 05:58:56

标签: android android-widget robotium

我是Android机器人的新手。我有自定义小部件(MyButton,MyTextView,MyCheckBox等..),它从本机android小部件继承。如何在robotium脚本中为自定义控件添加click事件?

我尝试使用Solo.clickOnButton(“Test Button”),其中“Test Button”是MyButton的一个实例,但我没有获得Button的click事件。任何建议都会非常有用。

谢谢, -Ron ..

1 个答案:

答案 0 :(得分:0)

我建议您使用扩展按钮等创建MyButton

对于asign点击操作,您应该使用普通表单。对于例子

main.xml中:

<com.Ron.MyButton
    android:id="@+id/custom_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

在您的代码中,您可以访问该按钮

 Button myButton  = (Button)findViewById(R.id.custom_button);

然后像使用其他普通按钮一样执行onClick动作:

 myButton.setOnclickListener(new onclickListener ....

将onClickAction作为所有视图的其他方法是使用int xml:

<com.Ron.MyButton
    android:id="@+id/custom_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="nameOfMethod"/>  

然后在你的代码中:

  public void nameOfMethod (View v){
    //code when click the view 
  }

(这适用于所有视图,linearlayout,imageviews,bustom按钮....所有)