0

私の現在の使命は、いくつかの Android GUI テストを作成することであり、ベース ビューから開かれたダイアログにある要素への参照を見つけるのに苦労しています。

setContentView(R.layout.dialog_new_type); 

コントロールを見つけるには、findViewById、f.ex を使用します。

Button addType = (Button) mainActivity.findViewById(R.id.main_menu_button_add_type);

これはうまくいきます。このボタンを押すと、カスタム ダイアログが開きます。

new DialogNewType(v.getContext(), controller).show();

そのダイアログにあるコントロールを見つけると、頭が痛くなります。

基本的な考え方は次のようなものだと思います findViewById(R.id.dialog_new_type).findViewById(R.id.whatever_control_on_the_dialog)が、ダイアログへの参照は null を返します。また、を使用してダイアログへの参照を取得しようとしましたが、findViewById(R.layout.my_dialog)null も返されます。

または、次のように、Robotium フレームワークを使用してコントロールに到達しようとしました。

ArrayList<EditText> test = solo.getCurrentViews(EditText.class);
EditText et = test.get(0); 

このようにして、必要なコントロールへの参照を取得しますが、それは非常に汚いソリューションであり、「プレーンな古い Android」を使用して同じソリューションに到達することを好みます。

ダイアログのレイアウトは次のようになります。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/dialog_new_type"
>

    <EditText 
     android:hint="@string/dialog_new_type_edit_hint"
     android:inputType="textAutoCorrect"
     android:layout_height="wrap_content"
     android:layout_width="fill_parent"
     android:visibility="visible"
     android:id="@+id/dialog_new_type_edit">
        <requestFocus></requestFocus>
    </EditText>
</LinearLayout>
4

1 に答える 1

1

ダイアログはビューを拡張しないため、使用できません:

findViewById(R.id.dialog_new_type);

なぜあなただ​​けを使用しないのですか:

findViewById(R.id.whatever_control_on_the_dialog)
于 2013-08-04T17:27:10.510 に答える