gpt4 book ai didi

android - 显示 PopupWindow 时无法单击父 Activity 按钮

转载 作者:行者123 更新时间:2023-11-29 18:04:52 25 4
gpt4 key购买 nike

在我的父 Activity 中我有一个按钮,当我点击它时显示带有 2 个 ImageButton 的 PopUpWindow .. 当这个 PopUpWindow 存在时'无法点击我的父 Activity Button .. 这是我的代码,它有什么问题吗..

public class PopUpExample extends Activity {

Button but;
LinearLayout mainLayout;
PopupWindow popUp;
boolean click = true;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mainLayout = (LinearLayout) findViewById(R.id.main_layout);
but = (Button) findViewById(R.id.main_btn);

but.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

View popView;

if(click){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popUp = new PopupWindow(inflater.inflate(R.layout.popup_example, null, false),100, 50, true);
popUp.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
popUp.update();
click = false;

popView = popUp.getContentView();

ImageButton call = (ImageButton) popView.findViewById(R.id.call_btn);

call.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Toast.makeText(PopUpExample.this, "Calling...", Toast.LENGTH_SHORT).show();
}
});

ImageButton sms = (ImageButton) popView.findViewById(R.id.sms_btn);

sms.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Toast.makeText(PopUpExample.this, "Sms...", Toast.LENGTH_SHORT).show();
}
});

}else{
popUp.dismiss();
click = true;
}

}
});
}
}

最佳答案

弹出 View 在创建时会从主视图中夺走焦点,因此用户无法单击主视图上的元素。
要单击主视图,需要先关闭 popview。
关于代码中的上述理论,您试图通过单击处于主要 Activity 中的按钮来关闭 popview,这是不可能的。

以下代码具有您需要合并到上述代码中的更改

public class PopUpExample extends Activity {    
Button but;
LinearLayout mainLayout;
PopupWindow popUp;
//boolean click = true;
View popView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mainLayout = (LinearLayout) findViewById(R.id.main_layout);
but = (Button) findViewById(R.id.main_btn);

but.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// if(click){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popUp = new PopupWindow(inflater.inflate(R.layout.popup_example, null, false),100, 50, true);
popUp.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
popUp.update();
//click = false;

popView = popUp.getContentView();

ImageButton call = (ImageButton)popView.findViewById(R.id.call_btn);

call.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Toast.makeText(MainActivity.this, "Calling...", Toast.LENGTH_SHORT).show();
popUp.dismiss();
}
});

ImageButton sms = (ImageButton)popView.findViewById(R.id.sms_btn);

sms.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Toast.makeText(MainActivity.this, "Sms...", Toast.LENGTH_SHORT).show();
popUp.dismiss();
}
});

//}else{
// popUp.dismiss();
// click = true;
// }

}
});
}
}

关于android - 显示 PopupWindow 时无法单击父 Activity 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13932139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com