получить доступ к состоянию виджетов в DialogPreference - PullRequest
0 голосов
/ 21 января 2011

У меня есть диалог в моей активности предпочтений, определенный следующим образом (макет определен в xml):

public class DialogPreference extends android.preference.DialogPreference {
    public DialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DialogPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void onClick (DialogInterface dialog, int which) {
        if (DialogInterface.BUTTON_POSITIVE == which) {
            if (getKey() == 
                getContext().getResources().getText(R.string.blabla)) {
                // FIXME ??? - how ?
                Log.v(TAG, "DialogPreference: onClick");
            }
        }
    }
}

В диалоговом окне есть несколько виджетов, а именно RadioGroup и несколько RadioButtons.В настоящее время я не могу найти способ доступа к этим виджетам в методе onClick.

Как получить доступ к этим виджетам?

Ответы [ 2 ]

2 голосов
/ 27 января 2011

Вы можете разыграть DialogInterface до AlertDialog примерно так:

public void onClick (DialogInterface dialog, int which) {
    if (DialogInterface.BUTTON_POSITIVE == which) {
        RadioButton button = (RadioButton) ((AlertDialog) dialog).findViewById(R.id.radio_button_id);
        // continue using button
    }
}

(или что-то подобное)

1 голос
/ 21 января 2011

Ниже весь мой код, как я это делаю

final AlertDialog.Builder Main_Dialog = new AlertDialog.Builder(this);
            ////This is how to set layout
            final View layout = getLayoutInflater().inflate(R.layout.conpassword,null);
    Main_Dialog.setTitle("Choose reminder");
    Main_Dialog.setView(layout);


    Main_Dialog.setTitle("Enter IP Address:");
    Main_Dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int whichButton)
        {
            connectWIFi();
            if (connect && ni.getType() == ConnectivityManager.TYPE_WIFI)
            {
                                    final ImageButton input = (ImageButton) layout.findViewById(R.id.ImageButton01);
                saveIP(input.getText().toString(), deviceId);

                Thread cThread = new Thread(new ClientServer(LoginScreen.this, input.getText().toString(),
                        deviceId, mHandler));
                cThread.start();

                dialog.dismiss();
            }
            else
            {
                Builder builder = new AlertDialog.Builder(LoginScreen.this);
                builder.setTitle("Alert !");
                builder.setMessage("No WiFi connection. Please check your WiFi settings");
                builder.setPositiveButton("Ok", null);
                builder.show();
            }
        }
    });
    Main_Dialog.show();

Я не знаю, почему вы не получаете getLayoutInflater, но этот код работает без проблем со мной

...