Android開發(fā)中Button組件的使用
前言
安卓系統(tǒng)中,Button是程序和用戶進(jìn)行交互的一個重要控件,今天我們就來簡單的對Button進(jìn)行學(xué)習(xí),其中Button組件是文本按鈕(繼承自TextView),而ImageButton是圖像按鈕(繼承自ImageView)。兩者之間的區(qū)別在于:
- 1、Button即可顯示文本也可顯示圖形(通過設(shè)置背景圖),而ImageButton只能顯示圖形不能顯示文本;
- 2、Button可在文本周圍區(qū)域顯示小圖,而ImageButton無法在某個區(qū)域顯示小圖;
- 3、ImageButton上的圖像可按比例進(jìn)行拉伸,而Button上的大圖會拉伸變形(因為背景圖無法按比例拉伸);
從上面可以看出,Button的適應(yīng)面更廣,所以實際開發(fā)中基本使用Button。
使用
在界面顯示
首先我們能夠xml文件中加入Button,如下面代碼所示:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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=".ButtonActivity"> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World!" /> </android.support.constraint.ConstraintLayout>
加入之后顯示效果如下所示:
button說明
就這樣,我們就在活動中加入了一個Button控件,并且命名為Hello World,但是有沒有發(fā)現(xiàn)活動上現(xiàn)實的名稱和我們輸入的名稱是不是不一樣呢?這是由于系統(tǒng)會對Button控件中所有的英文字母自動進(jìn)行大寫轉(zhuǎn)換,當(dāng)然,我們肯定需要禁用這一屬性,如下面代碼,我們進(jìn)行對這一屬性進(jìn)行禁用
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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=".ButtonActivity"> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello World!" android:textAllCaps="false" /> </android.support.constraint.ConstraintLayout>
上面代碼中,我們使用了android:textAllCaps="false"進(jìn)行對默認(rèn)全部大寫進(jìn)行禁用,當(dāng)然對于按鈕控件不僅僅就這么簡單的一些屬性,詳細(xì)信息可通過該文檔詳細(xì)了解。
現(xiàn)在我們的按鈕正常顯示在活動中,但是我們該怎么讓他點擊時能夠響應(yīng),其實響應(yīng)的方法有很多,下面就來說說常見的兩種響應(yīng)方法
添加響應(yīng)事件
- 匿名內(nèi)部類
<第一種方法就是在ButtonActivity中為Button添加監(jiān)聽器,如下面代碼所示:
package com.example.jkwu.uicomponent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class ButtonActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_button); Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 在這里實現(xiàn)響應(yīng) // 我們在這里就進(jìn)行Toast Toast.makeText(ButtonActivity.this, "點擊響應(yīng),通過匿名內(nèi)部類實現(xiàn)", Toast.LENGTH_SHORT).show(); } }); } }
效果如下所示:
button點擊響應(yīng)說明
這樣,每當(dāng)點擊按鈕的時候,就會執(zhí)行監(jiān)聽器中onClick()方法,我們只需要在這個方法中加入我們需要處理的邏輯就好。
- 實現(xiàn)接口
第二種方法就是使用實現(xiàn)接口的方法進(jìn)行實現(xiàn)注冊監(jiān)聽器的功能,代碼如下所示:
package com.example.jkwu.uicomponent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class ButtonActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_button); Button button = findViewById(R.id.button); button.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: // 實現(xiàn)處理邏輯 Toast.makeText(ButtonActivity.this, "點擊響應(yīng),通過實現(xiàn)接口實現(xiàn)", Toast.LENGTH_SHORT).show(); break; default: break; } } }
實現(xiàn)效果如下所示:
button點擊響應(yīng)說明
上面兩種方法是最常用的響應(yīng)點擊事件的方法
到此這篇關(guān)于Android開發(fā)中Button組件的使用的文章就介紹到這了,更多相關(guān)Android中Button組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android應(yīng)用程序四大組件之使用AIDL如何實現(xiàn)跨進(jìn)程調(diào)用Service
- Android的Service應(yīng)用程序組件基本編寫方法
- Android Jetpack架構(gòu)組件 ViewModel詳解
- Android ListView UI組件使用說明
- android自定義組件實現(xiàn)儀表計數(shù)盤
- Android中butterknife的使用與自動化查找組件插件詳解
- Android開發(fā)之組件GridView簡單使用方法示例
- Android列表組件ListView使用詳解之動態(tài)加載或修改列表數(shù)據(jù)
- Android四大組件之Service詳解
- Android框架組件Lifecycle的使用詳解
- Android UI新組件學(xué)習(xí)和使用
- 詳解Android的四大應(yīng)用程序組件
相關(guān)文章
Android使用Service實現(xiàn)IPC通信的2種方式
這篇文章主要介紹了Android使用Service實現(xiàn)IPC通信的2種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03JetPack Compose底部導(dǎo)航欄的實現(xiàn)方法詳解
開發(fā)一個新項目,底部導(dǎo)航欄一般是首頁的標(biāo)配,在以前的xml布局中,我們可以很輕松的是用谷歌提供的BottomNavigationView或者自定義來實現(xiàn)底部導(dǎo)航的功能,在Compose中也有也提供了一個類似的控件androidx.compose.material.BottomNavigation2022-09-09Android實現(xiàn)動態(tài)自動匹配輸入內(nèi)容功能
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)動態(tài)自動匹配輸入內(nèi)容功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06Android 組合控件實現(xiàn)布局的復(fù)用的方法
本篇文章主要介紹了Android 組合控件實現(xiàn)布局的復(fù)用的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08淺析Android手機(jī)衛(wèi)士讀取聯(lián)系人
這篇文章主要介紹了淺析Android手機(jī)衛(wèi)士讀取聯(lián)系人的相關(guān)內(nèi)容,通過getContentResolver()方法獲取獲取ContentResolver內(nèi)容解析器對象,對android手機(jī)衛(wèi)士讀取聯(lián)系人相關(guān)知識感興趣的朋友參考下吧2016-04-04Android編程解析XML文件的方法詳解【基于XmlPullParser】
這篇文章主要介紹了Android編程解析XML文件的方法,結(jié)合實例形式分析了Android基于XmlPullParser解析xml文件的相關(guān)操作技巧與注意事項,需要的朋友可以參考下2017-07-07Android 實現(xiàn)秒轉(zhuǎn)換成時分秒的方法
這篇文章主要介紹了Android 實現(xiàn)秒轉(zhuǎn)換成時分秒的方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05