簡體   English   中英

Robotium:如何使用clickOnButton( <name> )用於自定義按鈕

[英]Robotium: how to use clickOnButton(<name>) for custom buttons

我是Android Robotium的新手。 我有自定義的小部件(MyButton,MyTextView,MyCheckBox等。),這些小部件是從本地android小部件繼承的。 如何在Robotium腳本中為自定義控件添加click事件?

我嘗試使用Solo.clickOnButton(“ Test Button”),其中“ Test Button”是MyButton的實例,但是我沒有獲得Button的click事件。 任何建議都會很有幫助。

謝謝-羅恩

我想你使用擴展按鈕等創建MyButton

要指定點擊操作,您應該使用常規形式。 舉個例子

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賦予所有視圖的其他方法是使用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 
  }

(這適用於所有視圖,線性布局,圖像視圖,濃淡按鈕...。全部)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM