Android 一个有趣的小程序!!!看你能有多短!!!

闲来无事,写一个简单小Demo,挺有意思的,真的防老年痴呆啊! 因为… …

Android 一个有趣的小程序!!!看你能有多短!!!_第1张图片

package com.god.d_beer;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText main_ed;

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

        initview();


    }

    private void initview() {
        main_ed = (EditText) findViewById(R.id.main_ed);
    }

    private int beersNum;

    private int beerBottle;

    private int beerCap;
    private boolean ok = true;

    private void countBeers(int i) {
        beersNum = i / 2;
        beerBottle = i / 2;
        beerCap = i / 2;
        while (ok) {
            circulationBeers();
            if (beerBottle < 2 && beerCap < 4) {
                Toast.makeText(MainActivity.this, "酒鬼,你能买 " + beersNum + " 瓶啤酒!", Toast.LENGTH_SHORT).show();
                ok = false;
            }
        }
        ok = true;
    }

    private void circulationBeers() {
        int bottles = beerBottle / 2;
        int bottles1 = beerBottle % 2;
        int caps = beerCap / 4;
        int caps1 = beerCap % 4;
        //最新的数据
        beersNum = beersNum + bottles + caps;
        beerBottle = bottles + bottles1 + caps;
        beerCap = caps + caps1 + bottles;

    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_bt_refresh:
                String s = main_ed.getText().toString();
                if (s != null && !s.equals("")) {
                    countBeers(Integer.parseInt(s));
                }else {
                    Toast.makeText(MainActivity.this, "酒鬼,输入数字!!!", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.main_bt_reset:
                main_ed.getText().clear();
                ok = true;
                break;
        }
    }
}

布局文件

"1.0" encoding="utf-8"?>
"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.god.d_beer.MainActivity">

    "@+id/main_ed"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="200dp"
        android:layout_marginRight="200dp"
        android:layout_marginTop="80dp"
        android:hint="就问你腰包里多少钱"
        android:inputType="number"
        android:textColor="#f0001c"
        android:textColorHint="#00FF00"
        android:windowSoftInputMode="adjustUnspecified|stateHidden" />

    "@+id/main_aline"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerInParent="true" />

    

效果展示
Android 一个有趣的小程序!!!看你能有多短!!!_第2张图片

你可能感兴趣的:(Code趣事)