button 继承了 TextView
button分成3层:
最上面:前景色 foreground
中间:文字
最下面:背景色 background
一 图片选择器
案例功能: 按钮 随着点击 切换 背景图片
step1 导两张图片
app\src\main\res\drawable\--new--vector asset -- 点 clip art--选一个图片 --next-- finish
drawable下就会多一个图片的xml 文件
ic_brightness_5_black_24dp.xml
ic_insert_emoticon_black_24dp.xml
step2 新建drawable文件
app\src\main\res\drawable\下新建drawable resource file: btn_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 按下的时候显示-->
<item android:drawable="@drawable/ic_brightness_5_black_24dp" android:state_pressed="true"/>
<!-- 默认的情况显示-->
<item android:drawable="@drawable/ic_insert_emoticon_black_24dp"/>
</selector>
step3 背景颜色关联上 drawable 文件
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:text="我是按钮"
android:background="@drawable/btn_selector"
android:layout_width="200dp"
android:layout_height="100dp"/>
</LinearLayout>
效果图


二 颜色选择器
案例功能: 在上个案例的基础上,按钮随着点击切换背景颜色
step1 新建color 文件
res 下新建文件夹 color -- color文件夹 -- 右键 -- new -- color resource file --btn_color_selectot.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorAccent" android:state_pressed="true"/>
<item android:color="@color/colorPrimary" />
</selector>
step2 activity_main.xml
Button 添加这个属性 backgroundTint (背景色调) , 关联上color 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:text="我是按钮"
android:background="@drawable/btn_selector"
android:backgroundTint="@color/btn_color_selectot"
android:layout_width="200dp"
android:layout_height="100dp"/>
</LinearLayout>
效果图


三 button 事件处理
1 点击事件 | 长按事件 | 触摸事件
step1 activity_main.xml :设置 button 的 id
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/mbtn"
android:text="我是按钮"
android:background="@drawable/btn_selector"
android:backgroundTint="@color/btn_color_selectot"
android:layout_width="200dp"
android:layout_height="100dp"/>
</LinearLayout>
step2 MainActivity.java
package com.example.mybutton;
import androidx.appcompat.app.AppCompatActivity;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "cs";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.mbtn);
//点击事件
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//打印
Log.e(TAG,"OnClick");
}
});
//长按事件
btn.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Log.e(TAG,"OnLongClick");
return false;
}
});
//触摸事件
btn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//event.getAction()
//OnTouch 分为3种 0代表摸下 2 摸完不松手移动 1代表摸完松手
Log.e(TAG,"OnTouch"+event.getAction());
return false;
}
});
}
}
测试结果

OnTouch 分为3种
0代表摸下 2摸完不松手移动 1代表摸完松手

2 消费事件
修改上述代码的 MainActivity.java 里的 事件返回值,会形成消费事件
示例1
onTouch 返回值 true
onLongClick 返回值 false
onClick 无返回值
第一步走 onTouch, 返回值 true 结束 (表示这个onTouch 事件被消费了)
另两个事件 onLongClick 和 onClick 就不会被触发

示例2
onTouch 返回值 false
onLongClick 返回值 true
onClick 无返回值
长按的时候,第一步走 onTouch ,返回值 false ,继续走 onLongClick ,返回值 true ,结束
就不执行onClick事件了

示例3
onTouch 返回值 false
onLongClick 返回值 true
onClick 无返回值
短按的时候,第一步走 onTouch事件,返回值 false ,继续走 onClick事件
(注意是 短按,走不到 onLongClick)

示例4
onTouch 返回值 true
onLongClick 返回值 true
onClick 无返回值
第一步走 onTouch事件,返回值 true ,结束

示例5
最后再回过头看一下最开始 不消费 的时候
onTouch 返回值 false
onLongClick 返回值 false
onClick 无返回值
长按的时候,第一步走 onTouch ,返回值 false ,继续走 onLongClick ,返回值 false ,
继续走 onClick

3 onClick的另一种写法
step1 在 activity_main.xml 添加一个onClick属性,起一个名字
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/mbtn"
android:text="我是按钮"
android:background="@drawable/btn_selector"
android:backgroundTint="@color/btn_color_selectot"
android:onClick="heiClick"
android:layout_width="200dp"
android:layout_height="100dp"/>
</LinearLayout>
step2 选中名字,alt + enter 快捷键,在java里创建一个 该Click方法,注销原来的OnClick 方法

MainActivity.java
package com.example.mybutton;
import androidx.appcompat.app.AppCompatActivity;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "cs";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.mbtn);
//点击事件
// btn.setOnClickListener(new View.OnClickListener(){
// @Override
// public void onClick(View v) {
// //打印
// Log.e(TAG,"OnClick");
// }
// });
//长按事件
btn.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Log.e(TAG,"OnLongClick");
return false;
}
});
//触摸事件
btn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//event.getAction()
//OnTouch 分为3种 0代表摸下 2 摸完不松手移动 1代表摸完松手
Log.e(TAG,"OnTouch"+event.getAction());
return false;
}
});
}
public void heiClick(View view) {
Log.e(TAG,"heiClick");
}
}
执行结果

备注: 如果保留 onClick 和 heiClick 两个方法,那么,会执行 onClick,不会执行 heiClick
本文介绍了如何通过drawable资源文件实现按钮背景图片和颜色的动态切换,包括图片选择器和颜色选择器的应用,以及事件处理的详细步骤。通过实例演示,展示了不同事件处理策略对按钮行为的影响。

1242

被折叠的 条评论
为什么被折叠?



