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" > |
009 | android:layout_width = "match_parent" |
010 | android:layout_height = "55dp" |
011 | android:orientation = "horizontal" > |
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 = "合計金額" /> |
021 | android:id = "@+id/etSum" |
022 | android:layout_width = "wrap_content" |
023 | android:layout_height = "wrap_content" |
024 | android:layout_weight = "1" |
026 | android:inputType = "number" /> |
031 | android:layout_width = "match_parent" |
032 | android:layout_height = "52dp" |
033 | android:orientation = "horizontal" > |
037 | android:layout_width = "wrap_content" |
038 | android:layout_height = "wrap_content" |
039 | android:layout_weight = "1" |
040 | android:text = "甲の人数" /> |
043 | android:id = "@+id/etA" |
044 | android:layout_width = "wrap_content" |
045 | android:layout_height = "wrap_content" |
046 | android:layout_weight = "1" |
048 | android:inputType = "number" /> |
053 | android:layout_width = "match_parent" |
054 | android:layout_height = "52dp" |
055 | android:orientation = "horizontal" > |
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 = "乙の人数" /> |
065 | android:id = "@+id/etB" |
066 | android:layout_width = "wrap_content" |
067 | android:layout_height = "wrap_content" |
068 | android:layout_weight = "1" |
070 | android:inputType = "number" /> |
076 | android:layout_width = "match_parent" |
077 | android:layout_height = "31dp" |
078 | android:orientation = "horizontal" > |
081 | android:id = "@+id/textView5" |
082 | android:layout_width = "wrap_content" |
083 | android:layout_height = "wrap_content" |
084 | android:text = "甲に対する乙の割合:" |
085 | android:textSize = "16sp" /> |
088 | android:id = "@+id/tvRatio" |
089 | android:layout_width = "wrap_content" |
090 | android:layout_height = "wrap_content" |
093 | android:textSize = "16sp" /> |
099 | android:layout_width = "match_parent" |
100 | android:layout_height = "wrap_content" |
101 | android:layout_marginBottom = "10dp" |
103 | android:paddingBottom = "10dp" |
104 | android:paddingTop = "10dp" |
105 | android:progress = "100" /> |
108 | android:id = "@+id/textView7" |
109 | android:layout_width = "match_parent" |
110 | android:layout_height = "wrap_content" |
115 | android:layout_width = "match_parent" |
116 | android:layout_height = "43dp" |
117 | android:orientation = "horizontal" > |
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" |
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円" /> |
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円" /> |
143 | android:id = "@+id/button" |
144 | android:layout_width = "match_parent" |
145 | android:layout_height = "wrap_content" |
146 | android:onClick = "btCalc" |
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" /> |
●MainActivity.java
01 | import android.os.Bundle; |
02 | import android.support.v7.app.AppCompatActivity; |
03 | import android.view.View; |
04 | import android.widget.EditText; |
05 | import android.widget.RadioGroup; |
06 | import android.widget.SeekBar; |
07 | import android.widget.TextView; |
08 | import android.widget.Toast; |
10 | import java.util.InputMismatchException; |
12 | public class MainActivity extends AppCompatActivity { |
13 | private EditText etSum, etA, etB; |
14 | private TextView tvRatio, tvResult; |
16 | private RadioGroup rg; |
19 | protected void onCreate(Bundle savedInstanceState) { |
20 | super .onCreate(savedInstanceState); |
21 | setContentView(R.layout.activity_main); |
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); |
33 | sb.setOnSeekBarChangeListener( new SeekBar.OnSeekBarChangeListener() { |
35 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
36 | tvRatio.setText(progress + "%" ); |
40 | public void onStartTrackingTouch(SeekBar seekBar) {} |
43 | public void onStopTrackingTouch(SeekBar seekBar) {} |
47 | public void btCalc(View v) { |
50 | sum = Integer.parseInt(etSum.getText().toString()); |
51 | a = Integer.parseInt(etA.getText().toString()); |
54 | throw new InputMismatchException(); |
57 | String etBstr = etB.getText().toString(); |
58 | b = etBstr.length() == 0 ? 0 : Integer.parseInt(etBstr); |
60 | } catch (NumberFormatException e) { |
62 | Toast.makeText( this , "不正な値です。" , Toast.LENGTH_SHORT).show(); |
64 | } catch (InputMismatchException e) { |
66 | Toast.makeText( this , " 甲は1以上の値をいれてください。" , Toast.LENGTH_SHORT).show(); |
71 | double ratio = sb.getProgress() / 100 .0d; |
73 | double paymentAd = sum / (a + b * ratio); |
75 | double paymentBd = b != 0 ? paymentAd * ratio : 0.0 ; |
77 | int checkedId = rg.getCheckedRadioButtonId(); |
92 | int paymentA = ( int ) (Math.ceil(paymentAd / unit) * unit); |
93 | int paymentB = ( int ) (Math.ceil(paymentBd / unit) * unit); |
95 | int change = paymentA * a + paymentB * b - sum; |
97 | tvResult.setText(String.format( "甲(%d円),乙(%d円),おつり(%d円)" , paymentA, paymentB, change)); |
コメント