Android開發(fā)中Button組件的使用
前言
安卓系統(tǒng)中,Button是程序和用戶進(jìn)行交互的一個(gè)重要控件,今天我們就來簡單的對(duì)Button進(jìn)行學(xué)習(xí),其中Button組件是文本按鈕(繼承自TextView),而ImageButton是圖像按鈕(繼承自ImageView)。兩者之間的區(qū)別在于:
- 1、Button即可顯示文本也可顯示圖形(通過設(shè)置背景圖),而ImageButton只能顯示圖形不能顯示文本;
- 2、Button可在文本周圍區(qū)域顯示小圖,而ImageButton無法在某個(gè)區(qū)域顯示小圖;
- 3、ImageButton上的圖像可按比例進(jìn)行拉伸,而Button上的大圖會(huì)拉伸變形(因?yàn)楸尘皥D無法按比例拉伸);
從上面可以看出,Button的適應(yīng)面更廣,所以實(shí)際開發(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說明
就這樣,我們就在活動(dòng)中加入了一個(gè)Button控件,并且命名為Hello World,但是有沒有發(fā)現(xiàn)活動(dòng)上現(xiàn)實(shí)的名稱和我們輸入的名稱是不是不一樣呢?這是由于系統(tǒng)會(huì)對(duì)Button控件中所有的英文字母自動(dòng)進(jìn)行大寫轉(zhuǎn)換,當(dāng)然,我們肯定需要禁用這一屬性,如下面代碼,我們進(jìn)行對(duì)這一屬性進(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)行對(duì)默認(rèn)全部大寫進(jìn)行禁用,當(dāng)然對(duì)于按鈕控件不僅僅就這么簡單的一些屬性,詳細(xì)信息可通過該文檔詳細(xì)了解。
現(xiàn)在我們的按鈕正常顯示在活動(dòng)中,但是我們?cè)撛趺醋屗c(diǎn)擊時(shí)能夠響應(yīng),其實(shí)響應(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) {
// 在這里實(shí)現(xiàn)響應(yīng)
// 我們?cè)谶@里就進(jìn)行Toast
Toast.makeText(ButtonActivity.this, "點(diǎn)擊響應(yīng),通過匿名內(nèi)部類實(shí)現(xiàn)", Toast.LENGTH_SHORT).show();
}
});
}
}
效果如下所示:
button點(diǎn)擊響應(yīng)說明
這樣,每當(dāng)點(diǎn)擊按鈕的時(shí)候,就會(huì)執(zhí)行監(jiān)聽器中onClick()方法,我們只需要在這個(gè)方法中加入我們需要處理的邏輯就好。
- 實(shí)現(xiàn)接口
第二種方法就是使用實(shí)現(xiàn)接口的方法進(jìn)行實(shí)現(xiàn)注冊(cè)監(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:
// 實(shí)現(xiàn)處理邏輯
Toast.makeText(ButtonActivity.this, "點(diǎn)擊響應(yīng),通過實(shí)現(xiàn)接口實(shí)現(xiàn)", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
實(shí)現(xiàn)效果如下所示:
button點(diǎn)擊響應(yīng)說明
上面兩種方法是最常用的響應(yīng)點(diǎn)擊事件的方法
到此這篇關(guān)于Android開發(fā)中Button組件的使用的文章就介紹到這了,更多相關(guān)Android中Button組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android應(yīng)用程序四大組件之使用AIDL如何實(shí)現(xiàn)跨進(jìn)程調(diào)用Service
- Android的Service應(yīng)用程序組件基本編寫方法
- Android Jetpack架構(gòu)組件 ViewModel詳解
- Android ListView UI組件使用說明
- android自定義組件實(shí)現(xiàn)儀表計(jì)數(shù)盤
- Android中butterknife的使用與自動(dòng)化查找組件插件詳解
- Android開發(fā)之組件GridView簡單使用方法示例
- Android列表組件ListView使用詳解之動(dòng)態(tài)加載或修改列表數(shù)據(jù)
- Android四大組件之Service詳解
- Android框架組件Lifecycle的使用詳解
- Android UI新組件學(xué)習(xí)和使用
- 詳解Android的四大應(yīng)用程序組件
相關(guān)文章
Android ViewPager無限循環(huán)實(shí)現(xiàn)底部小圓點(diǎn)動(dòng)態(tài)滑動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android ViewPager無限循環(huán)實(shí)現(xiàn)底部小圓點(diǎn)動(dòng)態(tài)滑動(dòng)的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03
Android實(shí)現(xiàn)長按圓環(huán)動(dòng)畫View效果的思路代碼
這篇文章主要介紹了Android實(shí)現(xiàn)長按圓環(huán)動(dòng)畫View效果,本文給大家分享實(shí)現(xiàn)思路,通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Android InputMethodManager輸入法簡介
這篇文章主要介紹了Android InputMethodManager輸入法框架的使用,具有參考價(jià)值,需要的朋友可以參考下。2016-06-06
Android開發(fā)實(shí)現(xiàn)日期時(shí)間控件選擇
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)實(shí)現(xiàn)日期時(shí)間控件選擇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
Android xmlns 的作用及其自定義實(shí)例詳解
這篇文章主要介紹了 Android xmlns 的作用及其自定義實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
Kotlin標(biāo)準(zhǔn)函數(shù)與靜態(tài)方法基礎(chǔ)知識(shí)詳解
Kotlin中的標(biāo)準(zhǔn)函數(shù)指的是Standard.kt文件中定義的函數(shù),任何Kotlin代碼都可以自由地調(diào)用所有的標(biāo)準(zhǔn)函數(shù)。例如let這個(gè)標(biāo)準(zhǔn)函數(shù),他的主要作用就是配合?.操作符來進(jìn)行輔助判空處理2022-11-11
詳解Android控件之DatePicker、TimePicker探究
本篇文章主要介紹了Android控件之DatePicker、TimePicker探究,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2016-12-12

