Android - change dialog button location

前端 未结 4 701
无人及你
无人及你 2021-01-13 14:09

Is it possible to change the location of the button on the dialog to the outside of the dialog itself? something like this (the red squares are buttons):

4条回答
  •  温柔的废话
    2021-01-13 14:56

    You will need to create a custom DialogFragment. Below I will give an analytical example of how to implement one and call it with several parameters each time, so you won't need to repeat code each time you want an Dialog with different message.

    CustomAlertDialog.java

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.os.Bundle;
    import android.support.v4.app.DialogFragment;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    /**
    * Custom DialogFragment class
    */
    public class CustomAlertDialog extends DialogFragment implements
    View.OnClickListener {
    /**
     * Interface for receiving the wanted callbacks
     * */
    public interface CallbacksListener
    {
        public void onPositiveButtonClicked();
    
        public void onNegativeButtonClicked();
    }
    
    private CallbacksListener callbacksListener;
    
    public void setCallbacksListener(CallbacksListener callbacksListener)
    {
        this.callbacksListener = callbacksListener;
    }
    
    public CustomAlertDialog()
    {
        //empty constructor
    }
    
    private String titleString;
    private String messageString;
    private String positiveString;
    private String negativeString;
    
    @Override
    public void setArguments(Bundle bundle)
    {
        titleString = bundle.getString("titleString");
        messageString = bundle.getString("messageString");
        positiveString = bundle.getString("positiveString");
        negativeString = bundle.getString("negativeString");
    }
    
    public static CustomAlertDialog newInstance(AlertDialogStrings alertDialogStrings)
    {
        CustomAlertDialog customAlertDialog = new CustomAlertDialog();
        Bundle b = new Bundle();
        b.putString("titleString", alertDialogStrings.titleString);
        b.putString("messageString", alertDialogStrings.messageString);
        b.putString("negativeString", alertDialogStrings.negativeString);
        b.putString("positiveString", alertDialogStrings.positiveString);
        customAlertDialog.setArguments(b);
    
        return customAlertDialog;
    }
    
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View v = getActivity().getLayoutInflater().inflate(R.layout.custom_alert_dialog, null);
        TextView titleTV = (TextView) v.findViewById(R.id.title_customAlertDialog);
        TextView messageTV = (TextView) v.findViewById(R.id.message_customAlertDialog);
        Button positiveButton = (Button) v.findViewById(R.id.okBtn_customAlertDialog);
        Button negativeButton = (Button) v.findViewById(R.id.cancelBtn_customAlertDialog);
        titleTV.setText(titleString);
        messageTV.setText(messageString);
        positiveButton.setText(positiveString);
        negativeButton.setText(negativeString);
        positiveButton.setOnClickListener(this);
        negativeButton.setOnClickListener(this);
    
        builder.setView(v);
        return builder.create();
    }
    
    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.okBtn_customAlertDialog:
                callbacksListener.onPositiveButtonClicked();
                dismiss();
                break;
            case R.id.cancelBtn_customAlertDialog:
                callbacksListener.onNegativeButtonClicked();
                dismiss();
                break;
            default:
                break;
        }
    }
    
    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        try
        {
            callbacksListener = (CallbacksListener) activity;
        }
        catch (ClassCastException e)
        {
            throw new ClassCastException(activity.toString()
                    + " must implement CallbacksListener");
        }
    }
    
    @Override
    public void onDetach()
    {
        super.onDetach();
        callbacksListener = null;
    }
    
    /**
     * Class for saving the wanted Strings we want to have on our CustomDialog implementation
     * */
    public static class AlertDialogStrings
    {
        public String titleString;
        public String messageString;
        public String positiveString;
        public String negativeString;
    
        public AlertDialogStrings(String title, String message, String positiveString, String negativeString)
        {
            this.messageString = message;
            this.titleString = title;
            this.positiveString = positiveString;
            this.negativeString = negativeString;
        }
      }
    }
    

    custom_alert_dialog.xml:

    
    
    
    
    
    
    
    
    
        

    To show your customAlertDialog:

    private void popUpAlertDialog() 
    {
        String title = "My title here?";
        String message = "My Message here";
        String positiveString = "OK";
        String negativeString = "Cancel";
        CustomAlertDialog.AlertDialogStrings customDialogStrings =
                new CustomAlertDialog.AlertDialogStrings
                        (title, message, positiveString, negativeString);
        CustomAlertDialog customAlertDialog =
                CustomAlertDialog.newInstance(alertDialogStrings);
        customAlertDialog.show(getSupportFragmentManager(), "customAlertDialog");
        customAlertDialog.setCallbacksListener(new CustomAlertDialog.CallbacksListener()
        {
            @Override
            public void onPositiveButtonClicked()
            {
               //do something 
            }
    
            @Override
            public void onNegativeButtonClicked()
            {
               //do something
            }
        });
    }
    

    The AlertDialogStrings class helps us maintain our wanted strings in a way that we can re-use our class with different strings each time and the CallbacksListener helps as settle the way of the OnClick responds each time. Note that this design follows the Material Design Dialog style principles.

提交回复
热议问题