2011-09-04 37 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