在Android中点击图片是否可以显示Toast消息或弹出窗口?

时间:2019-04-22 08:07:03

标签: android popup toast

我正在创建一个应用程序,我想在其中单击“消息”上的消息或显示弹出窗口。

注意:我不想使用ImageButton,ImageView或Button显示Toast或弹出窗口。

请输入验证码

代码:

我想在myImage对象上发生事件

私人地图地图;

private ImageButton button;

private SupportMapFragment mapFragment = null;

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

    mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapfragment);
    boolean success = com.here.android.mpa.common.MapSettings.setIsolatedDiskCacheRootPath(
            getApplicationContext().getExternalFilesDir(null) + File.separator + ".here-maps",
            "");

    mapFragment.init(new OnEngineInitListener() {
        @Override
        public void onEngineInitializationCompleted(
                OnEngineInitListener.Error error) {
            if (error == OnEngineInitListener.Error.NONE) {
                // now the map is ready to be used
                map = mapFragment.getMap();

              //  map.setZoomLevel(12);
                // ...

                com.here.android.mpa.common.Image myImage =
                        new com.here.android.mpa.common.Image();



                button = new ImageButton(MainActivity.this);
                button.setImageResource(R.drawable.ic_action_name);

                try {
                    myImage.setImageResource(R.drawable.ic_action_name);
                } catch (IOException e) {
                    e.printStackTrace();
                }


                                    map.addMapOverlay(
                        new MapOverlay(button,
                                new GeoCoordinate(49.203076, -123.133849, 0.0)));


                // Set the map center to Vancouver, Canada.
                map.setCenter(new GeoCoordinate(49.203076, -123.133849), Map.Animation.NONE);



                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getApplicationContext(),"Harish",Toast.LENGTH_LONG).show();
                    }
                });

//创建自定义标记图像

            } else {
                System.out.println("ERROR: Cannot initialize SupportMapFragment");
            }
        }
    });
}

3 个答案:

答案 0 :(得分:0)

是的,您可以做到:

yourImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             Toast.makeText(GroupProfile.this, "Your Toast here", Toast.LENGTH_SHORT).show();
             }
        });

答案 1 :(得分:0)

您可以先使用onTouch事件检测是否触摸了图像,然后使用Toast方法显示消息。 看起来像这样。

public boolean onTouchEvent(MotionEvent event){
int action = event.getAction();
int x = event.getX()  
int y = event.getY();

switch(action){
case MotionEvent.ACTION_DOWN:
    if (x >= xOfYourBitmap && x < (xOfYourBitmap + yourBitmap.getWidth())
            && y >= yOfYourBitmap && y < (yOfYourBitmap + yourBitmap.getHeight())) {
        //tada, if this is true, you've started your click inside your bitmap
        Toast.makeText(getActivity(), "This is my Toast message!",Toast.LENGTH_LONG).show();

    }
    break;
}
}

答案 2 :(得分:0)

如果您使用ImageView而不是android.media.image,则可以执行此操作。没有像android.media.image那样的OnClickListener()方法。

另一种解决方案是遵循Chetan Mehara https://stackoverflow.com/users/1725131/chetan-mehra给出的答案,如下所示

public boolean onTouchEvent(MotionEvent event){
  int action = event.getAction();
  int x = event.getX()  
  int y = event.getY();

  switch(action){
   case MotionEvent.ACTION_DOWN:
      if (x >= xOfYourBitmap && x < (xOfYourBitmap + yourBitmap.getWidth())
        && y >= yOfYourBitmap && y < (yOfYourBitmap + yourBitmap.getHeight())) {
        //tada, if this is true, you've started your click inside your bitmap
         Toast.makeText(getActivity(), "This is my Toast message!",Toast.LENGTH_LONG).show();

      }
      break;
   }
}