android學習之旅(7)---button

Button

Button繼承了TextView組件,它主要是在UI界面上生成一個按鈕,該按鈕可以供用戶點擊,當用戶點擊Button時,會自動觸發onClick事件。下面是Button的一些常見的用法:

(1)普通按鈕
(2)圓角按鈕
(3)描邊按鈕
(4)按壓變色按鈕
(5)按鈕點擊事件

普通按鈕

由於Button繼承於TextView所以,TextView的屬性也適用於Button。

示例程序1

    <!--佈局使用相對佈局-->
    <Button
        android:id="@+id/btn_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FF2600"
        android:text="ButtonOne"
        android:textColor="#00ff00"
        android:textSize="30sp"/>

圓角按鈕

如果需要給按鈕添加樣式,主要是通過修改android:background屬性來實現。

  • (1)在res/drawable下添加XML文件。eg:botton_two_style.xml
  • (2)選擇根元素爲:shape
  • (3)在XML中選擇感興趣的屬性進行修改。
  • (4)通過android:background來使用該xml文件

botton_two_style.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--設置圓角的半徑爲5dp-->
    <corners android:radius="5dp"></corners>
    <!--設置組件的填充顏色-->
    <solid android:color="#ff00ff"></solid>
</shape>

示例程序2

    <Button
        android:id="@+id/btn_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn_one"
        android:layout_marginTop="10dp"
        android:background="@drawable/button_two_style"
        android:text="ButtonTwo"
        android:textColor="#000000"
        android:textSize="30sp"/>

描邊按鈕

shape中有6個基本子元素:corners(圓角)、gradient(漸變)、padding(內邊距)、size(尺寸)、solid(填充)、stroke(描邊)。剛纔的示例程序中展示corners的功能,這個列子將展示描邊的功能,步驟和剛纔是一樣的。

button_three_style.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="5dp"/>
    <stroke android:width="1dp"
            android:color="#00FFD9"/>
</shape>

示例程序3

    <Button
        android:id="@+id/btn_Three"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/button_three_style"
        android:layout_below="@id/btn_two"
        android:onClick="showToast"
        android:textSize="30sp"
        android:textColor="#2196F3"
        android:text="ButtonThree"/>

這裏推薦一篇文章:簡書:Android shape屬性大全

按壓變色按鈕

按壓變色這個功能在我們平時的使用會經常使用到,那它是如何實現的?利用selector選擇器我們可以是實現這個功能。

button_four_style.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--沒有被按壓的效果-->
    <item android:state_pressed="false">
        <shape>
            <corners android:radius="5dp"/>
            <solid android:color="#0099FF"/>
        </shape>
    </item>
    <!--按壓效果-->
    <item android:state_pressed="true">
        <shape>
            <corners android:radius="5dp"/>
            <solid android:color="#3F51B5"/>
        </shape>
    </item>
</selector>

示例程序4

    <Button
        android:id="@+id/btn_four"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/button_four_style"
        android:layout_below="@id/btn_Three"
        android:text="ButtonFour"
        android:textSize="30sp"/>

按鈕點擊事件

給按鍵添加點擊事件兩種方法:

(1)onClick

通過onClick屬性可以爲按鈕添加一個點擊事件。

  • (1)在onclick中指定點擊事件觸發方法的方法名。
  • (2)在對應的.java文件中實現該方法。

需要注意的是:方法的形式必須爲:public void 方法名(View),方法有一個類型爲View的參數。示例程序參照:示例程序3

(2) 在.java中爲對應按鈕設置對應的點擊事件
  • (1)通過findViewById方法找到對應的按鈕
  • (2)通過setOnClickListener方法設置點擊事件
  • (3)重寫View.OnClickListeneronClick方法

示例程序參照:示例程序4

.java

    private Button m_ButtonFour;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_test);
        m_ButtonFour=findViewById(R.id.btn_four);
        m_ButtonFour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //獲取按鈕文本內容
                String str=((Button) v).getText().toString();
                str+="被點擊";
                Toast.makeText(ButtonTestActivity.this,str,Toast.LENGTH_SHORT).show();
            }
        });
    }
    public void showToast(View view)
    {
        String str=((Button) view).getText().toString();
        str+="被點擊";
        Toast.makeText(ButtonTestActivity.this,str,Toast.LENGTH_SHORT).show();
    }

運行效果

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-KKLEkkox-1575622216314)(./button運行效果.png)]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章