Java 更改PopupWindow中TextView的文本不工作
我在Android的弹出窗口中动态更改TextView中的文本时遇到问题。我使用LayoutFlater和ViewGroup引用了PopupWindow中的TextView元素,但文本没有更新。非常感谢您的帮助 我的代码如下:Java 更改PopupWindow中TextView的文本不工作,java,android,textview,popupwindow,viewgroup,Java,Android,Textview,Popupwindow,Viewgroup,我在Android的弹出窗口中动态更改TextView中的文本时遇到问题。我使用LayoutFlater和ViewGroup引用了PopupWindow中的TextView元素,但文本没有更新。非常感谢您的帮助 我的代码如下: private void popupWindow() { try { LayoutInflater inflater = (LayoutInflater) SwingSpeed.this .g
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));