how to handle onclick event of button inside popup window in android

前端 未结 4 1099
天命终不由人
天命终不由人 2021-01-02 11:01

In my application, I have a button initially on the screen, and in onclick of the button, a popup window should open. In the popup window, I have an imagebutton

相关标签:
4条回答
  • 2021-01-02 11:32

    This will not give error in Inflater and work properly:

        LayoutInflater layoutInflater = getLayoutInflater();
    
    
          View pview = layoutInflater.inflate(R.layout.popup_example,    (ViewGroup)findViewById(R.layout.main));
           PopupWindow pw = new PopupWindow(pview);
            pw.showAtLocation(v, Gravity.LEFT,0,0);
            pw.update(8,-70,150,270);
    
              //if onclick written here, it gives null pointer exception.
            ImageButton img=(ImageButton)pview.findViewById(R.id.home);
            img.setOnClickListener(new OnClickListener()
            {
                public void onClick(View v)
                {
                    Intent.....
                }
        });
    
    0 讨论(0)
  • 2021-01-02 11:33

    best solution :) Pass onCclickMethod from xml

    <ImageButton
                android:id="@+id/save_btn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                **android:onClick="onClick"**
                android:contentDescription="@string/save"
                android:src="@drawable/save" />
    

    it wroked from me..

    0 讨论(0)
  • 2021-01-02 11:35

    You have to find the button into the Popup view:

    View pview = inflater.inflate(R.layout.popup_example,(ViewGroup)findViewById(R.layout.main));
    PopupWindow pw = new PopupWindow(pview);
                pw.showAtLocation(v, Gravity.LEFT,0,0);
                pw.update(8,-70,150,270);
    
                  //if onclick written here, it gives null pointer exception.
                ImageButton img=(ImageButton)pview.findViewById(R.id.home);
                img.setOnClickListener(new OnClickListener()
                {
                    public void onClick(View v)
                    {
                        Intent.....
                    }
            });
    
    0 讨论(0)
  • 2021-01-02 11:40

    In main activity button onClick you can use:

        popupWindow.getContentView().findViewById(R.id.buttonInPopup).setOnClickListener(new OnClickListener(...)
    
    0 讨论(0)
提交回复
热议问题