Android基礎控件之Button的基本使用


Button基礎

 

  用戶界面部分學起來還真是無處下手哇,總不能一個控件發一篇文吧,略有點費時間啊。。。這個難道不是邊用邊學才給力嗎。。所以我打算從最實用的Button開始下手。

 

  先貼幾個鏈接,好東西:

  android用戶界面的詳盡教程實例系列:

  http://www.cnblogs.com/aimeng/archive/2012/06/26/2563762.html

  android用戶界面教程實例匯總:

  http://www.cnblogs.com/aimeng/archive/2012/06/25/2560905.html

 

  本文主要內容是Button,相關的鏈接:

  一個強大的學習貼:

  http://www.apkbus.com/android-48448-1-1.html

  官方文檔資料:

  http://developer.android.com/reference/android/widget/Button.html

  http://developer.android.com/guide/topics/ui/controls/button.html

 

Button基本使用方法

  首先,添加Button控件到XML布局文件中。也可通過程序添加。

  在布局文件中設置按鈕的一些屬性,如位置,寬高,按鈕上的字,顏色等。

  比較重要的是要給按鈕一個id號,這是按鈕唯一的名字。

  這樣在程序中可以通過如下形式獲得按鈕:

  button = (Button)findViewById(R.id.buttonId);

 

處理按鈕點擊

  按鈕點擊有兩種處理方法。

  第一種是通過onClick屬性,通過這個屬性設置處理點擊事件的方法名,在Activity中實現這個方法。

  另一種方法是典型的事件監聽機制的應用形式,下面詳細說明這兩種方法。

 

1.通過onClick屬性設置處理方法

  在XML布局文件中設置Button的屬性:

  android:onClick="yourMethodName"

  然后在該布局文件對應的Acitivity中實現該方法:

 

  /** Called when the user touches the button */

  public void yourMethodName(View view)

  {

       // Do something in response to button click

  }

 

  需要注意的是這個方法必須符合三個條件:

  1.public

  2.返回void

  3.只有一個參數View,這個View就是被點擊的這個控件。

 

2.使用setOnClickListener添加監聽器對象

  關於事件監聽模式,參見

  http://www.cnblogs.com/mengdd/archive/2012/09/08/2676587.html

 

  可以寫一個內部類實現OnClickListener接口,在這個類中實現onClick方法,方法里面寫在按鈕點擊時想做的具體工作。

  將這個內部類的對象傳入按鈕的setOnClickListener方法中,即完成監聽器對象和按鈕的綁定(在事件源Button上注冊了事件監聽器),這時候只要按鈕被點擊,那么監聽器對象的onClick方法就會被調用。

  當然這里也不一定要自己寫一個內部類出來,比如這個例子

 

  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

      }

  });

 

 

按鈕基本操作實例

  奉上實例一個:

  XML布局文件

XML布局文件
<RelativeLayout 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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:padding="@dimen/padding_medium"
        android:text="@string/hello_world"
        tools:context=".ButtonActivity" />

    <Button
        android:id="@+id/button_first"
        android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:text="@string/buttonText"
         android:onClick="changeButtonColor"
        >       
    </Button>    
    
    <Button
        android:id="@+id/button_second"
        android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:text="@string/buttonText2"
         android:layout_below="@id/button_first"
         
        >       
    </Button>

 </RelativeLayout>

 

  Activity代碼

Activity代碼
package com.example.buttontest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ButtonActivity extends Activity 
{
    private Button button01 = null;
    private Button button02 = null;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button);
        
        button01 = (Button)findViewById(R.id.button_first);
        button02 = (Button)findViewById(R.id.button_second);
       
        //綁定事件源和監聽器對象
        button02.setOnClickListener(new MyButtonListener());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.activity_button, menu);
        return true;
    }
    
    //按鈕1的點擊事件
    public void changeButtonColor(View view)
    {
        button01.setBackgroundColor(getResources().getColor(R.color.red));
        
    }
    
    //內部類,實現OnClickListener接口
    //作為第二個按鈕的監聽器類
    class MyButtonListener implements OnClickListener
    {

        public void onClick(View v)
        {
            button02.setBackgroundColor(getResources().getColor(R.color.blue));
            
        }
        
        
    }

    
}

 

  運行截圖                       

  點擊前:

 

 

 

  點擊后:

 

 

 

  相關的資源文件中內容

  strings.xml

strings.xml
<resources>

    <string name="app_name">ButtonTest</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_button">ButtonActivity</string>
    <string name="buttonText">ThisIsAButton</string>
    <string name="buttonText2">ThisIsAnotherButton</string>
</resources>

 

  colors.xml

colors.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <color name="red">#f00</color>
    <color name="green">#0f0</color>
    <color name="blue">#00f</color>
    <color name="black">#000</color>
</resources>

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM