gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:27:01 24 4
gpt4 key购买 nike

在我的应用程序中,我最初在屏幕上有一个按钮,在按钮的 onclick 中,应该打开一个弹出窗口。在弹出窗口中,我有一个图像按钮,这个按钮的onclick,我想开始一个 Activity 。弹出窗口打开,但我不明白如何处理弹出窗口内图像按钮的 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.
});

我有两个 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>

最佳答案

您必须在弹出 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.....
}
});

关于android - 如何处理android中弹出窗口内按钮的onclick事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2997338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com