一、Button简介
Button可以说是在开发中最常见的控件之一,主要用于进行与用户的交互、响应用户需求。Android中的Button继承自TextView,拥有所有TextView所拥有的属性和方法。
二、Button的基本使用
我们一般在布局文件中直接创建Button控件,调整好布局,也可以在代码中通过New的方式创建Button,但是一般不推荐。
下面的代码,创建了三个垂直排列的按钮,设置了相同的样式。并未每个按钮设置了一个唯一的ID,此ID会在代码中获取按钮的时候使用到。
<LinearLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:orientation="vertical">
<Button
android:id="@+id/btnTest1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/btn_style"
android:text="测试点击按钮" />
<Button
android:id="@+id/btnTest2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/btn_style"
android:text="测试长按按钮" />
<Button
android:id="@+id/btnTest3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/btn_style"
android:text="onTouch按钮" />
</LinearLayout>
public class MainActivity02 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_02);
/*通过ID查找到对应的按钮*/
Button btnTest1 = findViewById(R.id.btnTest1);
Button btnTest2 = findViewById(R.id.btnTest2);
Button btnTest3 = findViewById(R.id.btnTest3);
btnTest1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("我点击了按钮一。。");
}
});
btnTest2.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
System.out.println("我长按了按钮二。。");
return false;
}
});
btnTest3.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
System.out.println("我touch了按钮三。。");
return false;
}
});
}
}
三、Button事件的处理
Button可以响应三种事件,点击、长按和触摸。
- 添加点击事件
btnTest1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("我点击了按钮一。。");
}
});
- 添加长按事件
btnTest2.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
System.out.println("我长按了按钮二。。");
return false;
}
});
- 添加触摸事件
btnTest3.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
System.out.println("我touch了按钮三。。");
return false;
}
});
触摸事件中,包含按下、松开、按下移动即拖拽三种情况,允许在不同的状态下处理不同的业务逻辑,比如:在做拖动某个控件时,onTouch事件特别有用。
btnTest3.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
System.out.println("我touch了按钮三。。");
int action = motionEvent.getAction();
if(action == MotionEvent.ACTION_UP) {
System.out.println("我按下了按钮三。。");
}
else if(action == MotionEvent.ACTION_DOWN) {
System.out.println("我松开了按钮三。。");
}
else if(action == MotionEvent.ACTION_MOVE) {
System.out.println("我按下并移动了按钮三。。");
}
return false;
}
});
四、动态改变Button的状态
通常情况下,我们需要根据按钮的不同状态改变按钮不同的样式,比如:按钮默认样式、按压时的样式、以及按钮选中时的样式等等。
在Android中我们也可以像设置CSS样式一样给按钮定义好不同状态的样式,而不需要再代码中进行修改。我们只需要给按钮添加android:background属性,引入一个事先定义好的drawable资源即可,drawable资源就相当于我们的CSS文件,按钮的样式就写在drawable资源中。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--按钮的默认状态,默认背景色为红色,圆角5dp-->
<!--此处按钮样式使用的直接定义图片样式,还可以引入drawable资源文件,使用android:drawable=""引入资源文件-->
<item android:state_window_focused="false">
<shape>
<solid android:color="#ff0000"></solid>
<corners android:radius="5dp"></corners>
</shape>
</item>
<!--按下时的按钮状态,按下时背景色变为蓝色,圆角5dp-->
<item android:state_pressed="true">
<shape>
<solid android:color="#0000ff"></solid>
<corners android:radius="5dp"></corners>
</shape>
</item>
<!--选中时的按钮状态,按下时背景色变为绿色,圆角5dp-->
<item android:state_selected="true">
<shape>
<solid android:color="#00ffff"></solid>
<corners android:radius="5dp"></corners>
</shape>
</item>
</selector>
需要注意的是:按钮默认状态和按下状态,系统会帮我们自己去判断,但选中状态,系统并不知道什么时候是选中,因此,若想设置成选中状态,需要手动调用按钮的setSeclect()函数。
原创不易,点个赞再走呗。。。