代码之家  ›  专栏  ›  技术社区  ›  AIK

从alertDialog将数据发送回活动

  •  1
  • AIK  · 技术社区  · 9 年前

    大家好,需要一些帮助。我创建了一个自定义警报对话框,它接受用户输入,如用户名和密码。我跟着去了 the android developers site 我想做的是,一旦用户输入用户名和密码,并按下alertdialog中的登录按钮,我就想向创建该对话框的活动显示这些值。我被这件事耽搁了3个小时。任何帮助都将不胜感激。这是我的密码。

    主要活动.java

    public class MainActivity extends FragmentActivity implements NoticeDialogFragment.NoticeDialogListener{
    
    private Button dialogButton;
    private Button customDialogButton;
    private TextView customDialogTextview;
    
    private Button dialogWithInterface;
    private EditText dialogUsername;
    private EditText dialogPassword;
    String username;
    String password;
    
    
    public void showNoticeDialog() {
        // Create an instance of the dialog fragment and show it
        DialogFragment dialog = new NoticeDialogFragment();
        dialog.show(getFragmentManager(), "NoticeDialogFragment");
    
    }
    
    
    
    // The dialog fragment receives a reference to this Activity through the
    // Fragment.onAttach() callback, which it uses to call the following methods
    // defined by the NoticeDialogFragment.NoticeDialogListener interface
    @Override
    public void onDialogPositiveClick(DialogFragment dialog) {
        // User touched the dialog's positive button
    
    }
    
    @Override
    public void onDialogNegativeClick(DialogFragment dialog) {
        // User touched the dialog's negative button
    
    }
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        customDialogTextview = (TextView)findViewById(R.id.customdialogtext);
        customDialogTextview.setText("Email and Password: ");
    
        dialogButton = (Button)findViewById(R.id.dialogbutton);
        dialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 1. Instantiate an AlertDialog.Builder with its constructor
                final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                // Add the buttons
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // User clicked OK button
    
                    }
                });
    
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // User cancelled the dialog
                    }
                });
    
                // 2. Chain together various setter methods to set the dialog characteristics
                builder.setMessage("hello")
                        .setTitle("Dialog");
    
                // 3. Get the AlertDialog from create()
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });
    
        customDialogButton = (Button)findViewById(R.id.customdialogbutton);
        customDialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                // Get the layout inflater
                LayoutInflater inflater = MainActivity.this.getLayoutInflater();
    
                // Inflate and set the layout for the dialog
                // Pass null as the parent view because its going in the dialog layout
                builder.setView(inflater.inflate(R.layout.dialog, null));
    
                builder.setPositiveButton("Sign In", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //sign in the user
                    }
                });
    
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
    
                AlertDialog dialog = builder.create();
                dialog.show();
    
            }
        });
    
        dialogWithInterface = (Button)findViewById(R.id.dialogwithinterface);
        dialogWithInterface.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                showNoticeDialog();
            }
        });
    
    }
    

    通知对话框碎片.java

    public class NoticeDialogFragment extends DialogFragment {
    
    
    /* The activity that creates an instance of this dialog fragment must
     * implement this interface in order to receive event callbacks.
     * Each method passes the DialogFragment in case the host needs to query it. */
    public interface NoticeDialogListener {
        public void onDialogPositiveClick(DialogFragment dialog);
        public void onDialogNegativeClick(DialogFragment dialog);
    
    }
    
    // Use this instance of the interface to deliver action events
    NoticeDialogListener mListener;
    
    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (NoticeDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement NoticeDialogListener");
        }
    }
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Build the dialog and set up the button click handlers
    
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
    
        builder.setView(inflater.inflate(R.layout.dialog, null));
        builder.setPositiveButton("Sign In", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // Send the positive button event back to the host activity
                mListener.onDialogPositiveClick(NoticeDialogFragment.this);
                Toast.makeText(getActivity(), "positive", Toast.LENGTH_SHORT).show();
    
    
            }
        })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // Send the negative button event back to the host activity
                        mListener.onDialogNegativeClick(NoticeDialogFragment.this);
                    }
                });
        return builder.create();
    }
    
    }
    
    1 回复  |  直到 9 年前
        1
  •  5
  •   Lucas Crawford    9 年前

    查看您的活动实现的方法:

    @Override
    public void onDialogPositiveClick(DialogFragment dialog) {
        // User touched the dialog's positive button
    }
    

    它是您在名为NoticeDialogListener的对话框中创建的自定义界面的一部分,也是您希望对话框与调用它的活动通信的方式。

    public interface NoticeDialogListener {
        public void onDialogPositiveClick(DialogFragment dialog);
        public void onDialogNegativeClick(DialogFragment dialog);
    }
    

    更改此设置,使onDialogPostiveClick看起来像:

    public void onDialogPositiveClick(String name, String password);
    

    并在单击按钮时将EditText中的值传递到调用中,如下所示:

    builder.setPositiveButton("Sign In", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // Send the positive button event back to the host activity
            mListener.onDialogPositiveClick(mNameEdit.getText().toString(), mPasswordEdit.getText().toString());
        }
    });
    

    下一步是对在onDialogPositiveClick()方法的活动中覆盖的方法中的名称和密码值执行任何您想做的操作。

    @Override
    public void onDialogPositiveClick(String name, String password) {
        //do something with name and password here?
    }
    

    这似乎是最简单的方法来完成您想要对现有代码执行的操作。