### oefening 1 ###
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/txtNumber1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/txtNumber2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="optellen" />
<TextView
android:id="@+id/txtResult1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Download hier het bestand.
### oefening 2 ###
public class MainActivity extends AppCompatActivity {
EditText firstNumber;
EditText secondNumber;
TextView addResult;
Button btnAdd;
double num1, num2, sum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstNumber = (EditText)findViewById(R.id.txtNumber1);
secondNumber = (EditText)findViewById(R.id.txtNumber2);
addResult = (TextView)findViewById(R.id.txtResult1);
btnAdd = (Button)findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
num1 = Double.parseDouble(firstNumber.getText().toString());
num2 = Double.parseDouble(secondNumber.getText().toString());
sum = num1 + num2;
addResult.setText(Double.toString(sum));
} catch (Exception e) {
addResult.setText(e.getMessage());
}
}
});
}
}
Download hier het bestand.
### oefening 3 ###
btnAdd.setOnClickListener(new View.OnClickListener() {
...
} catch (Exception e) {
if (e instanceof NumberFormatException) {
addResult.setText("Geen tekst a.u.b.");
} else {
addResult.setText(e.getMessage());
}
}
...
}
});
Download hier het bestand.
