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上的大圖會拉伸變形(因?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: 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)實(shí)的名稱和我們輸入的名稱是不是不一樣呢?這是由于系統(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: 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)在我們的按鈕正常顯示在活動中,但是我們該怎么讓他點(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) // 我們在這里就進(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í)候,就會執(zhí)行監(jiān)聽器中onClick()方法,我們只需要在這個方法中加入我們需要處理的邏輯就好。
實(shí)現(xiàn)接口第二種方法就是使用實(shí)現(xiàn)接口的方法進(jìn)行實(shí)現(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: // 實(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)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera2. CSS hack用法案例詳解3. 讀大數(shù)據(jù)量的XML文件的讀取問題4. HTML DOM setInterval和clearInterval方法案例詳解5. XML入門的常見問題(一)6. html小技巧之td,div標(biāo)簽里內(nèi)容不換行7. 詳解盒子端CSS動畫性能提升8. 詳解瀏覽器的緩存機(jī)制9. 告別AJAX實(shí)現(xiàn)無刷新提交表單10. msxml3.dll 錯誤 800c0019 系統(tǒng)錯誤:-2146697191解決方法
