2011-09-04 148 views
2

我在动态更改TextView中的PopupWindow中的文本时出现问题。我通过使用LayoutInflaterViewGroup引用PopupWindow中的TextView元素,但文本未更新。任何帮助将非常感谢! :)在PopupWindow中更改TextView的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(); 
     } 
    } 

回答

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

非常好,工作!非常感谢!! – BGM