是否有可能在Android中单击图像时显示Toast消息或弹出窗口()

上下观古今,起伏千万途。这篇文章主要讲述是否有可能在Android中单击图像时显示Toast消息或弹出窗口?相关的知识,希望能为你提供帮助。
我正在创建一个应用程序,在其中我想要在图像点击上添加消息或显示弹出窗口。
注意:我不想使用ImageButton,ImageView或Button显示Toast或popup。
请用代码回答
代码:
我希望事件发生在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"); } } }); }

答案是的,你可以这样做:
yourImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(GroupProfile.this, "Your Toast here", Toast.LENGTH_SHORT).show(); } });

另一答案您可以先使用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; } }

另一答案如果使用ImageView而不是android.media.image,则可以执行此操作。对于android.media.image,没有像OnClickListener()这样的方法......
【是否有可能在Android中单击图像时显示Toast消息或弹出窗口()】另一个解决方案是遵循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; } }


    推荐阅读