How to create image button in android?

后端 未结 3 937
栀梦
栀梦 2021-02-19 19:18

So I\'m new to android development...How can I create an image that acts like button, so when I press that image, image starts a specific activity. So I want this to show as ima

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-19 19:43

    With the ImageView element, attaching a click listener to it.

    The XML:

    
    

    The code:

    ImageView imageView = (ImageView) findViewById(R.id.myImageView);
    imageView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(ThisActivity.this, MyOtherActivity.class);
            startActivity(intent);
        }
    });
    

    You also could use a ImageButton (in the same way). It makes almost no difference. You can see more details here. Difference between a clickable ImageView and ImageButton

提交回复
热议问题