开发者

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

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, and onclick of this button, I want to start an activity. The popup window opens, but I don't understand how to handle the onclick of the imagebutton inside the popup window.

In main.xml, I have a button, and in popup_example.xml, I have an imagebutton.

My Java code is as follows:

final LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
final Button b=(Button)findViewById(R.id.btn);
b.setOnClickListener(new OnClickListener()
{
    public void onClick(View v)
    {
        PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_example,(ViewGroup)findViewById(R.layout.main)));
        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)findViewById(R.id.home);
        img.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                Intent.....
            }
        });

        //if onclick is written h开发者_StackOverflow社区ere it gives runtime exception.
    }); 

and I have two xml layouts.........

  1. main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"> 
    
        <ImageButton 
            android:id="@+id/btn"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:src="@drawable/ghj" />
    </LinearLayout>
    
  2. popup_example.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" 
        android:padding="10dip" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:background="#8E2323"> 
    
       <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
            android:orientation="vertical" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:padding="5px">
    
            <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
                android:orientation="vertical" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:padding="5px"
                android:background="#000000">
    
                <ImageButton android:id="@+id/home"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:focusable="true"
                     android:src="@drawable/vitalss"
                     android:layout_weight="1"
                     android:background="#8E2323"/>                 
            </TableLayout>
        </TableLayout>
    </LinearLayout> 
    


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.....
                }
        });


In main activity button onClick you can use:

    popupWindow.getContentView().findViewById(R.id.buttonInPopup).setOnClickListener(new OnClickListener(...)


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.....
            }
    });


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