如何自动打开弹出式图像视图

How to automatically open a popup imageview

我现在有这个代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_serviciosp);

    final Button btnOpenPopup = (Button)findViewById(R.id.openpopup);
        btnOpenPopup.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                LayoutInflater layoutInflater
                        = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                View popupView = layoutInflater.inflate(R.layout.popup, null);
                final PopupWindow popupWindow = new PopupWindow(
                        popupView,
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);

                Button btnDismiss = (Button) popupView.findViewById(R.id.dismiss);
                btnDismiss.setOnClickListener(new Button.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        popupWindow.dismiss();
                    }
                });

                popupWindow.showAsDropDown(btnOpenPopup, 50, -30);

            }
        });

    CustomList adapter = new CustomList(Agro.this, web, desc, tel, imageId, imageId3);
    list = (ListView) findViewById(R.id.lvlista);
    list.setAdapter(adapter);

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(Agro.this, "Usted Clico en " + web[+position], Toast.LENGTH_SHORT).show();

            if (position == 0) {

                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:22222222222222"));
                startActivity(callIntent);
            }


        }
    });
}

它的作用是在我的列表视图上方显示一个按钮,当我按下按钮时它会显示弹出窗口,但我想做的是当我加入列表后等待 3 或 4 秒时它会自动弹出无需按下按钮。

我还想知道如何随机化自动弹出窗口,所以它最终会弹出我们,而不是每次我进入布局时都会弹出。

谢谢

要在 4 秒后打开弹出窗口,您可以在 onCreate 中使用以下代码

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            btnOpenPopup.performClick();
         }, 4000);

它的作用是在启动 activity 4 秒后模拟按钮点击。

如果您不想在用户已单击按钮时自动弹出,您可以使用全局布尔值 - userClicked。 Uf 用户点击将布尔值设置为 true,否则最初将其设置为 false。在这种情况下,上述方法可以更改为

        new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            if(!userClicked)
              btnOpenPopup.performClick();
         }, 4000);

对于随机化,只需使用随机生成器并获得 1 到 5 之间的整数。如果整数为 1,则显示,否则不显示。你可以按照下面的方式做。

    Random random = new Random();
     int x= random.nextInt(5)+1;

AND 在处理程序方法中,您的 if 语句应更改为

      if(!userClicked && x==1)
            btnOpenPopup.performClick();

所以您的代码将更改为

   prvate boolean userClicked = false;

     @Override
 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_serviciosp);

final Button btnOpenPopup = (Button)findViewById(R.id.openpopup);
    btnOpenPopup.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            userClicked=true;
            LayoutInflater layoutInflater
                    = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupView = layoutInflater.inflate(R.layout.popup, null);
            final PopupWindow popupWindow = new PopupWindow(
                    popupView,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);

            Button btnDismiss = (Button) popupView.findViewById(R.id.dismiss);
            btnDismiss.setOnClickListener(new Button.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    popupWindow.dismiss();
                }
            });

            popupWindow.showAsDropDown(btnOpenPopup, 50, -30);

        }
    });

     new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
           Random random = new Random();
           int x= random.nextInt(5)+1;
            if(!userClicked && x==1)
              btnOpenPopup.performClick();
         }}, 4000);

    ........