How can can I add custom buttons into an AlertDialog's layout?

后端 未结 6 882
误落风尘
误落风尘 2020-12-09 08:48

I have AlertDialog with Positive and Negative buttons. In AlertDialog layout I have EditText and two Buttons (btnAdd1, btnAdd2). I want when user click at the Button btnAdd1

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

    This is the way I did.

    custom_alert_dialog.xml file created

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>
    
    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text2"
        app:layout_constraintTop_toBottomOf="@+id/text1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>
    
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="OK"
        app:layout_constraintTop_toBottomOf="@+id/text2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>
    
    </android.support.constraint.ConstraintLayout>
    

    In activity file

    LayoutInflater layoutInflater = getLayoutInflater();
    View alertLayout = layoutInflater.inflate(R.layout.custom_alert_dialog, null);
    Button button = alertLayout.findViewById(R.id.button1);
    
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
    
    alertDialog.setCancelable(false);
    alertDialog.setView(alertLayout);
    AlertDialog dialog = alertDialog.create();
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    dialog.show();
    
    0 讨论(0)
  • 2020-12-09 09:27

    According to this approach i am able to create the image button but if i want to dismiss or cancel dialog on Cancel button then what i have to do..

    public static void alertDialogShow(final Context context,
                final String resultMobile) {
    
            LayoutInflater li = LayoutInflater.from(context);
            View promptsView = li.inflate(R.layout.prompt,
                    null);
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
            // set prompts.xml to alertdialog builder
            alertDialogBuilder.setView(promptsView);
            final EditText userInput = (EditText) promptsView
                    .findViewById(R.id.editTextDialogUserInput);
            userInput.setText(resultMobile);
            userInput.setEnabled(false);
    btnCancel.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
    
                }
            });
    
    0 讨论(0)
  • 2020-12-09 09:28

    The following code will inflate a view from R.layout.prompt and set it to the AlertDialog. The positive and negative buttons will not be used. You can set the onClick behaviors for btnAdd1 and btnAdd2:

    LayoutInflater layoutInflater = LayoutInflater.from(this);
    View promptView = layoutInflater.inflate(R.layout.prompt, null);
    
    final AlertDialog alertD = new AlertDialog.Builder(this).create();
    
    EditText userInput = (EditText) promptView.findViewById(R.id.userInput);
    
    Button btnAdd1 = (Button) promptView.findViewById(R.id.btnAdd1);
    
    Button btnAdd2 = (Button) promptView.findViewById(R.id.btnAdd2);
    
    btnAdd1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
    
            // btnAdd1 has been clicked
    
        }
    });
    
    btnAdd2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
    
            // btnAdd2 has been clicked
    
        }
    });
    
    alertD.setView(promptView);
    
    alertD.show();
    
    0 讨论(0)
  • 2020-12-09 09:28

    My solution for your question.

     LayoutInflater layoutInflater = LayoutInflater.from(this);
     View promptView = layoutInflater.inflate(R.layout.prompt, null);
    
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
     alertDialogBuilder.setView(promptView);
    
     Button btn_1= (Button)promptView.findViewById(R.id.btnAdd1);
     btn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
               //do required function
               // don't forget to call alertD.dismiss()
    
            }
        });
    
    
     Button btn_2 = (Button)promptView.findViewById(R.id.btnAdd2);
     btn_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
               //do required function
    
            }
        });
    
    
    alertDialogBuilder
            .setCancelable(false)
    
    
    
    AlertDialog alertD = alertDialogBuilder.create();
    alertD.show();
    
    0 讨论(0)
  • 2020-12-09 09:29

    what you want to do is;

    alertD.show();
    Button button = (Button)promptView.findViewById(R.id.buttonId);
    button.setOnClickListener(....)
    

    using the view to call findViewById, rather than the activity, which will look for the id in the layout that is being displayed.

    0 讨论(0)
  • 2020-12-09 09:33

    You could try something like this :

    dialog.setPositiveButton(R.string.positive, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialog.show();
        }
    });
    
    0 讨论(0)
提交回复
热议问题