app直播源码,ButtonTextView的背景设置
Button能对用户的点击行为作出反应。
在xml文件中放置一个button。
<Button
android:id="@+id/btn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/self_destruct" />
要想监听到 button 的点击事件,我们在 Activity 中进行设置。
public class MyActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.content_layout_id); final Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { // 这里是按钮点击事件,在主线程执行
}
});
}
}
上面我们用到了 View.OnClickListener
。button 被点击后会执行 onClick
方法。 系统会在App的主线程中执行 onClick
方法。我们可以在这里面更新UI。但不要做太耗时的操作。
我们注意到 OnClickListener 其实是 View 中的一个接口。setOnClickListener 也是View 的一个方法。 换句话说,就算我们这里用的不是 button,也可以用这样的方式来监听点击事件。 即 View.setOnClickListener(View.OnClickListener())
。
以后会遇到TextView,ImageView监听点击事件,或是整个Layout来监听点击事件。 这里使用的是监听器模式。
实际上,Button继承自TextView。
Button,TextView背景设置
如何给按钮增加动感?
Button 有按下(pressed)和未按下之分,我们可给这 2 种状态不同的背景颜色和文字颜色。本文要介绍的是 selector
,即状态列表。 和前面的 shape 类似, selector
也是一个xml文件。
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="
设置Button背景
- 准备shape文件
先准备shape文件。这里准备3个shape。分别代表3个不同的状态。
shape_btn_1_normal.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="
android:shape="rectangle">
<solid android:color="#6CB3DD" />
<corners android:radius="2dp" /></shape>
shape_btn_1_pressed.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="
android:shape="rectangle">
<solid android:color="#1E6283" />
<corners android:radius="4dp" /></shape>
shape_btn_disable.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="
android:shape="rectangle">
<solid android:color="#6D6D6D" /></shape>
- 新建selector文件
新建drawable文件 bg_1.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android=">
<item android:drawable="@drawable/shape_btn_disable" android:state_enabled="false" />
<item android:drawable="@drawable/shape_btn_1_normal" android:state_enabled="true" android:state_pressed="false" />
<item android:drawable="@drawable/shape_btn_1_pressed" android:state_enabled="true" android:state_pressed="true" /></selector>
- 设置Button背景
在layout中设置背景。
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_1"
android:text="RFDev btn 1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:background="@drawable/bg_1"
android:enabled="false"
android:text="RFDev btn 2" />
运行一下看看效果。按下按钮和没按下的时候,按钮的背景颜色是不同的。
以上就是 app直播源码,ButtonTextView的背景设置,更多内容欢迎关注之后的文章
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现