欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)中Button組件的使用

 更新時間:2020年07月20日 12:06:28   作者:suwu150  
這篇文章主要介紹了Android開發(fā)中Button組件的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言

   安卓系統(tǒng)中,Button是程序和用戶進行交互的一個重要控件,今天我們就來簡單的對Button進行學習,其中Button組件是文本按鈕(繼承自TextView),而ImageButton是圖像按鈕(繼承自ImageView)。兩者之間的區(qū)別在于:

  • 1、Button即可顯示文本也可顯示圖形(通過設(shè)置背景圖),而ImageButton只能顯示圖形不能顯示文本;
  • 2、Button可在文本周圍區(qū)域顯示小圖,而ImageButton無法在某個區(qū)域顯示小圖;
  • 3、ImageButton上的圖像可按比例進行拉伸,而Button上的大圖會拉伸變形(因為背景圖無法按比例拉伸);

從上面可以看出,Button的適應面更廣,所以實際開發(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控件中所有的英文字母自動進行大寫轉(zhuǎ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"進行對默認全部大寫進行禁用,當然對于按鈕控件不僅僅就這么簡單的一些屬性,詳細信息可通過該文檔詳細了解。

現(xiàn)在我們的按鈕正常顯示在活動中,但是我們該怎么讓他點擊時能夠響應,其實響應的方法有很多,下面就來說說常見的兩種響應方法

添加響應事件

  • 匿名內(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)響應
 // 我們在這里就進行Toast
 Toast.makeText(ButtonActivity.this, "點擊響應,通過匿名內(nèi)部類實現(xiàn)", Toast.LENGTH_SHORT).show();
 }
 });
 }
}

效果如下所示:

 

button點擊響應說明

這樣,每當點擊按鈕的時候,就會執(zhí)行監(jiān)聽器中onClick()方法,我們只需要在這個方法中加入我們需要處理的邏輯就好。

  • 實現(xiàn)接口

第二種方法就是使用實現(xià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, "點擊響應,通過實現(xiàn)接口實現(xiàn)", Toast.LENGTH_SHORT).show();
 break;
 default:
 break;
 }
 }
}

實現(xiàn)效果如下所示:

 

button點擊響應說明

上面兩種方法是最常用的響應點擊事件的方法

到此這篇關(guān)于Android開發(fā)中Button組件的使用的文章就介紹到這了,更多相關(guān)Android中Button組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android使用Service實現(xiàn)IPC通信的2種方式

    Android使用Service實現(xiàn)IPC通信的2種方式

    這篇文章主要介紹了Android使用Service實現(xiàn)IPC通信的2種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03
  • Android listview多視圖嵌套多視圖

    Android listview多視圖嵌套多視圖

    這篇文章主要介紹了Android listview多視圖嵌套多視圖 的相關(guān)資料,需要的朋友可以參考下
    2016-02-02
  • Android編程之書架效果背景圖處理方法

    Android編程之書架效果背景圖處理方法

    這篇文章主要介紹了Android編程之書架效果背景圖處理方法,在前面一篇《android書架效果實現(xiàn)原理與代碼》的基礎(chǔ)上做了一定的修改,重寫GridView類處理了背景圖效果,需要的朋友可以參考下
    2015-12-12
  • JetPack Compose底部導航欄的實現(xiàn)方法詳解

    JetPack Compose底部導航欄的實現(xiàn)方法詳解

    開發(fā)一個新項目,底部導航欄一般是首頁的標配,在以前的xml布局中,我們可以很輕松的是用谷歌提供的BottomNavigationView或者自定義來實現(xiàn)底部導航的功能,在Compose中也有也提供了一個類似的控件androidx.compose.material.BottomNavigation
    2022-09-09
  • Android實現(xiàn)動態(tài)自動匹配輸入內(nèi)容功能

    Android實現(xiàn)動態(tài)自動匹配輸入內(nèi)容功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)動態(tài)自動匹配輸入內(nèi)容功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Android 組合控件實現(xiàn)布局的復用的方法

    Android 組合控件實現(xiàn)布局的復用的方法

    本篇文章主要介紹了Android 組合控件實現(xiàn)布局的復用的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • 詳解Android的兩種事件處理機制

    詳解Android的兩種事件處理機制

    這篇文章主要介紹了詳解Android的兩種事件處理機制,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2016-11-11
  • 淺析Android手機衛(wèi)士讀取聯(lián)系人

    淺析Android手機衛(wèi)士讀取聯(lián)系人

    這篇文章主要介紹了淺析Android手機衛(wèi)士讀取聯(lián)系人的相關(guān)內(nèi)容,通過getContentResolver()方法獲取獲取ContentResolver內(nèi)容解析器對象,對android手機衛(wèi)士讀取聯(lián)系人相關(guān)知識感興趣的朋友參考下吧
    2016-04-04
  • Android編程解析XML文件的方法詳解【基于XmlPullParser】

    Android編程解析XML文件的方法詳解【基于XmlPullParser】

    這篇文章主要介紹了Android編程解析XML文件的方法,結(jié)合實例形式分析了Android基于XmlPullParser解析xml文件的相關(guān)操作技巧與注意事項,需要的朋友可以參考下
    2017-07-07
  • Android 實現(xiàn)秒轉(zhuǎn)換成時分秒的方法

    Android 實現(xiàn)秒轉(zhuǎn)換成時分秒的方法

    這篇文章主要介紹了Android 實現(xiàn)秒轉(zhuǎn)換成時分秒的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05

最新評論