标签:throw state alt ntb done value none font tla
描述:做一个如图所示的篮球比赛记分板。
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <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="horizontal"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="4dp" android:text="Team A" android:textSize="14sp" android:textColor="#616161" android:fontFamily="sans-serif-medium"/> <TextView android:id="@+id/team_a_score" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="4dp" android:text="0" android:textSize="56sp" android:textColor="#000000" android:fontFamily="sans-serif-light"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:onClick="addThreeForTeamA" android:text="+3 Points" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:onClick="addTwoForTeamA" android:text="+2 Points" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:onClick="addOneForTeamA" android:text="Free throw" /> </LinearLayout> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="@android:color/darker_gray"/> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="4dp" android:text="Team B" android:textSize="14sp" android:textColor="#616161" android:fontFamily="sans-serif-medium"/> <TextView android:id="@+id/team_b_score" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="4dp" android:text="0" android:textSize="56sp" android:textColor="#000000" android:fontFamily="sans-serif-light"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:onClick="addThreeForTeamB" android:text="+3 Points" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:onClick="addTwoForTeamB" android:text="+2 Points" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:onClick="addOneForTeamB" android:text="Free throw" /> </LinearLayout> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerInParent="true" android:padding="4dp" android:text="RESET" android:onClick="resetScore"/> </RelativeLayout> </android.support.constraint.ConstraintLayout>
package com.learn.nianhao.courtcount; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { int scoreTeamA=0; int scoreTeamB=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void addThreeForTeamA(View view) { scoreTeamA+=3; displayForTeamA(scoreTeamA); } public void addTwoForTeamA(View view) { scoreTeamA+=2; displayForTeamA(scoreTeamA); } public void addOneForTeamA(View view) { scoreTeamA+=1; displayForTeamA(scoreTeamA); } public void displayForTeamA(int score) { TextView scoreView = (TextView) findViewById(R.id.team_a_score); scoreView.setText(String.valueOf(score)); } public void addOneForTeamB(View view) { scoreTeamB+=1; displayForTeamB(scoreTeamB); } public void addTwoForTeamB(View view) { scoreTeamB+=2; displayForTeamB(scoreTeamB); } public void addThreeForTeamB(View view) { scoreTeamB+=3; displayForTeamB(scoreTeamB); } public void displayForTeamB(int score) { TextView scoreView = (TextView) findViewById(R.id.team_b_score); scoreView.setText(String.valueOf(score)); } public void resetScore(View view) { scoreTeamB=0; scoreTeamA=0; displayForTeamA(0); displayForTeamB(0); } }
标签:throw state alt ntb done value none font tla
原文地址:https://www.cnblogs.com/superxuezhazha/p/9426838.html