Skip to content Skip to sidebar Skip to footer

Get The Alert Dialog Selected Value To Edittext

I am following this tutorial http://labs.makemachine.net/2010/03/android-multi-selection-dialogs/ and I want to get selected value of alert dialog to my edittext can any one help m

Solution 1:

First you have to initialize EditText in onCreate() and set or change value at positive button clicked :

Example :

protectedCharSequence[] _options = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
    protectedboolean[] _selections = newboolean[_options.length];

    protectedButton _optionsButton;
    protectedEditText inputTemp;

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        inputTemp = (EditText) findViewById(R.id.edittexts);
        _optionsButton = (Button) findViewById(R.id.button);
        _optionsButton.setOnClickListener(newButtonClickHandler());
    }

    publicclassButtonClickHandlerimplementsView.OnClickListener {
        publicvoidonClick(View view) {
            showDialog(0);
        }
    }

    @OverrideprotectedDialogonCreateDialog(int id) {
        returnnewAlertDialog.Builder(this)
                        .setTitle("Planets")
                        .setMultiChoiceItems(_options, _selections, newDialogSelectionClickHandler())
                        .setPositiveButton("OK", newDialogButtonClickHandler())
                        .create();
    }

    publicclassDialogSelectionClickHandlerimplementsDialogInterface.OnMultiChoiceClickListener {
        publicvoidonClick(DialogInterface dialog, int clicked, boolean selected) {
            Log.i("ME", _options[clicked] + " selected: " + selected);
        }
    }


    publicclassDialogButtonClickHandlerimplementsDialogInterface.OnClickListener {
        publicvoidonClick(DialogInterface dialog, int clicked) {
            switch (clicked) {
                caseDialogInterface.BUTTON_POSITIVE:
                    inputTemp.setText("");
                    for (int i = 0; i < _options.length; i++) {
                        if(_selections[i]){
                            if(inputTemp.getText().toString().length()>0){
                                inputTemp.setText(inputTemp.getText().toString().trim()+","+_options[i]);
                            }else{
                                inputTemp.setText(_options[i]);
                            }
                        }

                    }
                    break;
            }
        }
    }

Post a Comment for "Get The Alert Dialog Selected Value To Edittext"