你笑我為何拿上錘和盾,有你在,我不會受到傷害,只是,你不知道,會讓我受傷的,是你,
Android修行手冊-玩轉TextView,想不到有這么多屬性
【萬字】熬夜修行Android Studio技巧到出神入化,快速漲薪【建議收藏】
Android修行手冊之從頭到尾學Kotlin【全】
點擊跳轉=>熬夜再戰Android從青銅到王者-開發效率插件篇
點擊跳轉=>Unity粒子特效系列-龍卷風預制體做好了,unitypackage包直接用!
點擊跳轉=>姐姐喊我解鎖套娃新技能:FairyGUI在Unity中實作List嵌套List/立體畫廊等,玩出花兒來
點擊跳轉=>Unity新手必備5款寶藏插件–價值上千元白嫖最新版
爆肝萬字C#基礎入門大總結【建議收藏】
👉關于作者
眾所周知,人生是一個漫長的流程,不斷克服困難,不斷反思前進的程序,在這個程序中會產生很多對于人生的質疑和思考,于是我決定將自己的思考,經驗和故事全部分享出來,以此尋找共鳴 !!!
專注于Android/Unity和各種游戲開發技巧,以及各種資源分享(網站、工具、素材、原始碼、游戲等)
有什么需要歡迎私我,交流群讓學習不再孤單,
👉前提
前兩天我們剛學問TextView的屬性詳解,應該是最全的了,今天我們學學按鈕(Button)這個控制元件,
這是小空堅持寫的Android系列,歡迎品嘗,
👉實踐程序
😜初識
我們先創建一個基本的Button看看:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TextActivity">
<Button
android:id="@+id/mtBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這是按鈕" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="this button" />
</RelativeLayout>
界面顯示如下:

恩,漢字顯示很正常,但是英文代碼中不是小寫嗎,為什么顯示是大寫,
其實是屬性textAllCaps在作怪,改成下面再看看是否正常了,
<Button
android:id="@+id/mtBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textAllCaps="false"
android:text="this button" />
😜事故
這時候又有朋友提問了:不對啊,除了英文你的顏色和我的也不一樣啊,明明我也是和你寫的一樣的代碼,

小空告訴你:其實這是應用主題導致的,默認創建的專案有個主題,當然你可以自定義,
在【AndroidManifest.xml】檔案的【android:theme】屬性設定,【Ctrl+左鍵】點進去后你就會發現里面是一些相關顏色定義,而且你要注意布局預覽界面也有主題的選擇,小心被坑,
主題我們先暫且放下,繼續深入Button,
既然是按鈕,那互動是必須的操作,怎么設定點擊事件呢?
public class TextActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text);
Button btn = findViewById(R.id.mtBtn);
btn.setOnClickListener(view -> {
Toast.makeText(TextActivity.this, "點擊了按鈕", Toast.LENGTH_SHORT).show();
});
}
}
運行后,點擊按鈕即可看見效果啦,
題外話:在App中,除了用戶無意的頻繁點擊,有時候網路請求慢,用戶也會有意的一直去點擊,如果你不做處理,會重復的做處理,這必然是不合規的,所以就有了防止按鈕多次點擊的方案:
- 在點擊事件中判斷距離上一次點擊的時間,
- 封裝個點擊事件里面同樣是判斷點擊時間,只不過后續view的點擊事件全部用這個,
- 寫一個靜態方法同樣是判斷點擊時間,在系統的點擊事件第一行就判斷這個靜態時間回傳true或false
😜selector選擇器
我們先按照上一節的shape方式創建兩個shape背景
btn_selector_shape1.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 圓角 -->
<corners android:radius="5dp" />
<!--填充顏色-->
<solid android:color="#00ff00" />
</shape>
btn_selector_shape2.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--圓角-->
<corners android:radius="5dp" />
<!--填充顏色-->
<solid android:color="#0000ff" />
</shape>
接著我們在【res-drawable】右鍵創建個Drawable Resource File ,彈出框寫檔案名創建檔案,設定默認【Root element】為selector,
btn_selector0.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_selector_shape1" android:state_pressed="true" />
<item android:drawable="@drawable/btn_selector_shape2" android:state_window_focused="false" />
</selector>
布局中參考
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".TextActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="160dp"
android:background="@drawable/btn_selector0"
android:text="按下變色"
android:textColor="@color/white" />
</RelativeLayout>
我們運行下看看


但是
我們回憶下,剛才是不是創建了三個檔案,按鈕少的情況下還好,自定義的按鈕一多,這么多檔案非常不容易管理,所以我們要用另外一種寫法,將所有內容放到一個檔案中,
我們在剛才的btn.selector0.xml中修改:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--這是第一種方式,利用drwable參考檔案-->
<!--<item android:drawable="@drawable/btn_selector_shape1" android:state_pressed="true" />-->
<!--<item android:drawable="@drawable/btn_selector_shape2" android:state_pressed="false" />-->
<!--第二種方式如下-->
<item android:state_pressed="false">
<shape android:shape="rectangle">
<!-- 圓角 -->
<corners android:radius="5dp" />
<!--填充顏色為白色-->
<solid android:color="#0000ff" />
</shape>
</item>
<!--單擊時是一個帶圓角,白色背景,綠色邊框的矩形-->
<item android:state_pressed="true">
<shape android:shape="rectangle">
<!--圓角-->
<corners android:radius="5dp" />
<!--填充顏色為白色-->
<solid android:color="#00ff00" />
</shape>
</item>
</selector>
我們運行起來看看,哎,效果很正確啊
Selector的屬性不止這兩個哦:
- state_focused 布林值,是否獲得焦點
- state_window_focused 布林值,是否獲得視窗焦點
- state_enabled 布林值,控制元件是否可用
- state_checkable 布林值,控制元件可否被勾選
- state_checked 布林值,控制元件是否被勾選
- state_selected 布林值,控制元件是否被選擇,針對有滾輪的情況
- state_pressed 布林值,控制元件是否被按下
- state_active 布林值,控制元件是否處于活動狀態
- state_single和state_first和state_middle很少使用,知道就行,
👉其他
📢作者:小空和小芝中的小空
📢轉載說明-務必注明來源:https://zhima.blog.csdn.net/
📢歡迎點贊👍收藏🌟留言📝
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/385421.html
標籤:其他
