Android-Button和ImageButton

Android-Button和ImageButton

Button顾名思义,就是安卓设备的按钮组件,非常简单,比如都会有这样一些方法,设置按钮监听事件,以及它的选择器来设置按钮样式,还有Text按钮的名称,不过我觉得还是复习一下,另外还有一个我很少使用,那就是ImageButton。

区别
从代码上来看有以下区别:
ImageButton:有src方式进行引用,但是没用Button的text属性
Button:有text的属性,但是没用src的引用

实例:

<Button  android:id="@+id/id_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button"/>

 <ImageButton  android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" />

效果:
Android-Button和ImageButton_第1张图片
上面是Button组件,下面是ImageButton组件。
还有一点,ImageButton还有一个background属性。

<ImageButton  android:background="#ff66ffff" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" />

效果:
Android-Button和ImageButton_第2张图片
可以看到设置成了背景色。

设置点击事件

    // Button组件
    findViewById(R.id.id_btn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "Button", Toast.LENGTH_SHORT).show();
        }
    });

    //ImageButton组件
    findViewById(R.id.id_btnImage).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "ImageButton", Toast.LENGTH_SHORT).show();
        }
    });

记录一下

你可能感兴趣的:(Android-Button和ImageButton)