Android開發(fā)中Button組件的使用
前言
安卓系統(tǒng)中,Button是程序和用戶進(jìn)行交互的一個(gè)重要控件,今天我們就來(lái)簡(jiǎn)單的對(duì)Button進(jìn)行學(xué)習(xí),其中Button組件是文本按鈕(繼承自TextView),而ImageButton是圖像按鈕(繼承自ImageView)。兩者之間的區(qū)別在于:
1、Button即可顯示文本也可顯示圖形(通過設(shè)置背景圖),而ImageButton只能顯示圖形不能顯示文本; 2、Button可在文本周圍區(qū)域顯示小圖,而ImageButton無(wú)法在某個(gè)區(qū)域顯示小圖; 3、ImageButton上的圖像可按比例進(jìn)行拉伸,而Button上的大圖會(huì)拉伸變形(因?yàn)楸尘皥D無(wú)法按比例拉伸);從上面可以看出,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說(shuō)明
就這樣,我們就在活動(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: 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ì)于按鈕控件不僅僅就這么簡(jiǎn)單的一些屬性,詳細(xì)信息可通過該文檔詳細(xì)了解。
現(xiàn)在我們的按鈕正常顯示在活動(dòng)中,但是我們?cè)撛趺醋屗c(diǎn)擊時(shí)能夠響應(yīng),其實(shí)響應(yīng)的方法有很多,下面就來(lái)說(shuō)說(shuō)常見的兩種響應(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)說(shuō)明
這樣,每當(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)說(shuō)明
上面兩種方法是最常用的響應(yīng)點(diǎn)擊事件的方法
到此這篇關(guān)于Android開發(fā)中Button組件的使用的文章就介紹到這了,更多相關(guān)Android中Button組件內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. .net如何優(yōu)雅的使用EFCore實(shí)例詳解2. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車3. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)4. 通過Ajax方式綁定select選項(xiàng)數(shù)據(jù)的實(shí)例5. ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖6. 通過CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效7. 一文透徹詳解.NET框架類型系統(tǒng)設(shè)計(jì)要點(diǎn)8. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁(yè)9. ajax動(dòng)態(tài)加載json數(shù)據(jù)并詳細(xì)解析10. Python快速將ppt制作成配音視頻課件的操作方法
