项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

示例如下:

/view/button/ImageButtonDemo1.java

/**
 * ImageButton - 图片按钮控件(继承自 ImageView)
 *     setClickable(boolean clickable) - 指定按钮是否可单击(如果要指定为 false 的话,需要在 setOnClickListener() 方法之后指定才会生效)
 *     setOnClickListener(OnClickListener l) - 指定单击事件的回调
 *
 *
 * ImageButton 和 Button 差不多(就是基类不一样,一个是 TextView 一个是 ImageView),详见 Button 的介绍吧
 */

package com.webabcd.androiddemo.view.button;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import com.webabcd.androiddemo.R;

public class ImageButtonDemo1 extends AppCompatActivity {

    private ImageButton _imageButton1;

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

        _imageButton1 = (ImageButton) findViewById(R.id.imageButton1);

        sample();
    }

    private void sample() {
        _imageButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ImageButtonDemo1.this, "imageButton1 clicked", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

/layout/activity_view_button_imagebuttondemo1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!--
        ImageButton - 图片按钮控件(继承自 ImageView)
    -->

    <ImageButton
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img_sample_son"/>

</LinearLayout>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

相关文章:

  • 2021-10-11
  • 2021-06-11
  • 2021-12-04
  • 2022-12-23
  • 2021-12-29
  • 2022-12-23
  • 2022-01-24
  • 2021-11-25
猜你喜欢
  • 2021-09-23
  • 2022-12-23
  • 2022-01-08
  • 2022-12-23
  • 2022-12-23
  • 2021-06-16
  • 2022-12-23
相关资源
相似解决方案