一、 概念
- Button 控件继承 TextView ,拥有 TextView 的属性
- StateListDrawable 是Drawable资源的一种,可以根据不同的状态,设置不同的图片效果,关键节点 < selector > ,我们只需要将Button的 background 属性设置为该drawable资源即可轻松实现,按下 按钮时不同的按钮颜色或背景!
二、 属性
三、案例1
- 按钮状态改变
- 按钮监听
- btn_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/white" android:state_pressed="true" />
<item android:drawable="@color/black" android:state_enabled="false" />
<item android:drawable="@color/purple_500" />
</selector>
- activity_btn.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="50dp">
<Button
android:id="@+id/btnOne"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="@drawable/btn_bg"
android:text="按钮"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold"/>
<Button
android:id="@+id/btnTwo"
android:layout_width="match_parent"
android:layout_height="64dp"
android:text="按钮不可用"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold"/>
</LinearLayout>
- Activity_Btn.java
package com.project;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity_Btn extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_btn);
setupBtn();
}
private void setupBtn(){
Button btnOne = (Button) findViewById(R.id.btnOne);
Button btnTwo = (Button) findViewById(R.id.btnTwo);
btnTwo.setOnClickListener(new View.OnClickListener() {
// 按钮绑定点击 事件
@Override
public void onClick(View v) {
if (btnTwo.getText().toString().equals("按钮不可用")) {
btnOne.setEnabled(false);
btnTwo.setText("按钮可用");
} else {
btnOne.setEnabled(true);
btnTwo.setText("按钮不可用");
}
}
});
}
}
四、注册点击事件的方式
- 自定义内部类
- 匿名内部类
- 当前activity去实现事件接口
- 在布局文件中添加点击事件属性
-
效果图
-
ui_button.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="通过自定义内部类实现点击事件" />
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="通过匿名内部类实现点击事件" />
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="通过当前Activity去实现点击事件接口" />
<Button
android:id="@+id/btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="在xml文件中绑定"
android:onClick="myClick" />
<Button
android:id="@+id/btn5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="在xml文件中绑定2"
android:onClick="myClick" />
</LinearLayout>
- ActivetyButton.java
package com.project.uikit;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.project.R;
public class UI_button2 extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ui_button);
//1.获取按钮
Button btn1 = findViewById(R.id.btn1);
//点击事件:被点击时被触发的事件
MyClickListener mcl = new MyClickListener();
btn1.setOnClickListener(mcl);
//2.为按钮注册点击事件监听器
Button btn2 = findViewById(R.id.btn2);
//匿名内部类适用于有唯一操作的按钮
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//在控制台输出
Log.e("TAG","==========匿名内部类==========");
}
});
//3.通过当前的activity实现onClick方法
// 需要引用 View.OnClickListener
Button btn3 = findViewById(R.id.btn3);
btn3.setOnClickListener(this);
}
//1.自定义内部类实现了点击
class MyClickListener implements View.OnClickListener{
@Override
public void onClick(View view) {
//在控制台输出一条语句
Log.e("TAG","刚刚点击的按钮时注册了内部类监听器对象的按钮");
}
}
// 3.当前类中实现
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn3){
Log.e("TAG","用本类实现了OnClickListener");
}
}
// 自己响应 参数:被点击的控件对象
public void myClick(View v){
switch (v.getId()){
case R.id.btn4:
Log.e("TAG","btn4======");
break;
case R.id.btn5:
Log.e("TAG","btn5======");
break;
}
}
}