顯示具有 button 標籤的文章。 顯示所有文章
顯示具有 button 標籤的文章。 顯示所有文章

2012年11月16日 星期五

監聽你的Button或界面(On Click)

當我們在程式中的變數與界面中連結後,在界面中的所有動作可以
適當的傳給程式,然後由程式去判斷要做什麼動作,但通常只會
做一次,除非我們有設定監聽器,才會針對每次的變化去做處理
最常見的是Button,它按下後我們會看見圖案的變化,但實際上,
每個物件都可以設變化
下面的例子將簡單說明監聽器的使用方法

//直接在這個活動中植入監聽器(implement OnClickListener)
public class MainActivity extends Activity implements OnClickListener {
    TextView tv1, tv2;
    Button bt1;
    View lay1;
      @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
         tv1 = (TextView) findViewById(R.id.textView1);
           bt1 = (Button) findViewById(R.id.button1);
        /*
         * this代表這個activity的內容 setOnClickListenr代表設定監聽器
         * 而連結的監聽器已用implements內建在Activity之中
         */

         //將bt1這個變數設監聽器,只要界面中與它連結的button被按下,就會反應
        bt1.setOnClickListener(MainActivity.this);
         //將tv1這個變數設監聽器,只要界面中與它連結的textview被按下,就會反應
         tv1.setOnClickListener(this);
        // 指定介面的layout與lay1連結,並設定監聽器Listener     
        lay1 = (View) findViewById(R.id.lay1);
        lay1.setOnClickListener(this);
        }

    // 每個監聽器都必須有OnClick來指定後續的動作View則代表所要監聽的視
    public void onClick(View v) {
        // if 如果符合條件,則做if裡面的動作,只做一次
        //bt1==v表示如果視窗中按下的是button
        if (bt1 == v) {
            //文字的內容顯示為"TEST BUTTON", 而文字顏色為紅色
            tv1.setText("TEST BUTTON");
            tv1.setTextColor(Color.RED);
        }
        //tv1==v表示如果視窗按下的是texeview
        if (tv1 == v) {
             //文字的顏色為綠色,並且將string中的文字讀取出來
                tv1.setText(R.string/TextViewShow);
                tv1.setTextColor(Color.GREEN);
                 }
          //lay1==v表示如果視窗按下的是layout而不是button或textview
        if (lay1 == v) {
           //文字的顏色為灰色,並且顯示的內容為"TEST VIEW"
            tv1.setText("TEST VIEW");
            tv1.setTextColor(Color.GRAY);
        }
    }

Layout的基本設計

預設的專案中,android已給定一個Relative的界面
上面這個界面很簡單,而裡面包含了3個物件~layout、textview、button
由這個結構可看出RelativeLayout包含了2個物件TextView與Button在裡面
結構簡單表示:
    <RelativeLayout
          <TextView     />
          <Button         />
     />

以下為它的xml源碼
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    (給定layout一個名稱lay1,而自訂id一定要用@+id/名稱)
    android:id="@+id/lay1"  
    (設定它的寬度 match_parent表示符合父層的寬)

    android:layout_width="match_parent"  
    (設定它的高度 match_parent表示符合父層的高)
    android:layout_height="match_parent" >   

        <TextView
         (給定textview一個名稱textView1,而自訂id一定要用@+id/名稱)
        android:id="@+id/textView1"
         (設定它的寬度 wrap_content表示符合輸入內容的寬)
        android:layout_width="wrap_content"
           (設定它的高度 wrap_content表示符合輸入內容的高)
        android:layout_height="wrap_content"
         (alignParentTop代表要以父層的最高處為參考線)
        android:layout_alignParentTop="true"
           (centerHorizontal代表是否要在水平的中央處)
        android:layout_centerHorizontal="true"
            (marginTop代表離上邊多遠處,48dp代表距離)
        android:layout_marginTop="48dp"
             (文字內容可直接寫入,要代入字串則用@string/字串名稱)
        android:text="@string/TextView"
             (文字的外觀:下面是android預設的字形(大的))
        android:textAppearance="?android:attr/textAppearanceLarge"
        />

       <Button
          (給定textview一個名稱Button1,而自訂id一定要用@+id/名稱)
        android:id="@+id/button1"
         (設定它的寬度 wrap_content表示符合輸入內容的寬)
        android:layout_width="wrap_content"
           (設定它的高度 match_parent表示符合父層的高)
        android:layout_height="wrap_content"
         (alignParentBottom代表要以父層的最底處為參考線)
        android:layout_alignParentBottom="true"
         (alignParentLeft代表要以父層的左邊為參考線)
        android:layout_alignParentLeft="true"
         (marginBottom表示要離底部多遠)
        android:layout_marginBottom="136dp"
         (marginLeft表示要離左邊多遠)
        android:layout_marginLeft="37dp"
        android:text="@string/Button"
         />

</RelativeLayout>