更改 PopupWindow 中 TextView 的文本不起作用

发布于 2024-12-03 01:11:10 字数 1070 浏览 2 评论 0原文

我在 Android 的 PopupWindow 中动态更改 TextView 中的文本时遇到问题。我已使用 LayoutInflaterViewGroup 引用了 PopupWindow 内的 TextView 元素,但文本未更新。任何帮助将非常感激! :)

我的代码如下:

private void popupWindow() {
        try {
            LayoutInflater inflater = (LayoutInflater) SwingSpeed.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.popup_recording,
                    (ViewGroup) findViewById(R.id.popup_element));
            pw = new PopupWindow(inflater.inflate(R.layout.popup_recording,
                    null, false), 480, 800, true);
            pw.showAtLocation(findViewById(R.id.swingspeed), Gravity.CENTER, 0,
                    0);
            recording_text = (TextView) layout
                    .findViewById(R.id.recording_text);
            recording_text.setText("Recording"); //Update the TextView  
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

I'm having problems dynamically changing the text in a TextView in a PopupWindow in Android. I've referenced the TextView element inside the PopupWindow by using a LayoutInflater and ViewGroup but the text is not updating. Any help would be very much appreciated! :)

My code is as follows:

private void popupWindow() {
        try {
            LayoutInflater inflater = (LayoutInflater) SwingSpeed.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.popup_recording,
                    (ViewGroup) findViewById(R.id.popup_element));
            pw = new PopupWindow(inflater.inflate(R.layout.popup_recording,
                    null, false), 480, 800, true);
            pw.showAtLocation(findViewById(R.id.swingspeed), Gravity.CENTER, 0,
                    0);
            recording_text = (TextView) layout
                    .findViewById(R.id.recording_text);
            recording_text.setText("Recording"); //Update the TextView  
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掀纱窥君容 2024-12-10 01:11:10

问题是:您将布局膨胀两次,
而不是:

recording_text = (TextView) layout.findViewById(R.id.recording_text);

这样做:

recording_text = (TextView) pw.getContentView().
                 findViewById(R.id.recording_text);

而且,你根本不需要这一行:

View layout = inflater.inflate(R.layout.popup_recording,
                (ViewGroup) findViewById(R.id.popup_element));

The problem is: you are inflating the layout two times,
instead of:

recording_text = (TextView) layout.findViewById(R.id.recording_text);

do this:

recording_text = (TextView) pw.getContentView().
                 findViewById(R.id.recording_text);

and also, you don't need this line at all:

View layout = inflater.inflate(R.layout.popup_recording,
                (ViewGroup) findViewById(R.id.popup_element));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文