Android編程之Button控制項用法執行個體分析_Android

來源:互聯網
上載者:User

本文執行個體講述了Android編程之Button控制項用法。分享給大家供大家參考,具體如下:

一、Button概述

android.widget.Button直接繼承於android.wdiget.TextView.

直接子類有:CompoundButton.

間接子類有:CheckBox,RadioButton,Switch,ToggleButton.

Button類表示一個“按鈕”控制項。“按鈕”控制項可以被使用者按下或者點擊,來觸發另一個操作。

二、Button的用法

一種典型的用法:

public class MyActivity extends Activity {   protected void onCreate(Bundle icicle) {     super.onCreate(icicle);     setContentView(R.layout.content_layout_id);     final Button button = (Button) findViewById(R.id.button_id);     button.setOnClickListener(new View.OnClickListener() {       public void onClick(View v) {         // Perform action on click       }     });   }}

除了直接在Activity類中為Button綁定OnClickListener之外,還可以在XML檔案中通過android:onClick屬性來綁定觸發的方法。

舉個例子:

<Button    android:layout_height="wrap_content"    android:layout_width="wrap_content"    android:text="@string/self_destruct"    android:onClick="selfDestruct" />

現在,當使用者按下這個按鈕,系統會調用Activity中的selfDestruct(View)方法,為了讓這個方法有效,方法必須為public且只能接受一個View參數。當方法被調用時,被點擊的那個控制項會作為View類型的參數傳入selfDestruct(View)方法中。如:

public void selfDestruct(View view) {    // Kabloey } 

三、XML屬性

Button的XML屬性基本與TextView一致。感興趣的朋友可參看本站相關文檔。

四、常用public方法

Button的常用public方法基本與TextView一致。

希望本文所述對大家Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.