0

私のアプリケーションでは、次のコードを含むダイアログ ボックスを表示します。

final AlertDialog.Builder builder = new AlertDialog.Builder(
        getActivity());
final View myDialogForm = getActivity().getLayoutInflater()
        .inflate(R.layout.my_dialog, null);

builder.setTitle(R.string.addCompanyDialogTitle)
    .setView(myDialogForm)
    .create()
    .show();

と が含まれていSpinnerますTextView。ユーザーがスピナーで何かを選択すると、テキスト ビューを更新する必要があります。

これをどのように実装できますか (によって生成されたダイアログ ボックスでスピナーの選択に反応しますAlertDialog.Builder) ?

4

2 に答える 2

1

次のような再帰関数を使用して、ダイアログのビューを反復処理できます。

    Dialog di = builder.setTitle(R.string.addCompanyDialogTitle).setView(myDialogForm).create();
    updateMessage(di.getWindow().getDecorView());
    di.show();
    //...
    private void updateMessage(View parent){
     if(parent instanceof ViewGroup){
        ViewGroup vg = (ViewGroup) parent;
        for(int i=0; i < vg.getChildCount();i++){
            View v = vg.getChildAt(i); 
            if(v instanceof RelativeLayout){
                RelativeLayout rl = (RelativeLayout) v;
                TextView tv = (TextView) rl.getChildAt(1);
                Spinner sp = (Spinner) rl.getChildAt(0);
                sp.setOnItemSelectedListener( 
                                        //..
                                        tv.setText(sp_value); 
                                        //..
                                    )
                break;
            } else {
                updateMessage(v);
            }
        }                       
    }
   }

your_root_view の助けを借りて、R.layout.my_dialog にいるときを制御できます。instanceOf私の例では RelativeLayout です。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android">

    <Spinner ... />

    <TextView ... />

 </RelativeLayout>

参照

于 2013-04-28T21:27:55.653 に答える
0

私はこの方法で問題を解決しました:

1) 上記のコードを次のように変更します。

final AlertDialog.Builder builder = new AlertDialog.Builder(
        getActivity());
final View myDialogForm = getActivity().getLayoutInflater()
        .inflate(R.layout.my_dialog, null);

final Spinner mySpinner = 
    (Spinner) addCompanyDialogForm.findViewById(R.id.spinner_id);

finalOutputSpinner.setOnItemSelectedListener(this);

builder.setTitle(R.string.addCompanyDialogTitle)
    .setView(myDialogForm)
    .create()
    .show();

2) クラスにインターフェースを実装させますAdapterView.OnItemSelectedListener

3) そのクラスでそのインターフェースのメソッドを実装します。

@Override
public void onItemSelected(final AdapterView<?> aParent, final View aView,
        final int aPosition, final long aRowId) {
    // aRowId contains the index of the selected item
}

@Override
public void onNothingSelected(final AdapterView<?> aParent) {
    // Nothing is selected
}
于 2013-05-01T11:27:11.137 に答える