package com.allmycode.a06_01;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText ageEditText;
CheckBox specialShowingCheckBox;
TextView outputTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ageEditText = (EditText) findViewById(R.id.ageEditText);
specialShowingCheckBox =
(CheckBox) findViewById(R.id.specialShowingCheckBox);
outputTextView = (TextView) findViewById(R.id.outputTextView);
}
public void onButtonClick(View view) {
<strong>int age = Integer.parseInt(ageEditText.getText().toString());</strong>
<strong>boolean isSpecialShowing = specialShowingCheckBox.isChecked();</strong>
boolean chargeDiscountPrice = (age < 18 || 65 <= age) && !isSpecialShowing;
outputTextView.setText(Boolean.toString(chargeDiscountPrice));
}
}
There's more to the app than the code. To create this app, you have to design the layout with its text fields, its check box, and its button. You also have to set the button's onClick
property to "onButtonClick"
.
isChecked
method and the isSpecialShowing
variable gets its value from a call to the isChecked
method. Here, the user hasn't selected the check box. So, when Android executes the code, the expression specialShowingCheckBox.isChecked()
has the value false
.But, in this image, the user has selected the check box. So, when Android executes the code, the expression specialShowingCheckBox.isChecked()
has the value true
.
To make the code work, you have to associate the variable names ageEditText
, specialShowingCheckBox
, and outputTextView
with the correct thingamajigs on the device's screen. The findViewById
statements help you do that.