`

APK开发示例---比赛简易计分器

阅读更多
  最近学习Android程序开发。突发奇想,琢磨了一下自身的定位:我应该是会Android开发的人里打羽毛球比较好的,会打羽毛球人里Android程序写得比较好的。
  比赛时,有时候记分不太方便,就自己边学边做,写了个极简单的“比赛简易计分器”程序。没有什么难度,把代码贴一下,大家自己看。

一、效果图




二、界面代码
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center|center_vertical"
        android:id="@+id/linearLayout"
        android:layout_toLeftOf="@+id/linearLayout3"
        android:layout_toStartOf="@+id/linearLayout3">

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="+"
            android:id="@+id/button"
            android:textSize="40dp"
            android:textStyle="bold"
            android:layout_alignBottom="@+id/textView3"
            android:layout_toLeftOf="@+id/textView3"
            android:layout_toStartOf="@+id/textView3"
            android:onClick="ScoreAPlus" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="01"
            android:id="@+id/tvScoreA"
            android:textSize="80dp"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/tvTo"
            android:layout_toStartOf="@+id/tvTo"
            android:textStyle="bold" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="-"
            android:id="@+id/button3"
            android:layout_below="@+id/tvTo"
            android:layout_toLeftOf="@+id/textView4"
            android:layout_toStartOf="@+id/textView4"
            android:textStyle="bold"
            android:textSize="40dp"
            android:onClick="ScoreAMinus" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center|center_vertical"
        android:id="@+id/linearLayout2"
        android:layout_toRightOf="@+id/linearLayout3"
        android:layout_toEndOf="@+id/linearLayout3">

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="+"
            android:id="@+id/button2"
            android:layout_alignBottom="@+id/textView3"
            android:layout_alignLeft="@+id/tvScoreB"
            android:layout_alignStart="@+id/tvScoreB"
            android:textSize="40dp"
            android:textStyle="bold"
            android:onClick="ScoreBPlus" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="02"
            android:id="@+id/tvScoreB"
            android:textSize="80dp"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/textView4"
            android:layout_toEndOf="@+id/textView4"
            android:textStyle="bold" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="-"
            android:id="@+id/button4"
            android:layout_below="@+id/tvTo"
            android:layout_alignLeft="@+id/tvScoreB"
            android:layout_alignStart="@+id/tvScoreB"
            android:textStyle="bold"
            android:textSize="40dp"
            android:onClick="ScoreBMinus" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center|center_vertical"
        android:id="@+id/linearLayout3"
        android:layout_centerHorizontal="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=":"
            android:id="@+id/tvTo"
            android:textSize="80dp"
            android:shadowColor="#474b49"
            android:textColor="#d11612"
            android:textStyle="bold"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
    </LinearLayout>

</RelativeLayout>


三、程序代码
package com.wallimn.iteye.ymq;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private TextView scoreA;
    private TextView scoreB;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scoreA = (TextView)this.findViewById(R.id.tvScoreA);
        scoreB = (TextView)this.findViewById(R.id.tvScoreB);

        scoreA.setText("00");
        scoreB.setText("00");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void NumberAdjust(TextView tv,int deta){
        String num = tv.getText().toString();
        int number = Integer.parseInt(num);
        if(number+deta<0)return;
        tv.setText(String.format("%02d", number+deta));
    }

    private long exitTime=0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(event.getAction()==KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK){
            if(System.currentTimeMillis()-exitTime>5000){
                Toast.makeText(this,"再按一次返回键程序!",Toast.LENGTH_LONG).show();
                exitTime = System.currentTimeMillis();
            }
            else{
                this.finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void ScoreAPlus(View source){
        this.NumberAdjust(scoreA, 1);
    }
    public void ScoreAMinus(View source){
        this.NumberAdjust(scoreA, -1);
    }
    public void ScoreBPlus(View source){
        this.NumberAdjust(scoreB, 1);
    }
    public void ScoreBMinus(View source){
        this.NumberAdjust(scoreB,-1);
    }
}



  apk程序下载,请猛戳附件(ymq-release.rar)。源码下载,请猛戳附件(ymq-src.rar)。


四、改进
  改成横屏显示,看起来舒服一些,增加了菜单,带了重置、退出功能。



  • 大小: 9.5 KB
  • 大小: 16.3 KB
1
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics