How can I get the results from an AlertDialog?

后端 未结 6 1461
别跟我提以往
别跟我提以往 2020-12-16 10:57

I am using an AlertDialog.Builder to display a dialog to prompt the user to enter a password, I then want to save that password in a preference, however I can\'t figure out

相关标签:
6条回答
  • 2020-12-16 11:09

    Weak but speedy solution:

    Declare an Activity level method:

    TextView lblDiaDestination;
    
    public void dialogReturn(String msg){
       lblDiaDestination.setText(msg);
    }
    

    and call it from positive button click listner:

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            dialogReturn(   input.getText().toString().trim()   );
        });
    

    You can change lblDiaDestination value just before alert.show():

    lblDiaDestination = myTextView;
    alert.show():
    
    0 讨论(0)
  • 2020-12-16 11:14

    This quite long. I hope my contribution could be useful for the the new comers. I think the key to answer the question is declaring the variable result as member of the activity (let's call it YourActivity). Then from inside the "onClick" method, use YourActivity.this.result to access this variable.

    I edit @finiteloop codes as below:

    public class YourActivity 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) {        
                   YourActivity.this.result = input.getText().toString();
                }
            });
            b.setNegativeButton("CANCEL", null);
            b.create().show();
        }
    }
    
    0 讨论(0)
  • 2020-12-16 11:15
    public class MyActivity extends Activity {
        private String result;
    
        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) {
                    result = input.getText().toString();
                }
            });
            b.setNegativeButton("CANCEL", null);
            b.show();
        }
    }
    
    0 讨论(0)
  • 2020-12-16 11:18

    Simplified example:

    public interface TextListener {
        void onPositiveResult(CharSequence text);
    }
    
    public static AlertDialog getTextDialog(Context ctx,
            final TextListener listener) {
        View view = LayoutInflater.from(ctx).inflate(R.layout.dialog, null);
        final TextView tv = (TextView) view.findViewById(R.id.tv);
        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setView(view);
        //
        builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
    
            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.onPositiveResult(tv.getText());
            }
        });
        builder.setNegativeButton(android.R.string.cancel, null);
        return builder.create();
    }
    

    -- EDIT -- Or you try this code:

     public class Main extends Activity {
        private Button btn;
        private String result;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            btn = (Button) findViewById(R.id.button1);
            btn.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    showDialog(0);
                }
            });
        }
    
        @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case 0:
                final EditText input = new EditText(this);
    
                return new AlertDialog.Builder(this)
                .setIcon(R.drawable.icon)
                .setTitle("Please enter a password")
                .setView(input)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
    
                    public void onClick(DialogInterface dialog, int whichButton) {
                        Toast.makeText(getBaseContext(), input.getText().toString(), Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    
                    public void onClick(DialogInterface dialog, int whichButton) {
                        Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_SHORT).show();
                    }
                }).create();
            }
    
            return null;
        }
    }
    
    0 讨论(0)
  • 2020-12-16 11:28

    You can follow the Dialog tutorial in developer.android.com.

    First create dialog class:

    Adding a list

    Then add listener to this class to pass result to the activity.

    Passing Events Back to the Dialog's Host

    0 讨论(0)
  • 2020-12-16 11:29

    Variable result , make it a member variable , instead of local variable. By making "result" as member variable it is accessible in the entire activity.(parent class which extends activity)

    0 讨论(0)
提交回复
热议问题