Androidの学習をはじめて4日くらいの人の問題集

Android

LinearLayout,SeekBar,RadioGroup等。

Q1
割り勘電卓を作成せよ。男女などで支払い額が変えられるようにする。
レイアウトのルートにはLinearLayoutを用いること。

[実行例]

スタート画面

女子は2割引きなどにも対応。また単位を選べる。

甲のみの入力にも対応

●activity_main.xml

001<?xml version="1.0" encoding="utf-8"?>
002<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
003    android:layout_width="match_parent"
004    android:layout_height="match_parent"
005    android:orientation="vertical"
006    android:padding="14dp">
007 
008    <LinearLayout
009        android:layout_width="match_parent"
010        android:layout_height="55dp"
011        android:orientation="horizontal">
012 
013        <TextView
014            android:id="@+id/textView10"
015            android:layout_width="wrap_content"
016            android:layout_height="wrap_content"
017            android:layout_weight="1"
018            android:text="合計金額" />
019 
020        <EditText
021            android:id="@+id/etSum"
022            android:layout_width="wrap_content"
023            android:layout_height="wrap_content"
024            android:layout_weight="1"
025            android:ems="10"
026            android:inputType="number" />
027 
028    </LinearLayout>
029 
030    <LinearLayout
031        android:layout_width="match_parent"
032        android:layout_height="52dp"
033        android:orientation="horizontal">
034 
035        <TextView
036 
037            android:layout_width="wrap_content"
038            android:layout_height="wrap_content"
039            android:layout_weight="1"
040            android:text="甲の人数" />
041 
042        <EditText
043            android:id="@+id/etA"
044            android:layout_width="wrap_content"
045            android:layout_height="wrap_content"
046            android:layout_weight="1"
047            android:ems="5"
048            android:inputType="number" />
049 
050    </LinearLayout>
051 
052    <LinearLayout
053        android:layout_width="match_parent"
054        android:layout_height="52dp"
055        android:orientation="horizontal">
056 
057        <TextView
058            android:id="@+id/textView4"
059            android:layout_width="wrap_content"
060            android:layout_height="wrap_content"
061            android:layout_weight="1"
062            android:text="乙の人数" />
063 
064        <EditText
065            android:id="@+id/etB"
066            android:layout_width="wrap_content"
067            android:layout_height="wrap_content"
068            android:layout_weight="1"
069            android:ems="5"
070            android:inputType="number" />
071 
072 
073    </LinearLayout>
074 
075    <LinearLayout
076        android:layout_width="match_parent"
077        android:layout_height="31dp"
078        android:orientation="horizontal">
079 
080        <TextView
081            android:id="@+id/textView5"
082            android:layout_width="wrap_content"
083            android:layout_height="wrap_content"
084            android:text="甲に対する乙の割合:"
085            android:textSize="16sp" />
086 
087        <TextView
088            android:id="@+id/tvRatio"
089            android:layout_width="wrap_content"
090            android:layout_height="wrap_content"
091 
092            android:text="100%"
093            android:textSize="16sp" />
094 
095    </LinearLayout>
096 
097    <SeekBar
098        android:id="@+id/sb"
099        android:layout_width="match_parent"
100        android:layout_height="wrap_content"
101        android:layout_marginBottom="10dp"
102        android:max="200"
103        android:paddingBottom="10dp"
104        android:paddingTop="10dp"
105        android:progress="100" />
106 
107    <TextView
108        android:id="@+id/textView7"
109        android:layout_width="match_parent"
110        android:layout_height="wrap_content"
111        android:text="単位" />
112 
113    <RadioGroup
114        android:id="@+id/rg"
115        android:layout_width="match_parent"
116        android:layout_height="43dp"
117        android:orientation="horizontal">
118 
119        <RadioButton
120            android:id="@+id/rb1"
121            android:layout_width="wrap_content"
122            android:layout_height="wrap_content"
123            android:layout_weight="1"
124            android:checked="true"
125            android:text="1円" />
126 
127        <RadioButton
128            android:id="@+id/rb2"
129            android:layout_width="wrap_content"
130            android:layout_height="wrap_content"
131            android:layout_weight="1"
132            android:text="10円" />
133 
134        <RadioButton
135            android:id="@+id/rb3"
136            android:layout_width="wrap_content"
137            android:layout_height="wrap_content"
138            android:layout_weight="1"
139            android:text="100円" />
140    </RadioGroup>
141 
142    <Button
143        android:id="@+id/button"
144        android:layout_width="match_parent"
145        android:layout_height="wrap_content"
146        android:onClick="btCalc"
147        android:text="計算" />
148 
149    <TextView
150        android:id="@+id/tvResult"
151        android:layout_width="match_parent"
152        android:layout_height="wrap_content"
153        android:layout_marginTop="10dp"
154        android:textSize="18sp" />
155</LinearLayout>

●MainActivity.java

01import android.os.Bundle;
02import android.support.v7.app.AppCompatActivity;
03import android.view.View;
04import android.widget.EditText;
05import android.widget.RadioGroup;
06import android.widget.SeekBar;
07import android.widget.TextView;
08import android.widget.Toast;
09 
10import java.util.InputMismatchException;
11 
12public class MainActivity extends AppCompatActivity {
13    private EditText etSum, etA, etB;
14    private TextView tvRatio, tvResult;
15    private SeekBar sb;
16    private RadioGroup rg;
17 
18    @Override
19    protected void onCreate(Bundle savedInstanceState) {
20        super.onCreate(savedInstanceState);
21        setContentView(R.layout.activity_main);
22         
23        etSum = (EditText) findViewById(R.id.etSum);
24        etA = (EditText) findViewById(R.id.etA);
25        etB = (EditText) findViewById(R.id.etB);
26        tvRatio = (TextView) findViewById(R.id.tvRatio);
27        tvResult = (TextView) findViewById(R.id.tvResult);
28        rg = (RadioGroup) findViewById(R.id.rg);
29        sb = (SeekBar) findViewById(R.id.sb);
30         
31 
32        //Seekbarにチェンジリスナーを設定し、リアルタイムでTextViewに値を表示する。
33        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
34            @Override
35            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
36                tvRatio.setText(progress + "%");
37            }
38 
39            @Override
40            public void onStartTrackingTouch(SeekBar seekBar) {}
41 
42            @Override
43            public void onStopTrackingTouch(SeekBar seekBar) {}
44        });
45    }
46 
47    public void btCalc(View v) {
48        int sum, a, b;
49        try {
50            sum = Integer.parseInt(etSum.getText().toString());
51            a = Integer.parseInt(etA.getText().toString());
52            //甲が0人は例外を投げる
53            if (a == 0) {
54                throw new InputMismatchException();
55            }
56            //乙は未入力や0人を許す
57            String etBstr = etB.getText().toString();
58            b = etBstr.length() == 0 ? 0 : Integer.parseInt(etBstr);
59 
60        } catch (NumberFormatException e) {
61            //合計金額、甲未入力はここを通る。
62            Toast.makeText(this, "不正な値です。", Toast.LENGTH_SHORT).show();
63            return;
64        } catch (InputMismatchException e) {
65            //甲に0が入力された場合はここを通る。
66            Toast.makeText(this, " 甲は1以上の値をいれてください。", Toast.LENGTH_SHORT).show();
67            return;
68        }
69 
70        //SeekBarからの現在値を取得し、100.0dで割って%から比にする。(100%が1.0)
71        double ratio = sb.getProgress() / 100.0d;
72        //甲の支払い(double)
73        double paymentAd = sum / (a + b * ratio);
74        //乙の支払い(double)
75        double paymentBd = b != 0 ? paymentAd * ratio : 0.0;
76        //ラジオグループの現在選択されているラジオボタンのidを取得
77        int checkedId = rg.getCheckedRadioButtonId();
78        //何円単位か?
79        int unit = 1;
80        switch (checkedId) {
81            case R.id.rb1:
82                unit = 1;
83                break;
84            case R.id.rb2:
85                unit = 10;
86                break;
87            case R.id.rb3:
88                unit = 100;
89                break;
90        }
91        //単位を考慮しながら実際の支払額を求める。端数の扱いは切り上げとした。
92        int paymentA = (int) (Math.ceil(paymentAd / unit) * unit);
93        int paymentB = (int) (Math.ceil(paymentBd / unit) * unit);
94        //お釣りの計算
95        int change = paymentA * a + paymentB * b - sum;
96        //結果表示
97        tvResult.setText(String.format("甲(%d円),乙(%d円),おつり(%d円)", paymentA, paymentB, change));
98    }
99}

コメント

タイトルとURLをコピーしました