alertDialog with PositiveButton and NegativeButton from xml

后端 未结 2 973
隐瞒了意图╮
隐瞒了意图╮ 2020-12-22 07:01

I have alert dialog and both PositiveButton and NegativeButton sited programmatically , i want to retrieve them from xml layout ,i try to do it but im new to android develop

相关标签:
2条回答
  • 2020-12-22 07:27

    You can use your dialog_layout to retrieve your Buttons as you did with your EditText

    Button okBtn= (Button) dialog_layout.findViewById(R.id.btn_ok);
    Button cancelBtn = (Button) dialog_layout.findViewById(R.id.btn_cancel);
    

    then set you onClickListeners

    okBtn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // some code
        }
    });
    cancelBtn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // some code
        }
    });
    
    0 讨论(0)
  • 2020-12-22 07:48

    XML Code:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
          <Button
              android:id="@+id/dialogButtonCancell"
              android:layout_width="75dp"
              android:layout_height="75dp">
    
          <Button
              android:id="@+id/dialogButtonOK"
              android:layout_width="80dp"
              android:layout_height="70dp"/>
    
    </RelativeLayout>
    

    Java Code:

    // custom dialog
    final Dialog dialog = new Dialog(context);
    //name of xml - custom
    dialog.setContentView(R.layout.custom);
    
    // set the custom dialog components - text, image and button
    Button dialogButtonOK = (Button) dialog.findViewById(R.id.dialogButtonOK);
    Button dialogButtonCancell = (Button) dialog.findViewById(R.id.dialogButtonCancell);
    
      dialog.setTitle("Warning!!!!!");
    
    // if button is clicked, call some method..
    dialogButtonOk.setOnClickListener(new OnClickListener() {
    
        public void onClick(View v) {
        someMethod();
        }
    });
    
    dialogButtonCancell.setOnClickListener(new OnClickListener() {
       public void onClick(View v) {                            
       dialog.dismiss();                        
       }
    });
    dialog.show();
    
    0 讨论(0)
提交回复
热议问题