android - 如何从弹出窗口获取输入

标签 android popupwindow layout-inflater

//create inflater
final LayoutInflater inflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//create popupwindow
    PopupWindow pw=new PopupWindow(inflater.inflate(R.layout.menu, (ViewGroup)findViewById(R.layout.dictionarylist)));

        Button Menu = (Button) findViewById(R.id.Menu);
        Menu.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                pw.showAtLocation(v, Gravity.CENTER, 0, 0);
                pw.update(0, 0, 200, 250);
                pw.setOutsideTouchable(false);
            }
        });

我想要的是在单击父 Activity 中的按钮时显示弹出窗口。弹出窗口有按钮,当点击按钮时它会执行一些功能。

enter image description here

最佳答案

您必须找到按钮的 View ,然后像这样为其分配监听器:

View pview=inflater.inflate(R.layout.menu, (ViewGroup)findViewById(R.layout.dictionarylist));

Button Menu = (Button) pview.findViewById(R.id.Menu);

Menu.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                pw.showAtLocation(v, Gravity.CENTER, 0, 0);
                pw.update(0, 0, 200, 250);
                pw.setOutsideTouchable(false);
            }

如果你还没有像这样初始化你的充气机:

Inflator inflator = LayoutInflater.from(this);

关于android - 如何从弹出窗口获取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13217152/

相关文章:

android - 在服务中捕获屏幕上的任何触摸

android - 如何更改 drawableLeft 图标的大小?

ios - 呈现具有自定义大小的 UIViewController

java - 在image viewpager中的每个图像下添加文字描述

java - Android:扩展 LinearLayout 的自定义复合类认为它是 LinearLayout

java - 找不到 "avoid passing null as the view root"的父项

android - 如何在 Recyclerview 中使用 Spinner?

android - 每次插入一个整数时,如何计算我在 EditText 中插入的整数的总和?

android - 如何在 Android 的 PopupWindow 中设置 ListView 的宽度?

Javascript 将值从弹出窗口传递到其父窗口