Android 開發第五講 學習Button了解Button屬性
一丶Button的分類
1.1 Android Button類型
根據Android 官網文檔所屬. Button可以定義三種形式的
- Button 類型
- ImageButton 圖標類型的
- 帶有android:drawableLeft 屬性的 也就是帶有圖片的
XML如下
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
... />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button_icon"
android:contentDescription="@string/button_icon_desc"
... />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:drawableLeft="@drawable/button_icon"
... />
1.2 響應Button類型的事件
根據官方文檔所屬.有兩種形式可以進行響應Button類型事件
- 直接XML屬性中定義 android:onClick 並且指向你的函數 你在你的Active布局文件中(代碼文件)聲明並且實現這個方法
- 使用OnClickListener 方法.不用再XML中編寫指定函數了.
第一種XML形式如下;
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
指定Button 響應的方法為 sendMessage
在Active中聲明並實現
public void sendMessage(View view) {
// Do something in response to button click
//提示一個信息兩秒鍾消失
Toast.makeText(this, "點擊了我", Toast.LENGTH_SHORT).show();
}
注意
1.方法修飾必須是公共的(Public)
2.無返回值 void
3.比如傳入一個View 參數
為啥這樣做.在Android中我們聲明一個Button類型的變量 在上面 按快捷鍵 CTRL+H 可以看類繼承結構我們可以發現是繼承自 TextView類.那么說明可以使用TextView類中的方法. 而繼續網上看可以看到是繼承自Viw類. 所以里的View其實就是父類引用. 可以接納任何繼承自View的子類對象
第二種方式,使用OnClickListener 方法
這里我們需要走三步
1.聲明Button 變量
2.尋找Button Id
3.使用 setOnClickListener 來實現按鈕點擊事件
public Button m_BtnText;
m_BtnText = (Button)findViewById(R.id.m_btnText); //你在xml為button分配的ID
m_BtnText.setOnClickListener() //這里需要一個OnClickListerner對象.有兩種方式, 第一種使用 匿名內部類.創建一個.然后實現里面的OnClick方法. 第二種就是你繼承然后並且實現這個方法. 這里傳入這個類的對象
二丶Button的屬性,實現常用Button
2.1 設置基本Button
Button繼承自TextView.所以一些屬性都是可以用的
text = 指定文本
textSize 指定文字大熊啊
textColor 指定文字顏色等
<!-- 使用Xml指定Click的形式進行代碼操作 函數為sendMsg-->
<Button
android:id="@+id/btn_Id1"
android:layout_width="200dp"
android:layout_height="70dp"
android:text="指定函數形式"
android:textSize="20sp"
android:textColor="#FFA07A"
android:background="#FFEFD5"
android:onClick="sendMsg">
</Button>
2.2 設置圓角Button
圓角Button跟上面一樣,唯一不同的就是 background 位置我們要引用一個描述Button的xml文件. 文件還是放在 drawable 文件中
1.首先在drawable新建資源文件, 文件類型為 shape
新建的shape寫如下
關於 shape進階可以查看 博客 https://www.jianshu.com/p/ef734937b521
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 設置實心-->
<solid android:color="#F08080">
</solid>
<!-- 設置圓角-->
<corners android:radius="20dp">
</corners>
<!-- backgound引用即可-->
</shape>
首先指定了 shape為 rectangle 指定了按鈕為矩形方式
然后設置實心的背景顏色
最后設置圓角. radius是設置四個角.對應的還有左上 右上 右下 左下等.
button xml描述文件如下
<!--設置按鈕圓角 需要設置Drawable-->
<Button
android:id="@+id/btn_Id2"
android:layout_width="200dp"
android:layout_height="70dp"
android:layout_toRightOf="@id/btn_Id1"
android:layout_marginLeft="10dp"
android:background="@drawable/btn_shape"
android:text="圓角按鈕">
</Button>
運行效果截圖
2.3 設置描邊按鈕
復制一個shape xml文件修改位如下
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 設置描邊-->
<stroke
android:color="#FFD700"
android:width="1dp">
</stroke>
<!-- 設置圓角-->
<corners android:radius="20dp">
</corners>
<!-- backgound引用即可-->
</shape>
xml代碼描述如下
<!-- 設置描邊按鈕-->
<Button
android:id="@+id/btn_Id3"
android:layout_width="200dp"
android:layout_height="70dp"
android:layout_below="@id/btn_Id1"
android:layout_marginLeft="10dp"
android:background="@drawable/btn_shape1"
android:text="描邊按鈕">
</Button>
運行之后效果演示
2.4 實現按鈕 按壓效果. xml表達
我們的控件是運行在android手機上的.當按鈕按鈕的時候會產生效果.
比如按鈕當沒按壓的時候的顏色跟按壓的時候的顏色是不一樣的.
這里使用xml實現,這里要使用一個 狀態選擇器的 xml文件來描述
狀態選擇器參考資料 進階 https://blog.csdn.net/qq_20451879/article/details/80340823
首先跟創建shape文件一樣,只不過此時修改為select
selectxml文件如下
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 狀態選擇器 android:state_pressed = true代表按壓-->
<item android:state_pressed="true">
<!-- 如果是按壓,那么我們使用shape畫一個-->
<shape>
<!-- 設置實心顏色,並且設置圓角-->
<solid android:color="#ff0000"></solid>
<corners android:radius="5dp"></corners>
</shape>
</item>
<!-- 否則設置為綠色-->
<item android:state_pressed="false">
<!-- 如果是按壓,那么我們使用shape畫一個-->
<shape>
<!-- 設置實心顏色,並且設置圓角-->
<solid android:color="#0ca30c"></solid>
<corners android:radius="5dp"></corners>
</shape>
</item>
</selector>
button中的按鈕四進行引用
<!-- 設置按鈕按壓狀態-->
<Button
android:id="@+id/btn_Id4"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_below="@id/btn_Id3"
android:layout_marginLeft="10dp"
android:background="@drawable/btn_4"
android:text="掛斷電話">
</Button>
運行之后顯示效果
未按壓
按壓