如何处理按钮的onclick事件中弹出的安卓窗口 [英] how to handle onclick event of button inside popup window in android

查看:116
本文介绍了如何处理按钮的onclick事件中弹出的安卓窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我最初屏幕上的按钮,并在的onclick 按钮,弹出一个窗口应该打开。在弹出的窗口中,我有一个ImageButton的,而这个按钮的onclick ,我要开始一个活动。弹出窗口打开,但我不知道如何处理的的onclick 在弹出的窗口内的ImageButton。

在main.xml中,我有一个按钮,并在popup_example.xml,我有一个ImageButton的。

我的Java code是如下:

 最后LayoutInflater吹气=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
最后键b =(按钮)findViewById(R.id.btn);
b.setOnClickListener(新OnClickListener()
{
    公共无效的onClick(视图v)
    {
        PopupWindow PW =新PopupWindow(inflater.inflate(R.layout.popup_example,(ViewGroup中)findViewById(R.layout.main)));
        pw.showAtLocation(ⅴ,Gravity.LEFT,0,0);
        pw.update(8,-70,150,270);

        //如果的onclick写在这里,它给空指针异常。
        的ImageButton IMG =(的ImageButton)findViewById(R.id.home);
        img.setOnClickListener(新OnClickListener()
        {
            公共无效的onClick(视图v)
            {
                意图.....
            }
        });

        //如果的onclick这里写它使运行时异常。
    });
 

和我有两个XML布局.........

  1. main.xml中

     < XML版本=1.0编码=UTF-8&GT?;
    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:方向=垂直
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT>
    
        <的ImageButton
            机器人:ID =@ + ID / BTN。
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:SRC =@可绘制/ GHJ/>
    < / LinearLayout中>
     

  2. popup_example.xml

     < XML版本=1.0编码=UTF-8&GT?;
    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:方向=垂直
        机器人:填充=10dip
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:后台=#8E2323>
    
       < TableLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:方向=垂直
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:填充=5px的>
    
            < TableLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
                机器人:方向=垂直
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:填充=5像素
                机器人:后台=#000000>
    
                < ImageButton的机器人:ID =@ + ID /家
                     机器人:layout_width =WRAP_CONTENT
                     机器人:layout_height =WRAP_CONTENT
                     机器人:可聚焦=真
                     机器人:SRC =@可绘制/ vitalss
                     机器人:layout_weight =1
                     机器人:后台=#8E2323/>
            < / TableLayout>
        < / TableLayout>
    < / LinearLayout中>
     

解决方案

您必须找到该按钮弹出的看法:

 查看PVIEW = inflater.inflate(R.layout.popup_example,(ViewGroup中)findViewById(R.layout.main));
PopupWindow PW =新PopupWindow(PVIEW);
            pw.showAtLocation(ⅴ,Gravity.LEFT,0,0);
            pw.update(8,-70,150,270);

              //如果的onclick写在这里,它给空指针异常。
            的ImageButton IMG =(的ImageButton)pview.findViewById(R.id.home);
            img.setOnClickListener(新OnClickListener()
            {
                公共无效的onClick(视图v)
                {
                    意图.....
                }
        });
 

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

这篇关于如何处理按钮的onclick事件中弹出的安卓窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