今天完成的任務(wù)
- 目標(biāo)是創(chuàng)建一款 計(jì)分器應(yīng)用哨啃,使用戶能夠記錄兩支不同球隊(duì)之間的比賽得分
- git把項(xiàng)目提交到GitHub
1.作品展示
2017-03-08_115307.png
2017-03-08_115307.png
2.需要掌握的知識(shí)
- 向應(yīng)用中添加按鈕代碼攻走,設(shè)置點(diǎn)擊事件
- 更新視圖
- 正確設(shè)定變量范圍
- 通過(guò) ID 查找視圖
3.知識(shí)詳解
1.頁(yè)面布局思路:采用相對(duì)布局和線性布局的混合使用;最外層是RelativeLayout ,包含了1個(gè)LinearLayout布局和1個(gè)reset按鈕款咖,其中這個(gè)線性布局中包含了2個(gè)等分的LinearLayout交掏,然后每個(gè)小的LinearLayout中都有2個(gè)TextView和3個(gè)Button匾委。
2.對(duì)于線性布局中的等劃分理解需要用到layout_weight
屬性让虐,是指剩余空間的平均分配,參考如下:LinearLayout布局中Layout_weight的深刻理解
3.設(shè)置button點(diǎn)擊事件黑滴,這里是在XML中button加一個(gè)onclick屬性憨募,然后在Java代碼中直接使用觸發(fā)事件,先初始化兩隊(duì)分?jǐn)?shù)為0跷跪,然后獲取scoreTextView 的id,點(diǎn)擊后進(jìn)行加分?jǐn)?shù)馋嗜,然后顯示分?jǐn)?shù)即可
4.git提交命令:
1.使用git bash創(chuàng)建一個(gè)文件夾cd g:
2.初始化代碼倉(cāng)庫(kù) git init
3.添加一個(gè)文件到倉(cāng)庫(kù)的緩存區(qū)(從工作目錄添加到緩存區(qū))git add filename
4.將添加的文件從緩存區(qū)提交到HEADgit commit -m "代碼提交信息"
5.將本地代碼倉(cāng)庫(kù)中代碼push到gitHub上
git remote add origin git@github.com:ganwenkai/scoreCounter.git
How to use git
從0開(kāi)始學(xué)習(xí) GITHUB 系列之「向GITHUB 提交代碼」
5.項(xiàng)目代碼
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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.kevin.scorecount.MainActivity">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/teamA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="8dp"
android:textSize="16sp"
android:textColor="#616161"
android:fontFamily="sans-serif-medium"
android:text="公牛隊(duì)" />
<TextView
android:id="@+id/teamA_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="8dp"
android:textSize="56sp"
android:textColor="#000000"
android:text="0"/>
<Button
android:id="@+id/AddTeamA3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="teamAThreePoint"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="8dp"
android:text="+3 points"/>
<Button
android:id="@+id/AddTeamA2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="teamATwoPoint"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="8dp"
android:text="+2 points"/>
<Button
android:id="@+id/AddTeamA1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="teamAOnePoint"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="8dp"
android:text="Free throw"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/teamB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="8dp"
android:textSize="16sp"
android:textColor="#616161"
android:fontFamily="sans-serif-medium"
android:text="湖人隊(duì)" />
<TextView
android:id="@+id/teamB_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="8dp"
android:textSize="56sp"
android:textColor="#000000"
android:text="0"/>
<Button
android:id="@+id/AddTeamB3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="teamBThreePoint"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="8dp"
android:text="+3 points"/>
<Button
android:id="@+id/AddTeamB2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="teamBTwoPoint"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="8dp"
android:text="+2 points"/>
<Button
android:id="@+id/AddTeamB1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="teamBOnePoint"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="8dp"
android:text="Free throw"/>
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
android:onClick="reset"
android:text="Reset"/>
</RelativeLayout>
Java代碼:
public class MainActivity extends AppCompatActivity {
//初始化AB兩隊(duì)的得分
int scoreTeamA = 0;
int scoreTeamB = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayForTeamA(scoreTeamA);
displayForTeamB(scoreTeamB);
}
//進(jìn)行A隊(duì)的加分情況先展示后邏輯處理
public void displayForTeamA(int score){
TextView teamAScore = (TextView)findViewById(R.id.teamA_score);
teamAScore.setText(String.valueOf(score));
}
//進(jìn)行對(duì)A隊(duì)的加3分
public void teamAThreePoint(View view){
scoreTeamA +=3;
displayForTeamA(scoreTeamA);
}
//對(duì)A隊(duì)加2分
public void teamATwoPoint(View view){
scoreTeamA +=2;
displayForTeamA(scoreTeamA);
}
//對(duì)A隊(duì)加1分
public void teamAOnePoint(View view){
scoreTeamA +=1;
displayForTeamA(scoreTeamA);
}
//進(jìn)行B隊(duì)的加分情況先展示后邏輯處理
public void displayForTeamB(int score){
TextView teamAScore = (TextView)findViewById(R.id.teamB_score);
teamAScore.setText(String.valueOf(score));
}
//進(jìn)行對(duì)B隊(duì)的加3分
public void teamBThreePoint(View view){
scoreTeamB +=3;
displayForTeamB(scoreTeamB);
}
//對(duì)B隊(duì)加2分
public void teamBTwoPoint(View view){
scoreTeamB +=2;
displayForTeamB(scoreTeamB);
}
//對(duì)B隊(duì)加1分
public void teamBOnePoint(View view){
scoreTeamB +=1;
displayForTeamB(scoreTeamB);
}
//進(jìn)行對(duì)所有的結(jié)果重置
public void reset(View view){
displayForTeamA(0);
displayForTeamB(0);
scoreTeamA = 0;
scoreTeamB = 0;
}
}
4.反思總結(jié)
- 學(xué)習(xí)過(guò)程中不需要過(guò)于去搜索和找各種資源,好像自己找到這些東西就是自己的了吵瞻,浪費(fèi)了不少時(shí)間葛菇,先試試看哪里不會(huì)再對(duì)比下效率高些。
- 對(duì)于常見(jiàn)的git切換命令不熟悉橡羞,剛開(kāi)始吧眯停,得加強(qiáng)加強(qiáng)。
- 注意自己的黃金高效學(xué)習(xí)時(shí)間卿泽,一般覺(jué)得早上那2小時(shí)很幾種莺债,看看自己哪個(gè)時(shí)間段適合做什么事滋觉,感覺(jué)最近要廢了,好久都木有運(yùn)動(dòng)啦齐邦。(PS:在真人圖書(shū)館遇到的那個(gè)分享故事的妹紙沒(méi)有勇敢的加她微信椎侠,好后悔哎呀!)