找到你要的答案

Q:AlertDialog get selected value

Q:AlertDialog得到价值选择

This function create my alert dialog, and add two listeners, one to display the dialog when the users click on the EditText, and one to dismiss and fill the EditText when an AlertDialog value is selected.

I'm trying to retrieve the selected value of a AlertDialog, in order to fill an EditText.

I know where I can retrieve this value, but unfortunately, I don't know how.

public void addSex () {
    final AlertDialog.Builder builder = new AlertDialog.Builder(SignupActivity.this);
    builder.setTitle("Votre sexe").setItems(R.array.sex_array,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //Get dialog selected value
                }
            }
    );

    _sexText.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            AlertDialog dialog = builder.create();
            if (event.getAction() == MotionEvent.ACTION_DOWN)   dialog.show();
            return false;
        }
    });
}

这个函数创建我的警告对话框,并添加两个听众,一个显示对话框时,用户点击编辑,和一把和填充EditText当警告对话框选择价值。

我试图找回一个AlertDialog选定的值,为了填补一个EditText。

我知道我在哪里可以找回这个价值,但不幸的是,我不知道如何。

public void addSex () {
    final AlertDialog.Builder builder = new AlertDialog.Builder(SignupActivity.this);
    builder.setTitle("Votre sexe").setItems(R.array.sex_array,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //Get dialog selected value
                }
            }
    );

    _sexText.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            AlertDialog dialog = builder.create();
            if (event.getAction() == MotionEvent.ACTION_DOWN)   dialog.show();
            return false;
        }
    });
}
answer1: 回答1:
public class ABC extends Activity {
    private String result;

    // other activity stuff 

    void showDialog(){
    AlertDialog.Builder b = new AlertDialog.Builder(this);
    b.setTitle("Please enter a password");
    final EditText input = new EditText(this);
    b.setView(input);
    b.setPositiveButton("OK", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int whichButton)
        {
           // SHOULD NOW WORK
           result = input.getText().toString();
        }
    });
    b.setNegativeButton("CANCEL", null);
    b.create().show();
    }
}

Reference : How can I get the results from an AlertDialog?

public class ABC extends Activity {
    private String result;

    // other activity stuff 

    void showDialog(){
    AlertDialog.Builder b = new AlertDialog.Builder(this);
    b.setTitle("Please enter a password");
    final EditText input = new EditText(this);
    b.setView(input);
    b.setPositiveButton("OK", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int whichButton)
        {
           // SHOULD NOW WORK
           result = input.getText().toString();
        }
    });
    b.setNegativeButton("CANCEL", null);
    b.create().show();
    }
}

参考:我如何从一个警告对话框的结果吗?

answer2: 回答2:

Simply use which value to know selected element.

    @Override
public void onClick(DialogInterface dialog, int which) {
            //Get dialog selected value
            String[] sexArray =  getResources().getStringArray(R.array.sex_array);
            _sexText.setText(sexArray[which]);    

}

只需使用哪个值知道选定元素。

    @Override
public void onClick(DialogInterface dialog, int which) {
            //Get dialog selected value
            String[] sexArray =  getResources().getStringArray(R.array.sex_array);
            _sexText.setText(sexArray[which]);    

}

android  android-alertdialog  selecteditem  selectedvalue