如何处理android中弹出窗口内按钮的onclick事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2997338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:13:07  来源:igfitidea点击:

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

android

提问by henna

In my application, I have a button initially on the screen, and in onclickof the button, a popup window should open. In the popup window, I have an imagebutton, and onclickof this button, I want to start an activity. The popup window opens, but I don't understand how to handle the onclickof the imagebutton inside the popup window.

在我的应用程序中,我最初在屏幕上有一个按钮,在onclick按钮中,应该会打开一个弹出窗口。在弹出窗口中,我有一个图像onclick按钮,在这个按钮中,我想开始一个活动。弹出窗口打开,但我不明白如何处理onclick弹出窗口内的图像按钮。

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

在 main.xml 中,我有一个按钮,而在 popup_example.xml 中,我有一个图像按钮。

My Java code is as follows:

我的Java代码如下:

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 here it gives runtime exception.
    }); 

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

我有两个 xml 布局........

  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> 
    
  1. 主文件

    <?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> 
    

回答by Francesco Laurita

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

回答by Atish Dabholkar

This will not give error in Inflater and work properly:

这不会在 Inflater 中出错并正常工作:

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

回答by CrazyJ36

In main activity button onClick you can use:

在主活动按钮 onClick 中,您可以使用:

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

回答by Basavaraj Walikar

best solution :) Pass onCclickMethod from xml

最佳解决方案:) 从 xml 传递 onCclickMethod

<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..

它来自我..