LinearLayout,SeekBar,RadioGroup等。
Q1
割り勘電卓を作成せよ。男女などで支払い額が変えられるようにする。
レイアウトのルートにはLinearLayoutを用いること。
[実行例]
スタート画面

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

甲のみの入力にも対応

●activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="14dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="合計金額" />
<EditText
android:id="@+id/etSum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="52dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="甲の人数" />
<EditText
android:id="@+id/etA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="5"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="52dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="乙の人数" />
<EditText
android:id="@+id/etB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="5"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="31dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="甲に対する乙の割合:"
android:textSize="16sp" />
<TextView
android:id="@+id/tvRatio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100%"
android:textSize="16sp" />
</LinearLayout>
<SeekBar
android:id="@+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:max="200"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:progress="100" />
<TextView
android:id="@+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="単位" />
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="43dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:text="1円" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="10円" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="100円" />
</RadioGroup>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btCalc"
android:text="計算" />
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="18sp" />
</LinearLayout>
●MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import java.util.InputMismatchException;
public class MainActivity extends AppCompatActivity {
private EditText etSum, etA, etB;
private TextView tvRatio, tvResult;
private SeekBar sb;
private RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etSum = (EditText) findViewById(R.id.etSum);
etA = (EditText) findViewById(R.id.etA);
etB = (EditText) findViewById(R.id.etB);
tvRatio = (TextView) findViewById(R.id.tvRatio);
tvResult = (TextView) findViewById(R.id.tvResult);
rg = (RadioGroup) findViewById(R.id.rg);
sb = (SeekBar) findViewById(R.id.sb);
//Seekbarにチェンジリスナーを設定し、リアルタイムでTextViewに値を表示する。
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvRatio.setText(progress + "%");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
}
public void btCalc(View v) {
int sum, a, b;
try {
sum = Integer.parseInt(etSum.getText().toString());
a = Integer.parseInt(etA.getText().toString());
//甲が0人は例外を投げる
if (a == 0) {
throw new InputMismatchException();
}
//乙は未入力や0人を許す
String etBstr = etB.getText().toString();
b = etBstr.length() == 0 ? 0 : Integer.parseInt(etBstr);
} catch (NumberFormatException e) {
//合計金額、甲未入力はここを通る。
Toast.makeText(this, "不正な値です。", Toast.LENGTH_SHORT).show();
return;
} catch (InputMismatchException e) {
//甲に0が入力された場合はここを通る。
Toast.makeText(this, " 甲は1以上の値をいれてください。", Toast.LENGTH_SHORT).show();
return;
}
//SeekBarからの現在値を取得し、100.0dで割って%から比にする。(100%が1.0)
double ratio = sb.getProgress() / 100.0d;
//甲の支払い(double)
double paymentAd = sum / (a + b * ratio);
//乙の支払い(double)
double paymentBd = b != 0 ? paymentAd * ratio : 0.0;
//ラジオグループの現在選択されているラジオボタンのidを取得
int checkedId = rg.getCheckedRadioButtonId();
//何円単位か?
int unit = 1;
switch (checkedId) {
case R.id.rb1:
unit = 1;
break;
case R.id.rb2:
unit = 10;
break;
case R.id.rb3:
unit = 100;
break;
}
//単位を考慮しながら実際の支払額を求める。端数の扱いは切り上げとした。
int paymentA = (int) (Math.ceil(paymentAd / unit) * unit);
int paymentB = (int) (Math.ceil(paymentBd / unit) * unit);
//お釣りの計算
int change = paymentA * a + paymentB * b - sum;
//結果表示
tvResult.setText(String.format("甲(%d円),乙(%d円),おつり(%d円)", paymentA, paymentB, change));
}
}
コメント