最近参加了Google Study Jams这个在线课程学习,这是第sp2练习的代码,制作一个计分器的小app,写完这个小项目后我也想了很多东西,因为平常经常打球,但是有时也没有记分牌,还是很麻烦,所以这个app还是可以解决一些比赛时的使用,准备把这个项目再丰富一下,所以暂时先贴出根据他视频所学的项目,以后添加了之后再继续补充
先是xml布局文件:
这个文件布局稍微复杂了一点,日后再改进一下,不过大家也可以看一下我的思路,如果有更好的建议也可以跟我提
version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
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.example.zhouzhongyi.match.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<LinearLayout
android:id="@+id/lin1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="输入主队名"
android:textSize="30dp"
android:layout_marginTop="20dp"
/>
<TextView
android:id="@+id/teamas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="100dp"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+3 point"
android:onClick="displaya3"
android:layout_margin="20dp"
android:background="@color/colorPrimary"
android:textColor="@color/colorAccent"/>
<Button
android:id="@+id/button2"
android:onClick="displaya2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+2 point"
android:layout_margin="20dp"
android:background="@color/colorPrimary"
android:textColor="@color/colorAccent"/>
<Button
android:id="@+id/button3"
android:onClick="displaya1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+free throw"
android:layout_margin="20dp"
android:background="@color/colorPrimary"
android:textColor="@color/colorAccent"/>
LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="vertical">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="输入客队名"
android:textSize="30dp"
android:layout_marginTop="20dp"
/>
<TextView
android:id="@+id/teambs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="100dp"
/>
<Button
android:id="@+id/button4"
android:onClick="displayb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3 pront"
android:layout_margin="20dp"
android:background="@color/colorPrimary"
android:textColor="@color/colorAccent"/>
<Button
android:id="@+id/button5"
android:onClick="displayb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+2 point"
android:layout_margin="20dp"
android:background="@color/colorPrimary"
android:textColor="@color/colorAccent"/>
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+free throw"
android:onClick="displayb1"
android:layout_margin="20dp"
android:background="@color/colorPrimary"
android:textColor="@color/colorAccent"/>
LinearLayout>
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_weight="0.5"
android:gravity="center_horizontal">
<Button
android:id="@+id/btReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Reset"
android:onClick="resetScore"
android:background="@color/colorPrimary"
android:textColor="@color/colorAccent" />
LinearLayout>
LinearLayout>
然后是java代码:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView teamas,teambs;
int scoreTeamA = 0;
int scoreTeamB = 0;
private Button button1,button2,button3,button4,button5,button6,restart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
teamas = (TextView) findViewById(R.id.teamas);
teambs = (TextView) findViewById(R.id.teambs);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
restart = (Button) findViewById(R.id.btReset);
}
public void resetScore(View view){
scoreTeamA = 0;
scoreTeamB = 0;
displayForTeamA(scoreTeamA);
displayForTeamB(scoreTeamB);
}
/**
* Displays the given score for Team A.
*/
public void displayForTeamA(int score) {
teamas.setText(String.valueOf(score));
}
/**
* Displays the given score for Team B.
*/
public void displayForTeamB(int score) {
teambs.setText(String.valueOf(score));
}
/**
* Displays the +3 score for Team A.
*/
public void displaya3(View view){
scoreTeamA = scoreTeamA + 3;
displayForTeamA(scoreTeamA);
}
/**
* Displays the +2 score for Team A.
*/
public void displaya2(View view){
scoreTeamA = scoreTeamA + 2;
displayForTeamA(scoreTeamA);
}
/**
* Displays the +3 score for Team A.
*/
public void displaya1(View view){
scoreTeamA = scoreTeamA + 1;
displayForTeamA(scoreTeamA);
}
/**
* Displays the +3 score for Team B.
*/
public void displayb3(View view){
scoreTeamB = scoreTeamB + 3;
displayForTeamB(scoreTeamB);
}
/**
* Displays the +2 score for Team B.
*/
public void displayb2(View view){
scoreTeamB = scoreTeamB + 2;
displayForTeamB(scoreTeamB);
}
/**
* Displays the +1 score for Team B.
*/
public void displayb1(View view){
scoreTeamB = scoreTeamB + 1;
displayForTeamB(scoreTeamB);
}
}
这一段是java代码,感觉废代码多了点,日后做优化,并且肯定会再丰富功能,如果大家有什么想法也可以和我交流,大家一起学习,这就算是个我自己做的笔记吧
这是这个项目暂时的项目效果,很简单
下面贴上项目地址,大家也可以自己去下载下来看一看,或者还有在进行这个学习的新手也可以看一看,说不定会帮助到你们
项目地址:https://github.com/zzyai3/Match/tree/master