我已经创建了一个按钮并在其上设置了 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();
}
});
}
任何指导帮助和提示将不胜感激。