android基礎組件----Button的使用

  按鈕由文本或圖標(或文本和一個圖標)組成,當用戶觸摸到它時,會發生一些動作。今天我們開始Button的學習。少年的愛情永遠不夠用,一杯酒足以了卻一件心事。

?

Button的簡要說明

根據你是否想要一個帶有文本的按鈕,一個圖標,或者兩者,你可以在三種方式中創建按鈕來進行布局:

  

  • With text, using the Button class:
<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/button_text"... />
  • With an icon, using the ImageButton class:
<ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/button_icon"... />
  • With text and an icon, using the Button class with the android:drawableLeft attribute:
<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/button_text"android:drawableLeft="@drawable/button_icon"... />

?

按鈕的響應

1) xml中定義的Button中增加屬性android:onClick

  • 在xml中定義android:onClick屬性:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/button_send"android:onClick="sendMessage" />
  • 在MainActivity中定義sendMessage方法:
public void sendMessage(View view) {// Do something in response to button click
}

2) 在代碼中使用OnClickListener

Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// Do something in response to button click
    }
});

?

按鈕樣式

1) Borderless button : To create a borderless button, apply the borderlessButtonStyle style to the button.

<Buttonandroid:onClick="sendMessage"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="bordless button"style="?android:attr/borderlessButtonStyle" />

2) Custom background: Instead of supplying a simple bitmap or color, however, your background should be a state list resource that changes appearance depending on the button's current state

  • Create three bitmaps for the button background that represent the default, pressed, and focused button states.?
  • Place the bitmaps into the res/drawable/ directory of your project. Be sure each bitmap is named properly to reflect the button state that they each represent.

  

  • Create a new XML file in the res/drawable/ directory (name it something like button_custom.xml). Insert the following XML
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/button_pressed"android:state_pressed="true" /><item android:drawable="@drawable/button_focused"android:state_focused="true" /><item android:drawable="@drawable/button_default" />
</selector>
  • Then simply apply the drawable XML file as the button background
<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="send button"android:background="@drawable/button_custom"  />

?

測試的項目

?項目結構:

MainActivity.java:

package com.huhx.linux.buttontest;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;public class MainActivity extends AppCompatActivity {private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.sendButton);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "button click", Toast.LENGTH_SHORT).show();}});}// bordless buttonpublic void sendMessage(View view) {Toast.makeText(MainActivity.this, "Borderless Button", Toast.LENGTH_SHORT).show();}
}

?button_custom.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/button_pressed"android:state_pressed="true" /><item android:drawable="@drawable/button_focused"android:state_focused="true" /><item android:drawable="@drawable/button_default" />
</selector>

?activity_main.xml.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.huhx.linux.buttontest.MainActivity"><Buttonandroid:id="@+id/sendButton"android:text="text button"android:layout_width="wrap_content"android:layout_height="wrap_content" /><ImageButtonandroid:src="@mipmap/ic_launcher"android:layout_width="wrap_content"android:layout_height="wrap_content" /><Buttonandroid:drawableRight="@mipmap/ic_launcher"android:text="Linux"android:layout_width="wrap_content"android:layout_height="wrap_content" /><Buttonandroid:onClick="sendMessage"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="bordless button"style="?android:attr/borderlessButtonStyle" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="send button"android:background="@drawable/button_custom"  />
</LinearLayout>

?運行效果如下:

友情鏈接

?

轉載于:https://www.cnblogs.com/huhx/p/baseuseandroidbutton.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/255387.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/255387.shtml
英文地址,請注明出處:http://en.pswp.cn/news/255387.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

@Repository、@Service、@Controller 和 @Component

Repository 、Service 、 Controller 、Component 這四個Spring注解 ,用于把加了注解的 類 加入到Spring 容器中管理&#xff0c;節省了xml 的繁重的配置&#xff0c;盡管如此xml 同樣可以實現&#xff08;一般建議先搞懂xml&#xff09;。 Repository Repository注解便屬于最先…

【pyqt5學習】——QTextEdit控件學習:獲取文本、添加文本

