How can can I add custom buttons into an AlertDial

2020-02-04 07:28发布

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 or btnAdd2 add same text to EditText in AlertDialog (but no close AlertDialog). Is this possible do in AlertDialog or I have to use only Dialog?

This is layout (R.layout.prompt) of AlertDialog:

<LinearLayout>
<EditText
    android:id="@+id/userInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/btnAdd1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="bla" />

<Button
    android:id="@+id/btnAdd2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="bla" />

  </LinearLayout>

And this is source code:

    LayoutInflater layoutInflater = LayoutInflater.from(this);
        View promptView = layoutInflater.inflate(R.layout.prompt, null);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setView(promptView);
    alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                              //...

                }
            })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });

    AlertDialog alertD = alertDialogBuilder.create();
    alertD.show();

I want get acces to the btnAdd1 and btnAdd2 from the layout. Set the OnClickListener() to these two buttons.

6条回答
再贱就再见
2楼-- · 2020-02-04 07:30

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();
查看更多
我只想做你的唯一
3楼-- · 2020-02-04 07:31

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();
查看更多
Deceive 欺骗
4楼-- · 2020-02-04 07:32

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.

查看更多
虎瘦雄心在
5楼-- · 2020-02-04 07:32

You could try something like this :

dialog.setPositiveButton(R.string.positive, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        dialog.show();
    }
});
查看更多
forever°为你锁心
6楼-- · 2020-02-04 07:36

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) {

            }
        });
查看更多
够拽才男人
7楼-- · 2020-02-04 07:57

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();
查看更多
登录 后发表回答