Button 按钮
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="http://schemas.android.com/apk/res/android"></selector>
设置Button背景
- 筹备shape文件
先筹备shape文件。这里筹备3个shape。别离代表3个不同的状态。
shape_btn_1_normal.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/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="http://schemas.android.com/apk/res/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="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#6D6D6D" /></shape>
- 新建selector文件
新建drawable文件bg_1.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/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" />
运行一下看看成果。按下按钮和没按下的时候,按钮的背景色彩是不同的。
selector介绍
StateListDrawable 是在 XML 文件中定义的可绘制对象,它会依据对象状态,应用多个不同的图像来示意同一个图形。
例如,Button 微件能够是多种不同状态(按下、聚焦或这两种状态都不是)中的其中一种,并且可利用状态列表可绘制对象,为每种状态提供不同的背景图片。 能够在 XML 文件中形容状态列表。每个图形由繁多 <selector>
元素内的 <item>
元素示意。每个 <item>
均应用各种属性来形容利用作可绘制对象的图形的状态。 在每个状态变更期间,将从上到下遍历状态列表,并应用第一个与以后状态匹配的我的项目 — 此抉择并非基于“最佳匹配”,而是抉择合乎状态最低条件的第一个我的项目。 selector指向的是StateListDrawable,这里咱们不关注StateListDrawable,只关注xml。
语法
参考如下语法:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize=["true" | "false"] android:dither=["true" | "false"] android:variablePadding=["true" | "false"] > <item android:drawable="@[package:]drawable/drawable_resource" android:state_pressed=["true" | "false"] android:state_focused=["true" | "false"] android:state_hovered=["true" | "false"] android:state_selected=["true" | "false"] android:state_checkable=["true" | "false"] android:state_checked=["true" | "false"] android:state_enabled=["true" | "false"] android:state_activated=["true" | "false"] android:state_window_focused=["true" | "false"] /></selector>
应用注意事项
咱们能够给 Button 设置背景,也能够给 TextView 设置同样的背景。
<TextView android:id="@+id/tv1" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="12dp" android:background="@drawable/bg_1" android:gravity="center" android:text="RFDev text view 1" />
bg_1
中设置了state_pressed
的。 如果TextView没有设置点击事件,用户点击或按着这个TextView是不会产生背景变动的。 给TextView设置点击事件后,再点击就能够看到背景变动了。
本文咱们用Button和TextView来做例子。实际上View的其它子类,比方ImageView,LinearLayout都能够用这种形式设置背景。
Android零根底入门教程视频参考