我已经创建了一个按钮并在其上设置了onClicklistener,但是我无法打开弹出窗口。我编写了它的xml和java代码,可以在下面看到。当我点击它时它点击但没有做任何事情。
弹出窗口的xml代码
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:id = "@+id/popup"
android:orientation = "vertical">
<TextView
android:id = "@+id/popUpTitle"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "@string/popup" />
<TextView
android:id = "@+id/popUpContent"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "@string/popcontent"
android:layout_marginBottom="47dp" />
<Button
android:id="@+id/popUpButtonClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/close" />
用于单击时创建弹出窗口的按钮的java代码
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId())
{
case R.id.buttonCreate:
Intent i = new Intent(CreateQuestions.this, UploadQuestions.class);
startActivity(i);
break;
case R.id.buttonStartQuiz:
if (p2 != null)
{
showPopup(CreateQuestions.this, p2);
}
}
创建弹出窗口的方法
//method that displays the pop up
}
private void showPopup(final CreateQuestions createQuestions1, Point p2) {
// TODO Auto-generated method stub
int popupWidth = 200;
int popupHeight = 150;
LinearLayout viewGroup = (LinearLayout) createQuestions1.findViewById (R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) createQuestions1.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup, viewGroup);
//method to create the popup
final PopupWindow popupWindow = new PopupWindow(createQuestions1);
popupWindow.setContentView(layout);
popupWindow.setWidth(popupWidth);
popupWindow.setHeight(popupHeight);
popupWindow.setFocusable(true);
//some offseta to align the pop up relative to the button's positions
int OFFSET_X = 30;
int OFFSET_Y = 30;
//clear the default background
popupWindow.setBackgroundDrawable(new BitmapDrawable());
//displaying the pop up at the specified location and offsets
popupWindow.showAtLocation(layout, Gravity.NO_GRAVITY, p2.x + OFFSET_X, p2.y + OFFSET_Y);
//getting the reference to Close button and close the popup when clicked.
close = (Button) layout.findViewById(R.id.popUpButtonClose);
close.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}
});
}
非常感谢任何指导帮助和提示。
答案 0 :(得分:0)
hummmm。 。 。 。 看起来你没有把clickListener放在按钮上。
您是否已将此代码放在onCreate上?
buttonStartQuiz.setOnClickListener(this);
或
在下面的代码
之前检查 p2 的值if (p2 != null)
好像你没有初始化变量。所以请检查一下。
如果没有,那就把它看看。如果仍然没有解决您的问题,请告诉我。