Android添加button並響應

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   使用   

1.首先要在activity_main.xml檔案裡建立兩個button標籤。

  

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" 6     android:paddingBottom="@dimen/activity_vertical_margin" 7     android:paddingLeft="@dimen/activity_horizontal_margin" 8     android:paddingRight="@dimen/activity_horizontal_margin" 9     android:paddingTop="@dimen/activity_vertical_margin"10     tools:context="com.fate.testproject.MainActivity" >11 12     <TextView13         android:layout_width="match_parent"14         android:layout_height="wrap_content"15         android:text="@string/total" 16         android:textSize="45sp"17         android:layout_gravity="center" 18         android:gravity="center"19         android:id="@+id/tvTotal"20         />21     22     <Button23         android:layout_width="250sp"24         android:layout_height="wrap_content"25         android:text="@string/addOne"26         android:layout_gravity="center"27         android:textSize="20sp"28         android:id="@+id/bAddOne"29         />30     <Button 31         android:layout_width="250sp"32         android:layout_height="wrap_content"33         android:text="@string/subOne"34         android:layout_gravity="center"35         android:textSize="20sp"36         android:id="@+id/bSubOne"37         />38 39 40 </LinearLayout>

這裡出現了幾個問題。

  1)布局問題:我現在用的Android環境是最新的,建立工程下activity_main.xml檔案內建布局並非LinearLayout線性布局。之前對照書上的內容跟網上視頻教程總感覺不對勁。之後,改成了LinearLayout線性布局之後,又發現幾個控制項擠成一橫排,控制項甚至被擠出視圖外面。原來線性布局預設的是橫向的,不是我們所認為的豎向。這裡添加一句

android:orientation="vertical",所有空間依次豎向排列。
  2)硬性編碼問題:在定義控制項的寬度(width)和高度(height)的時候我使用了數值。一般人現在都知道如果用像素dx作為單位來定義控制項的寬和高會出現warning的黃色警告。但是我在使用dp作為單位的時候也出現了warning,最後在提示下使用的是sp。還有,在定義Android:text的時候,不引用string下的元素而直接輸入內容也會出現warning警告。這種硬性編碼引起warning警告一般不會導致整個程式出錯,但是良好的習慣和編程思路會減少出錯的機率。
  3)我在設定id的時候出現了個小毛病,寫錯了,將android:id="@+id/bSubOne"寫成了android:id="@id/bSubOne"少了一個+號,導致程式完全運行不了,出現紅色警報。千萬細心。

2.其次在resources下的string.xml檔案裡添加元素
 1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3  4     <string name="app_name">TestProject</string> 5     <string name="hello_world">Hello world!</string> 6     <string name="action_settings">Settings</string> 7     <string name="total">總數為:0</string> 8     <string name="subOne">數值減一</string> 9     <string name="addOne">數值加一</string>10 11 </resources>

將button的部分資訊在這裡添加,更加方便修改和管理。如果需要使用,直接引用。

3.在MainActivity函數中添加響應函數

 1 package com.fate.testproject; 2  3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.Menu; 6 import android.view.MenuItem; 7 import android.view.View; 8 import android.widget.Button; 9 import android.widget.TextView;10 11 12 public class MainActivity extends Activity {13 14     int counter;//建立int變數counter15     Button mAddOne, mSubOne;//建立兩個button變數,分別是加一button和減一button16     TextView mTotal;//建立TextView類型的變數mTotal17     18     @Override19     protected void onCreate(Bundle savedInstanceState) {20         super.onCreate(savedInstanceState);21         setContentView(R.layout.activity_main);22         23         24         //初始設定變數的值25         counter = 0;26         27         //關聯兩個button和TextView變數28         mAddOne = (Button)findViewById(R.id.bAddOne);29         mSubOne = (Button)findViewById(R.id.bSubOne);30         mTotal = (TextView)findViewById(R.id.tvTotal);31         32         //添加按鈕單機響應函數33         mAddOne.setOnClickListener(new View.OnClickListener() {34             35             @Override36             public void onClick(View v) {37                 // TODO Auto-generated method stub38                 counter++;39                 mTotal.setText("總數為:" + counter);40             }41         });42         43         mSubOne.setOnClickListener(new View.OnClickListener() {44             45             @Override46             public void onClick(View v) {47                 // TODO Auto-generated method stub48                 counter--;49                 mTotal.setText("總數為:" + counter);50             }51         });52     }53 54 55     @Override56     public boolean onCreateOptionsMenu(Menu menu) {57         // Inflate the menu; this adds items to the action bar if it is present.58         getMenuInflater().inflate(R.menu.main, menu);59         return true;60     }61 62     @Override63     public boolean onOptionsItemSelected(MenuItem item) {64         // Handle action bar item clicks here. The action bar will65         // automatically handle clicks on the Home/Up button, so long66         // as you specify a parent activity in AndroidManifest.xml.67         int id = item.getItemId();68         if (id == R.id.action_settings) {69             return true;70         }71         return super.onOptionsItemSelected(item);72     }73 }

 

簡單的添加button只需要修改這三個工程下的檔案就可以了。

這裡我為了養成自己的習慣,讓我寫出來的代碼更具有易讀性。我在定義各個變數的時候是這樣的。

  1)遵循定義名有意義,基本代表控制項的內容或含義,並且盡量使用英文代表其含義。如:加一我會定義成addone

  2)一個控制項的id,name,或者其他函數相關關聯的定義會採用稍微不同的定義。如在string裡的string name一般為小寫字母開頭,之後的每個單詞首個大寫;設定id則在前面添加其標籤的縮寫字母(如,button標籤下的就在string name 前面添加字母b,並將b後第一個字母大寫,TextView標籤下則添加字母tv,並將tv後第一個字母大寫);在其他函數如MainActivity函數中使用相關聯的變數時,則在string name前面添加字母m,並將m後第一個字母大寫。

  執行個體:建立一個加一按鈕則string name就是addOne,id就是bAddOne,主函數相關聯的函數變數就是mAddOne。

  3)注意分段,分行,讓整個代碼看起來有層次。方便閱讀,方便修改。也能讓自己寫代碼的時候心情更好,因為代碼不可能是一次性寫好寫完,總要回頭來修改查看,如果沒有好的層次感,很難在看自己寫的代碼的時候有好心情,這不但影響工作和學習,還打擊我們對開發的熱情。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.