【问题标题】:Change text of TextView in PopupWindow not working在 PopupWindow 中更改 TextView 的文本不起作用
【发布时间】:2011-09-04 14:47:49
【问题描述】:

我在 Android 中动态更改 TextViewPopupWindow 中的文本时遇到问题。我通过使用LayoutInflaterViewGroupPopupWindow 中引用了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();
        }
    }

【问题讨论】:

    标签: java android textview popupwindow viewgroup


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-06-03
      • 1970-01-01
      • 2016-03-02
      • 2014-01-03
      • 1970-01-01
      • 2021-05-05
      • 2014-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多