Android - 弹出窗口 window 未关闭
Android - Popup window is not closing
我想在单击按钮时关闭弹出窗口 window,但关闭功能似乎不起作用并且 window 没有关闭。我做错了什么?
(我是初学者,所以代码可能是'weird'。请理解...)
public class AlarmPopup extends Activity {
private PopupWindow popup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onShowPopup();
}
public void onShowPopup(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.alarm_popup, null, false);
final PopupWindow popup = new PopupWindow(view, 400, 300, true);
setContentView(R.layout.alarm_popup);
view.findViewById(R.id.button).post(new Runnable() {
@Override
public void run() {
popup.showAtLocation(view, Gravity.CENTER, 0, 0);
}
});
findViewById(R.id.button).setOnClickListener(mClickListener);
}
Button.OnClickListener mClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) { // dismiss and stop the alarm function on other class
Intent i = new Intent(AlarmPopup.this, AlarmService.class);
stopService(i); // this function is working...
popup.dismiss();
}
};
}
您已将弹出窗口声明为全局弹出窗口,并在您的 onShowPopup 内部为弹出窗口创建新对象,这样本地弹出窗口将永远无法从侦听器访问,因此请进行如下更改:
public void onShowPopup(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.alarm_popup, null, false);
popup = new PopupWindow(view, 400, 300, true);
setContentView(R.layout.alarm_popup);
view.findViewById(R.id.button).post(new Runnable() {
@Override
public void run() {
popup.showAtLocation(view, Gravity.CENTER, 0, 0);
}
});
view.findViewById(R.id.button).setOnClickListener(mClickListener);
}
您用来关闭弹出窗口的弹出变量 window 尚未在您发布的代码中初始化。您在方法内部创建的最终变量是本地变量,无法在该方法外部访问。
所以初始化你的变量或者在方法中使用相同的变量。
我想在单击按钮时关闭弹出窗口 window,但关闭功能似乎不起作用并且 window 没有关闭。我做错了什么?
(我是初学者,所以代码可能是'weird'。请理解...)
public class AlarmPopup extends Activity {
private PopupWindow popup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onShowPopup();
}
public void onShowPopup(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.alarm_popup, null, false);
final PopupWindow popup = new PopupWindow(view, 400, 300, true);
setContentView(R.layout.alarm_popup);
view.findViewById(R.id.button).post(new Runnable() {
@Override
public void run() {
popup.showAtLocation(view, Gravity.CENTER, 0, 0);
}
});
findViewById(R.id.button).setOnClickListener(mClickListener);
}
Button.OnClickListener mClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) { // dismiss and stop the alarm function on other class
Intent i = new Intent(AlarmPopup.this, AlarmService.class);
stopService(i); // this function is working...
popup.dismiss();
}
};
}
您已将弹出窗口声明为全局弹出窗口,并在您的 onShowPopup 内部为弹出窗口创建新对象,这样本地弹出窗口将永远无法从侦听器访问,因此请进行如下更改:
public void onShowPopup(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.alarm_popup, null, false);
popup = new PopupWindow(view, 400, 300, true);
setContentView(R.layout.alarm_popup);
view.findViewById(R.id.button).post(new Runnable() {
@Override
public void run() {
popup.showAtLocation(view, Gravity.CENTER, 0, 0);
}
});
view.findViewById(R.id.button).setOnClickListener(mClickListener);
}
您用来关闭弹出窗口的弹出变量 window 尚未在您发布的代码中初始化。您在方法内部创建的最终变量是本地变量,无法在该方法外部访问。 所以初始化你的变量或者在方法中使用相同的变量。