0

Android入门教程 | Button,TextView背景设置

 2 years ago
source link: https://segmentfault.com/a/1190000040847354
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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零基础入门教程视频参考


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK