Android Button的基本使用



title: Android Button的基本使用
tags: Button,按鈕

Button介紹:

Button(按鈕)繼承自TextView,在Android開發中,Button是常用的控件,用起來也很簡單,你可以在界面xml描述文檔中定義,也可以在程序中創建后加入到界面中,其效果都是一樣的。不過最好是在xml文檔中定義,因為一旦界面要改變是話,直接修改一下xml就行了,不用修改Java程序,並且在xml中定義層次分明,一目了然。

Button 支持的 XML 屬性及相關方法

XML 屬性 相關方法 說明
android:clickable setClickable(boolean clickable) 設置是否允許點擊。
clickable=true:允許點擊
clickable=false:禁止點擊
android:background setBackgroundResource(int resid) 通過資源文件設置背景色。
resid:資源xml文件ID
按鈕默認背景為android.R.drawable.btn_default
android:text setText(CharSequence text) 設置文字
android:textColor setTextColor(int color) 設置文字顏色
android:onClick setOnClickListener(OnClickListener l) 設置點擊事件

下面通過實例來給大家介紹Button的常用效果。

實例:Button點擊事件寫法1、寫法2、設置背景圖片、設置背景顏色、設置背景shape、V7包按鈕樣式

我們首先來看一下布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="10dp"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_click_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button點擊事件寫法1" />

    <Button
        android:id="@+id/btn_click_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="click"
        android:text="Button點擊事件寫法2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@mipmap/icon_button_bg"
        android:padding="10dp"
        android:text="Button設置背景圖片" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@android:color/holo_red_dark"
        android:padding="10dp"
        android:text="Button設置背景顏色" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/shape_button_test"
        android:padding="10dp"
        android:text="Button設置shape" />

    <TextView
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:text="V7包按鈕樣式"
        android:textColor="#ffffffff"
        android:textSize="20sp" />

</LinearLayout>

布局文件對應的效果圖如下:
Button基本使用

上面布局文件中定義了6個Button,它們指定的規則如下。
1.給Button指定了android:id="@+id/btn_click_one",在MainActivity.xml根據id進行查找並且設置點擊事件。

//給第一個按鈕設置點擊事件
findViewById(R.id.btn_click_one).setOnClickListener(onClickListener);

點擊之后進行Toast提示。

private View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v){
            Toast.makeText(MainActivity.this,"Button點擊事件1",Toast.LENGTH_LONG).show();
        }
    };

2.給xml中給button增加了android:onClick="click"屬性,然后在該布局文件對應的Acitivity中實現該方法。需要注意的是這個方法必須符合三個條件:
1).方法的修飾符是 public
2).返回值是 void 類型
3).只有一個參數View,這個View就是被點擊的這個控件。

     public void click(View v){
        switch (v.getId()){
            case R.id.btn_click_two:
                Toast.makeText(MainActivity.this,"Button點擊事件2",Toast.LENGTH_LONG).show();
                break;
        }
    }

3.設置一張背景圖片

android:background="@mipmap/icon_button_bg"

4.設置背景顏色

android:background="@android:color/holo_red_dark"

5.設置背景shape,android:background="@drawable/shape_button_test",可以自定義Button的外觀,從效果圖中我們可以看到Button背景透明,有邊框,有弧度。
shape_button_test.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!--     默認背景色 -->
    <solid android:color="@android:color/transparent"/>
    <!-- 邊框 -->
    <stroke
        android:width="1dp"
        android:color="@android:color/black" />
    <!--     設置弧度 -->
    <corners
        android:radius="20dp"/>
</shape>

6.設置按鈕的樣式

style="@style/Widget.AppCompat.Button.Colored"

這是V7包里面自帶的style樣式。按鈕的顏色是ButtonTest/app/src/main/res/values/colors.xml下name="colorAccent"的顏色。

Button使用注意事項:

1.Button的setOnClickListener優先級比xml中android:onClick高,如果同時設置點擊事件,只有setOnClickListener有效。
2.能用TextView就盡量不要用Button,感覺TextView靈活性更高。(純屬個人意見)

學到了以上幾招,能解決開發中Button的大部分用法。
點擊下載源碼

各位看官如果覺得文章不錯,幫忙點個贊吧,對於你來說是舉手之勞,但對於我來說這就是堅持下去的動力。

如果你想第一時間看我們的后期文章,掃碼關注公眾號,每周不定期推送Android開發實戰教程文章,你還等什么,趕快關注吧,學好技術,出任ceo,贏取白富美。。。。

      Android開發666 - 安卓開發技術分享
            掃描二維碼加關注

Android開發666


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM