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

后端 未结 6 908
误落风尘
误落风尘 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: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();
    

提交回复
热议问题