小编hen*_*nna的帖子

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

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

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

我的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.
    }); 
Run Code Online (Sandbox Code Playgroud)

我有两个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" …
    Run Code Online (Sandbox Code Playgroud)

android

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

android ×1