目錄 1、QTextEdit控件介紹 2、QTextEdit控件添加文本、添加HTML格式 3、QTextEdit控件獲取文本、獲取HTML格式文本 4、案例 1&#xff09;完整代碼 2&#xff09;效果 1、QTextEdit控件介紹 QTextEdit控件是一個支持多行輸入的輸入框&#xff0c;支持HTML進行格式的設置 2…

空間變化

空間變化 第一種&#xff1a;圓 第二種: 矩形 第三種&#xff1a;正五邊形 第四種:正六邊形 第五種&#xff1a;腰圓 算法&#xff1a; 在規則圖形&#xff08;不包括圓心-直徑法&#xff09;中&#xff0c;等于或者超過三個點。一般的&#xff0c;利用p1,p2,p3來建立一個用戶…

【pyqt5學習】——QAbstractButton學習(普通按鈕QPushButton、工具按鈕QToolButton、單選按鈕QRadioButton、復選框按鈕QCheckBox)

目錄 1、按鈕介紹 2、普通按鈕QPushButton學習 1&#xff09;特殊模式——開關按鈕&#xff08;toggle&#xff09; ①將按鈕設置為可選擇的 ②將按鈕按下 判斷按鈕是否被按下 isChecked() 2&#xff09;給按鈕設置前置圖標setIcon() ?編輯 3&#xff09;按鈕信號 4)完…

java 企業 網站源碼 模版 屏幕自適應 有前后臺 springmvc SSM 生成靜態化

前臺&#xff1a; 支持四套模版&#xff0c; 可以在后臺切換點擊&#xff1a;獲取地址QQ 313596790官網 http://www.fhadmin.org/系統介紹&#xff1a;1.網站后臺采用主流的 SSM 框架 jsp JSTL&#xff0c;網站后臺采用freemaker靜態化模版引擎生成html2.因為是生成的html&…

挖財后端架構簡介

挖財后端技術用的主要是比較大眾的東西&#xff0c;Web容器用Tomcat&#xff0c;框架主要是Spring MVC&#xff0c;也有少量的Play&#xff0c;中間服務層是Dubbo&#xff0c;微容器用Spring Boot&#xff0c;服務注冊這一塊是用ZooKeeper&#xff0c;核心業務開發方式還是圍繞…

【pyqt5學習】——QcomboBox學習

目錄 1、添加條目&#xff1a; 1)逐一添加 2&#xff09;批量添加 3&#xff09;獲取當前選中的索引 4&#xff09;獲取當前選中文本 5&#xff09;根據索引獲取文本 6&#xff09;統計條目總數 7&#xff09;信號——當前選中的索引發生改變currentIndexChanged() 2、…

vSphere Client 編輯虛擬機屬性的問題

vSphere Client 編輯虛擬機屬性的問題 編輯虛擬機屬性的時候&#xff0c; 出現&#xff1a; vpxclient.vmconfig.cpuid 初始值設置異常之類的&#xff0c;重置了&#xff0c; 并將注冊表中的所有vmvare 相關鍵值刪除了&#xff0c; 還是一樣的。。 后面參照https://kb.vmware.c…

Val編程-系統架構

利用Val可以編寫相對大型軟件。 界面書寫&#xff0c;運動軌跡規劃&#xff0c;外部通訊&#xff0c;進程交互&#xff0c;文本讀寫.... GlobalData可以利用一個庫來作為一個公用庫&#xff0c;實現各個庫的數據交互。 PublicFunc書寫基本的通用函數來作為基本函數&#xff0c;…

poj 1256 Anagram—next_permutation的神奇應用

題意&#xff1a;給你一條字符串&#xff0c;讓你輸出字符串中字符的全排列&#xff0c;輸出的順序要按它給的奇葩的字典序。 題解&#xff1a;要輸出全排列&#xff0c;暴力dfs可以過&#xff0c;但要注意題目的字典序以及相同字符的情況。如果用next_permutation()處理可以簡…

【pyqt5學習】—— 滑動條Qslider、計數器QSpinBox學習

