更改 PopupWindow 中 TextView 的文本不起作用
我在 Android 的 PopupWindow
中动态更改 TextView
中的文本时遇到问题。我已使用 LayoutInflater
和 ViewGroup
引用了 PopupWindow
内的 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();
}
}
I'm having problems dynamically changing the text in a TextView
in a PopupWindow
in Android. I've referenced the TextView
element inside the PopupWindow
by using a LayoutInflater
and ViewGroup
but the text is not updating. Any help would be very much appreciated! :)
My code is as follows:
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是:您将布局膨胀两次,
而不是:
这样做:
而且,你根本不需要这一行:
The problem is: you are inflating the layout two times,
instead of:
do this:
and also, you don't need this line at all: