官术网_书友最值得收藏!

實例2 最常用的線性布局

在本小節中,通過LinearLayout線性布局的應用,構建了一個小型的計算器界面。該計算器實現了整數間的加減乘除四則運算,并通過本計算器的實現,向讀者介紹LinearLayout線性布局的具體應用。

【實例描述】

在本小節中實現了小型計算器軟件的開發。該計算器通過0~9這10個數字按鈕,“加”、“減”、“乘”、“除”和“等于”5個運算按鈕,以及清空按鈕來實現對本程序的操控,并通過一個文本框來顯示計算的結果。應用本程序可以進行整數間簡單的加、減、乘、除四則運算。

本實例的運行效果圖,如圖2-2所示。

圖2-2 小型計算器

提示:使用者可以通過單擊數字輸入第一個計算值,然后單擊所需計算按鈕確定計算方式,再通過單擊數字輸入第二個計算值,之后當再次單擊“等于”按鈕時,軟件會自動計算出結果數值,如圖2-2所示。

【實現過程】

小型計算器的開發,主要運用了LinearLayout線性布局的相關知識。線性布局是最簡單的布局之一,其提供了控件水平或者垂直排列的模型。同時,使用此布局時可以通過設置控件的weight參數控制各個控件在容器中的相對大小。

提示:線性布局垂直時占用一列,水平時占用一行。特別要注意的是,水平時若超過一行則不會像SE中的FlowLayout一樣自動轉行。

線性布局中可以使用gravity屬性設置控件的對齊方式,詳情如表2.1所列。

表2.1 線性布局gravity屬性表

提示:當需要使用多個屬性時,用“|”分隔開即可。

【代碼解析】

首先為讀者介紹的是小型計算器的主界面mian.xml的開發,代碼如下。

代碼位置:見隨書光盤中源代碼/第2章/Sample2_2/ res/layout目錄下的main.xml。

    1   <?xml version="1.0" encoding="utf-8"?>              <!--版本號和編碼方式-->
    2   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3      android:orientation="vertical" android:layout_width="fill_parent"
    4      android:layout_height="fill_parent" android:paddingTop="5dip">
    5      <TextView
    6        android:id="@+id/tv" android:layout_width="fill_parent"
    7        android:layout_height="40dip" android:layout_marginRight="5dip"
    8        android:layout_marginLeft="5dip" android:background="#FFFFFF"
    9        android:gravity="center_vertical|right" android:textSize="30dip"
    10       android:textColor="#ff0000">                    <!--設置顏色-->
    11     </TextView>
    12     <LinearLayout
    13       android:orientation="horizontal" android:layout_width="fill_parent"
    14       android:layout_height="wrap_content" android:paddingTop="5dip">
                                                            <!--距上面的距離-->
    15          <Button
    16           android:text="7"  android:id="@+id/Button07"
    17           android:layout_width="80dip"android:layout_height="wrap_content"/>
    18         <Button
    19           android:text="8"  android:id="@+id/Button08"
    20           android:layout_width="80dip"android:layout_height="wrap_content"/>
    21          <Button
    22           android:text="9"  android:id="@+id/Button09"
    23           android:layout_width="80dip"android:layout_height="wrap_content"/>
    24          <Button
    25           android:text="+"  android:id="@+id/ButtonJia"
    26           android:layout_width="80dip"android:layout_height="wrap_content"/>
    27     </LinearLayout>
    28     <LinearLayout
    29       android:orientation="horizontal" android:layout_width="fill_parent"
    30       android:layout_height="wrap_content" android:paddingTop="5dip">
    31          <Button
    32           android:text="4"  android:id="@+id/Button04"
    33           android:layout_width="80dip"android:layout_height="wrap_content"/>
    34         <Button
    35           android:text="5"  android:id="@+id/Button05"
    36           android:layout_width="80dip"android:layout_height="wrap_content"/>
    37          <Button
    38           android:text="6"  android:id="@+id/Button06"
    39           android:layout_width="80dip"android:layout_height="wrap_content"/>
    40          <Button
    41           android:text="-"  android:id="@+id/ButtonJian"
    42           android:layout_width="80dip"android:layout_height="wrap_content"/>
    43     </LinearLayout>
                                                      <!--LinearLayout布局-->
    44     <LinearLayout
    45       android:orientation="horizontal" android:layout_width="fill_parent"
    46       android:layout_height="wrap_content" android:paddingTop="5dip">
    47          <Button
    48           android:text="1"  android:id="@+id/Button01"
    49           android:layout_width="80dip"android:layout_height="wrap_content"/>
    50         <Button
    51           android:text="2"   android:id="@+id/Button02"  android:layout_width="80dip"
    52           android:layout_height="wrap_content"/>
    53          <Button
    54           android:text="3"  android:id="@+id/Button03"
    55           android:layout_width="80dip"android:layout_height="wrap_content"/>
    56          <Button
    57           android:text="*"  android:id="@+id/ButtonCheng"
    58           android:layout_width="80dip"android:layout_height="wrap_content"/>
    59     </LinearLayout>                                  <!--LinearLayout布局-->
    60     <LinearLayout
    61       android:orientation="horizontal" android:layout_width="fill_parent"
    62       android:layout_height="wrap_content" android:paddingTop="5dip">
    63          <Button
    64           android:text="0"  android:id="@+id/Button00"
    65           android:layout_width="80dip"android:layout_height="wrap_content"/>
    66         <Button
    67           android:text="c"  android:id="@+id/ButtonC"
    68           android:layout_width="80dip"android:layout_height="wrap_content"/>
    69          <Button
    70           android:text="="  android:id="@+id/ButtonDengyu"
    71           android:layout_width="80dip"android:layout_height="wrap_content"/>
    72          <Button
    73           android:text="/"  android:id="@+id/ButtonChu"
    74           android:layout_width="80dip"android:layout_height="wrap_content"/>
    75     </LinearLayout>                                  <!--LinearLayout布局-->
    76  </LinearLayout>

提示:在該xml文件中實現了布局的設置。總的LinearLayout為垂直排列模式,其中垂直排列了四個子LinearLayout,并且每個子LinearLayout為水平排列模式,在每個子LinearLayout中水平排列了4個Button按鈕控件。通過對各個控件的屬性設置,實現了小型計算器的界面效果。

上面已經介紹了本程序主界面main.xml的開發,接下來將要為讀者介紹該小型計算器的功能的實現,代碼如下。

代碼位置:見隨書光盤中源代碼/第2章/Sample2_2/src/com/bn/es2b目錄下的Sample2_2_Activity.class。

    1   package com.bn.es2b;                                //聲明包
    2   ......//該處省略了部分類的導入,讀者可自行查看隨書光盤中源代碼
    3   import android.view.View.OnClickListener;           //導入相關類
    4   public class Sample2_2_Activity extends Activity{   //創建繼承Activity的類
    5        ......//該處省略了成員變量的代碼,讀者可自行查看隨書光盤中源代碼
    6        public void onCreate(Bundle savedInstanceState){    //繼承必須重寫的方法
    7          super.onCreate(savedInstanceState);               //調用父類
    8          setContentView(R.layout.main);                    //跳轉到main界面
    9          initButton();                                     //初始化控件
    10         buttonC.setOnClickListener(                  //清空按鈕的單擊事件監聽器
    11             new OnClickListener(){
    12                  public void onClick(View v){        //重寫的onClick方法
    13                        str1="";str2="";              //設定str1與str2的值
    14                        tv.setText(str1);             //清空記錄
    15                        flag=0;                       //標志為0
    16           } });
    17         for(int i=0;i<buttons.length;i++){            //for循環
    18           temp=(Button)findViewById(buttons[i]);     //初始化按鈕
    19           temp.setOnClickListener(                  //為Button添加監聽器
    20               new OnClickListener(){                 //內部類
    21                    @Override
    22                    public void onClick(View v){       //重寫的方法
    23                        str1=tv.getText().toString().trim();
                                                            //得到TextView中的字符串
    24
    str1=str1+String.valueOf(((Button)v).getText());//獲得新輸入的值
    25                            tv.setText(str1);         //設置TextView
    26           } }); }
    27          buttonListener(buttonJia,1);                //加號的監聽器
    28          buttonListener(buttonJian,2);               //減號的監聽器
    29          buttonListener(buttonCheng,3);              //乘號的監聽器
    30          buttonListener(buttonChu,4);                //除法的監聽器
    31         buttonDengyu.setOnClickListener(             //等號的監聽器
    32           new OnClickListener(){                     //內部類
    33               @Override
    34               public void onClick(View v){           //重寫的方法
    35                  result1=Integer.parseInt(str1);     //字符串轉換為int類型
    36                    if(flag==1){                       //判斷flag是否為1
    37                    result=result0+result1;            //實現加法
    38                    System.out.println(result0+":"+result1);
    39                 }else if(flag==2){                   //判斷flag是否為2
    40                    result=result0-result1;            //實現減法
    41                 }else if(flag==3){                   //判斷flag是否為3
    42                    result=result0*result1;            //實現乘法
    43                 }else if(flag==4){                   //判斷flag是否為4
    44                    result=(int)(result0/result1);     //實現除法
    45                 }
    46                    String str=(result+"").trim();     //運算結果轉換為字符串
    47                    tv.setText(str);                   //設置TextView的顯示值
    48  } }); }
    49     public void initButton(){                        //初始化控件資源
    50       ......//初始化控件的方法,讀者可自行查看隨書光盤中源代碼
    51     }
    52     public void buttonListener(Button button,final int id){
                                                            //對按鈕實現監聽的方法
    53       button.setOnClickListener(                     //設置監聽
    54           new OnClickListener(){
    55               public void onClick(View v){           //重寫onClick方法
    56                    String str=tv.getText().toString().trim();
                                                            //得到TextView的字符串
    57                    result0=Integer.parseInt(str);     //轉換為整型
    58                    tv.setText("");                    //清空
    59                    flag=id;                           //設定flag的值
    60  } }); } }

其中:

● 第10~16行表示的是對計算器清空按鈕的監聽,并且實現清空功能。

● 第17~26行表示的是對計算器九個值的按鍵進行監聽。

● 第31~48行表示的是對計算器等號的監聽,并且實現基本的四則運算。

● 第52~60行表示的是對計算器運算符按鈕的監聽。

主站蜘蛛池模板: 若羌县| 类乌齐县| 柳林县| 成安县| 长泰县| 枞阳县| 溧阳市| 永康市| 普陀区| 客服| 泸定县| 通江县| 伊川县| 栾城县| 张掖市| 长岛县| 无锡市| 九江市| 贡山| 湘阴县| 鸡泽县| 乌拉特后旗| 厦门市| 镇坪县| 密山市| 华容县| 临湘市| 眉山市| 安新县| 都昌县| 项城市| 开封县| 杭锦旗| 福清市| 昌邑市| 北票市| 嘉兴市| 延吉市| 上犹县| 永定县| 平和县|