0

我之前发送过这个问题,但我删除了它,因为我要粘贴更多代码。

我的问题是单击时出现 PopupUpWindow 并出现 2 个按钮。因此,这两个按钮具有 OnClick,但没有任何反应。我要粘贴我的代码:

        // PopupWindow de Exit

    Button exit=(Button) findViewById(R.id.button1);
    popUpView = getLayoutInflater().inflate(R.layout.estadisticaspopupwindowexit, null); 
    mpopup = new PopupWindow(popUpView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true);


    exit.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            System.out.println("no clicked");//For checking that it's ok
            mpopup.showAtLocation(popUpView, Gravity.BOTTOM, 0, 0);
            // UNTIL HERE IT'S OK
            View viewexit = (LinearLayout) factory.inflate(R.layout.estadisticaspopupwindowexit, null);
            Button si=(Button) viewexit.findViewById(R.id.buttonyes);
            Button no=(Button) viewexit.findViewById(R.id.buttonno);

            // THESE ARE BUTTONS CALLED FROM ANOTHER XML FILE

            si.setOnClickListener(new View.OnClickListener(){                   
                @Override
                public void onClick(View v) {
                    Intent intencion=new Intent(estadisticas.this, datosusuario.class);
                    startActivity(intencion);
                }


            });
            no.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v){
                    mpopup.dismiss();
                    System.out.println("no clicked"); 
// I'M WRITING THE LAST THING FOR CHECKING ON MY LOGCAST IF IT REALLY WORKS, BUT NOTHING HAPPENS

                }
            }); 


        }

    });

这就是一切。谢谢

4

2 回答 2

0

您可以使用AlertDialog.Builder获得相同的结果。这是显示带有两个按钮的弹出窗口的示例代码:

public class UIHelper {
   public static void createInformationalAlert(Context context,
        DialogInterface.OnClickListener positiveButtononClickListener,
        DialogInterface.OnClickListener negativeButtononClickListener,
        String content, String positiveButtonCaption,
        String negativeButtonCaption) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setMessage(content)
            .setPositiveButton(positiveButtonCaption,
                    positiveButtononClickListener)
            .setNegativeButton(negativeButtonCaption, negativeButtononClickListener);
    AlertDialog alertDialog = builder.create();
    alertDialog.show();
}
}

然后,要显示弹出窗口,请使用以下代码:

UIHelper.createInformationalAlert(this,
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }
            }, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }
            }, "Are you sure you want to exit?", "Yes", "No");

如果您希望为自定义视图充气,请使用setView(View)

于 2012-04-22T16:43:11.317 回答
0

也许你应该使用

Button si=(Button) popUpView.findViewById(R.id.buttonyes);
Button no=(Button) popUpView.findViewById(R.id.buttonno);
于 2012-04-22T16:22:43.947 回答