目錄 1、滑動條QSlider 1)常用屬性 2&#xff09;實例——利用滑動條來實現字體大小的修改 ?編輯 2、計數器QSpinBox 1&#xff09;屬性方法 2&#xff09;實例 1、滑動條QSlider 1)常用屬性 self.slider QSlider(Qt.Horizontal)# 設置最小值self.slider.setMinimum(2)…

shell常用命令之curl: -w,–write-out參數詳解

顧名思義&#xff0c;write-out的作用就是輸出點什么。curl的-w參數用于在一次完整且成功的操作后輸出指定格式的內容到標準輸出。 輸出格式由普通字符串和任意數量的變量組成&#xff0c;輸出變量需要按照%{variable_name}的格式&#xff0c;如果需要輸出%&#xff0c;double一…

Val編程-速度因子

機械手臂在一個三個基本指令&#xff08;movel,movej,movec&#xff09;指令中有下面基本參數進行配置。 1. Frame toolField; Tcp的值2. Frame frameField;用戶坐標系的值3. MoveType absRelField;絕對運動與相對運動4. Config configField;姿態5. BlendType blendTypeField;倒…

Node.js學習之路24——Express框架的app對象

1.express() 基于Node.js平臺&#xff0c;快速、開放、極簡的web開發框架。創建一個Express應用.express()是一個由express模塊導出的入口top-level函數.const express require(express); let app express(); 1.1 靜態資源管理 express.static(root, [options]) express.stat…

【pyqt5學習】——對話框QDialog學習(QMessageBox、QColorDialog、QFIleDialog、QFontDialog、QInputDialog)

目錄 1、對話框QDialog類別 2、通用對話框 ?編輯 3、消息對話框QMessageBox() 1&#xff09;消息對話框QMessageBox類型 2&#xff09;案例 ?編輯 4、輸入對話框QInputDialog 1) 類型 2&#xff09;案例 5、字體格式對話框QFontDialog 6、顏色對話框QColorDialog 1&…

使用 Mesos 管理虛擬機

摘要 為了滿足渲染、基因測序等計算密集型服務的需求&#xff0c;UCloud 推出了“計算工廠”產品&#xff0c;讓用戶可以快速創建大量的計算資源&#xff08;虛擬機&#xff09;。該產品的背后&#xff0c;是一套基于 Mesos 的計算資源管理系統。本文簡要介紹該系統的結構、Mes…

Swift數據類型_整型和浮點型

//swift中的整型和浮點型/***//類型推斷整數是Int 浮點數是Double ,日常使用需要注意不能越界,存儲時間毫秒數 英雄經驗數等等之類內容容易越界整型大多數情況下&#xff0c;你不需要在代碼中指定哪種整型。Swift提供了一種額外的整型&#xff0c;Int類型Java中的long型&#x…

Val編程-套接字

套接字的介紹&#xff1a;http://zh.wikipedia.org/wiki/Berkeley%E5%A5%97%E6%8E%A5%E5%AD%97 主要分為客戶端和服務器。客戶端一般是需要主動去鏈接&#xff0c;需要配置服務器的IP和端口。服務器是被動響應&#xff0c;需要打開相應的端口。端口一般不推薦使用系統端口和常用…

【pyqt5學習】——Qpainter控件學習(文本、圖像、各種圖形)

目錄 1、作用及應用步驟 2、繪制文本drawText() 3、像素級別繪制點&#xff08;正弦曲線為例&#xff09; 4、繪制不同樣式的直線 5、繪制弧形、圓形、扇形、圖像等 6、畫刷填充區域QBrush 1、作用及應用步驟 QPainter是一個用于繪制的類&#xff0c;該類可以用于繪制&…

反向代理與Real-IP和X-Forwarded-For(轉)

如下圖所示&#xff0c;客戶端通過Nginx Proxy1 和 Nginx Proxy2 兩層反向代理才訪問到具體服務Nginx Backend&#xff08;或如Tomcat服務&#xff09;。那Nginx Backend如何才能拿到真實客戶端IP呢&#xff1f; 接下來我們來看看如何才能獲取到客戶端真實IP。 場景1  場景1是…