1

我已经创建了一个按钮并在其上设置了 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();
        }


    });


    }

任何指导帮助和提示将不胜感激。

4

1 回答 1

0

嗯。. . . 看起来您没有为按钮放置 clickListener。

您是否将此代码放在 onCreate 上?

buttonStartQuiz.setOnClickListener(this);

或者

在下面的代码之前还要检查p2的值

if (p2 != null)

似乎您没有初始化变量。所以请检查一下。

如果没有那就放上来看看。如果仍然无法解决您的问题,请告诉我。

于 2012-12-12T04:00:40.820 回答