获取警报对话框选择值以编辑文本

时间:2014-11-25 10:25:22

标签: android android-alertdialog

我正在按照本教程http://labs.makemachine.net/2010/03/android-multi-selection-dialogs/进行操作,我希望获得一个警报对话框的选定值,我的edittext可以帮助我,我需要在edittext中获取详细信息

    public class PagesFragment extends Fragment{

/*private PopupWindow pw;
private boolean expanded;       //to  store information whether the selected values are displayed completely or in shortened representatn
public static boolean[] checkSelected;*/

private Spinner spfrom;
private Spinner spto;
private Spinner spheightfrom;
private Spinner spheightto;
private Spinner splang;
private Spinner sprelg;
private Spinner spmrgstatus;
private Spinner spcountry;
private Spinner spstate;
private Spinner spcity;
private EditText tv;
private ImageView btnmother;
private ImageView btnrelgn;
protected CharSequence[] _options = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
protected boolean[] _selections = new boolean[_options.length];

protected Button _optionsButton;
protected EditText inputTemp;
private EditText lang;
private ImageButton btnlangs;
static Dialog d ;


public PagesFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    //String strtext = getArguments().getString("position");
    View rootView = inflater.inflate(R.layout.search_profile, container, false);

    lang = (EditText) rootView.findViewById(R.id.edtmothertongue);
     btnlangs = (ImageButton) rootView.findViewById(R.id.btnmothertongue);
    spfrom=(Spinner)rootView.findViewById(R.id.spinneragefrom);
    spto=(Spinner)rootView.findViewById(R.id.spinnerageto);
    spheightfrom=(Spinner)rootView.findViewById(R.id.spinnerhfrom);
    spheightto=(Spinner)rootView.findViewById(R.id.spinnerhto);
    //splang=(Spinner)rootView.findViewById(R.id.spinnerlang);
    //sprelg=(Spinner)rootView.findViewById(R.id.spinnerrel);
    spmrgstatus=(Spinner)rootView.findViewById(R.id.spinnermrgstatus);
    spcountry=(Spinner)rootView.findViewById(R.id.spinnercountry);
    spstate=(Spinner)rootView.findViewById(R.id.spinnerstate);
    spcity=(Spinner)rootView.findViewById(R.id.spinnercity);

    btnlangs.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            showDialog(0);

        }
    });
    @Override
    protected Dialog onCreateDialog(int id) {
        return
                new AlertDialog.Builder(this)
                        .setTitle("Planets")
                        .setMultiChoiceItems(_options, _selections, new DialogSelectionClickHandler())
                        .setPositiveButton("OK", new DialogButtonClickHandler())
                        .create();
    }

    public class DialogSelectionClickHandler implements DialogInterface.OnMultiChoiceClickListener {
        public void onClick(DialogInterface dialog, int clicked, boolean selected) {
            Log.i("ME", _options[clicked] + " selected: " + selected);
        }
    }


    public class DialogButtonClickHandler implements DialogInterface.OnClickListener {
        public void onClick(DialogInterface dialog, int clicked) {
            switch (clicked) {
                case DialogInterface.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;
            }
        }
    }

    protected void printSelectedPlanets(){
        for( int i = 0; i < _options.length; i++ ){
            Log.i( "ME", _options[ i ] + " selected: " + _selections[i] );
        }
    }
 // on click of button display the dialog


    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(getActivity().getBaseContext(),
            R.array.height_from, R.layout.my_spinner);
    adapter1.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
    spheightfrom.setAdapter(adapter1);


    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(getActivity().getBaseContext(),
            R.array.height_to, R.layout.my_spinner);
    adapter2.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
    spheightto.setAdapter(adapter2);


    ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(getActivity().getBaseContext(),
            R.array.age_from, R.layout.my_spinner);
    adapter3.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
    spfrom.setAdapter(adapter3);

    ArrayAdapter<CharSequence> adapter10 = ArrayAdapter.createFromResource(getActivity().getBaseContext(),
            R.array.age_to, R.layout.my_spinner);
    adapter3.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
    spto.setAdapter(adapter10);


    btnrelgn=(ImageView)rootView.findViewById(R.id.btnrelgn);

    btnrelgn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent=new Intent(getActivity(),ReligionList.class);
            startActivity(intent);

        }
    });




    ArrayAdapter<CharSequence> adapter5 = ArrayAdapter.createFromResource(getActivity().getBaseContext(),
            R.array.mrgstatus, R.layout.my_spinner);
    adapter5.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
    spmrgstatus.setAdapter(adapter5);


    ArrayAdapter<CharSequence> adapter6=ArrayAdapter.createFromResource(getActivity(). getBaseContext(), R.array.country,R.layout.my_spinner);
    adapter6.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
    spcountry.setAdapter(adapter6);


    ArrayAdapter<CharSequence> adapter7=ArrayAdapter.createFromResource(getActivity(). getBaseContext(), R.array.state,R.layout.my_spinner);
    adapter7.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
    spstate.setAdapter(adapter7);


    ArrayAdapter<CharSequence> adapter8=ArrayAdapter.createFromResource(getActivity(). getBaseContext(), R.array.city,R.layout.my_spinner);
    adapter8.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
    spcity.setAdapter(adapter8);
    return rootView;

}

}

1 个答案:

答案 0 :(得分:0)

首先,你必须在onCreate()中初始化EditText,并在点击的正按钮上设置或更改值:

示例:

    protected CharSequence[] _options = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
    protected boolean[] _selections = new boolean[_options.length];

    protected Button _optionsButton;
    protected EditText inputTemp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        inputTemp = (EditText) findViewById(R.id.edittexts);
        _optionsButton = (Button) findViewById(R.id.button);
        _optionsButton.setOnClickListener(new ButtonClickHandler());
    }

    public class ButtonClickHandler implements View.OnClickListener {
        public void onClick(View view) {
            showDialog(0);
        }
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        return
                new AlertDialog.Builder(this)
                        .setTitle("Planets")
                        .setMultiChoiceItems(_options, _selections, new DialogSelectionClickHandler())
                        .setPositiveButton("OK", new DialogButtonClickHandler())
                        .create();
    }

    public class DialogSelectionClickHandler implements DialogInterface.OnMultiChoiceClickListener {
        public void onClick(DialogInterface dialog, int clicked, boolean selected) {
            Log.i("ME", _options[clicked] + " selected: " + selected);
        }
    }


    public class DialogButtonClickHandler implements DialogInterface.OnClickListener {
        public void onClick(DialogInterface dialog, int clicked) {
            switch (clicked) {
                case DialogInterface.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;
            }
        }
    }