更改PopupWindow中TextView的文本无法正常工作

时间:2011-09-04 14:47:49

标签: java android textview popupwindow viewgroup

我在Android中的TextView中动态更改PopupWindow中的文字时出现问题。我使用TextViewPopupWindow引用了LayoutInflater中的ViewGroup元素,但文本未更新。任何帮助将非常感谢! :)

我的代码如下:

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();
        }
    }

1 个答案:

答案 0 :(得分:5)

问题是:你要两次给布局充气, 而不是:

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));