在PopupWindow中更改TextView的TextView不起作用
问题描述:
我在动态更改TextView
中的PopupWindow
中的文本时出现问题。我通过使用LayoutInflater
和ViewGroup
引用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();
}
}
答
的问题是:你正在膨胀的布局两次的 而是:
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));
非常好,工作!非常感谢!! – BGM