J
JoMedics
Neues Mitglied
- 0
Hallo liebe Community,
ich bin sowohl auf dem Gebiet der Android-Entwicklung als auch in diesem Forum ziemlich neu.
Allerdings besitze ich schon relativ gute Kenntnisse in Java von der Schule her und hab mich privat einigermaßen damit beschäftigt.
Meine IDE ist Android Studio, da ich durch einen Freund drauf gekommen bin und mir die Oberfläche ganz gut gefällt.
Als Einstieg hab ich einen kleinen Login-Versuch gemacht mit Überprüfung ob es > 4 Zeichen lang ist, ob es eine gültige E-Mail ist usw., hat problemlos und ziemlich schnell funktioniert.
Jetzt will ich einen einfachen Taschenrechner als Einstieg programmieren mit Addition, Subtraktion, Multiplikation und Division zweier einzugebenden, was durch 4 Radiobuttons ausgewählt werden soll, um mich mal mit den verschiedenen "Klassen" zu befassen und deren Verhalten zu verstehen.
Von Java auf "PC-Ebene" bin ich folgendes gewohnt (ggf. noch umwandeln):
Nun weiß ich allerdings nicht, wie ich das auf Android umsetzen soll, außerdem kommt auf meinem Handy (S4 gerootet, 4.4.2) sofort die Meldung "Taschenrechner funktioniert nicht mehr!"
Hier mal meine MainActivity:
(Ja ich weiß, die Methode onClickButton wird noch nicht aufgerufen, darum kümmere ich mich später )
Und meine activity_main.xml:
Ich hoffe dass ich hier nicht ein zweites Thema gleichen Problems erstellt hab, falls ja tut es mir leid, aber hab bisher noch nichts derart gefunden und bin auch noch ziemlich neu. :blushing:
Vielen Dank schon mal im Voraus!
ich bin sowohl auf dem Gebiet der Android-Entwicklung als auch in diesem Forum ziemlich neu.
Allerdings besitze ich schon relativ gute Kenntnisse in Java von der Schule her und hab mich privat einigermaßen damit beschäftigt.
Meine IDE ist Android Studio, da ich durch einen Freund drauf gekommen bin und mir die Oberfläche ganz gut gefällt.
Als Einstieg hab ich einen kleinen Login-Versuch gemacht mit Überprüfung ob es > 4 Zeichen lang ist, ob es eine gültige E-Mail ist usw., hat problemlos und ziemlich schnell funktioniert.
Jetzt will ich einen einfachen Taschenrechner als Einstieg programmieren mit Addition, Subtraktion, Multiplikation und Division zweier einzugebenden, was durch 4 Radiobuttons ausgewählt werden soll, um mich mal mit den verschiedenen "Klassen" zu befassen und deren Verhalten zu verstehen.
Von Java auf "PC-Ebene" bin ich folgendes gewohnt (ggf. noch umwandeln):
Code:
int zahl1 = eingabeZahl1.getText();
Nun weiß ich allerdings nicht, wie ich das auf Android umsetzen soll, außerdem kommt auf meinem Handy (S4 gerootet, 4.4.2) sofort die Meldung "Taschenrechner funktioniert nicht mehr!"
Hier mal meine MainActivity:
Code:
package lukas.taschenrechner;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button berechne;
private int ergebnis;
private EditText z1;
private EditText z2;
private int zahl1;
private int zahl2;
TextView erg = (TextView) findViewById(R.id.txtErgebnis);
Button rechne = (Button) findViewById(R.id.btnBerechnen);
RadioButton r1 = (RadioButton) findViewById(R.id.radioAdd);
RadioButton r2 = (RadioButton) findViewById(R.id.radioSub);
RadioButton r3 = (RadioButton) findViewById(R.id.radioMulti);
RadioButton r4 = (RadioButton) findViewById(R.id.radioDiv);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
z1 = (EditText) findViewById(R.id.eingabeZahl1);
z2 = (EditText) findViewById(R.id.eingabeZahl2);
zahl1 = Integer.valueOf(z1.getText());
zahl2 = Integer.valueOf(z2.getText());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClickButton (View view) {
if (r1.isActivated()) {
Addieren(zahl1, zahl2);
}
else if (r2.isActivated()) {
Subtrahieren(zahl1, zahl2);
}
else if (r3.isActivated()) {
Multiplikation(zahl1, zahl2);
}
else if (r4.isActivated()) {
Division(zahl1, zahl2);
}
}
public void Addieren(int zahl1, int zahl2) {
ergebnis = zahl1 + zahl2;
erg.setText(ergebnis);
}
public void Subtrahieren(int zahl1, int zahl2) {
ergebnis = zahl1 - zahl2;
erg.setText(ergebnis);
}
public void Multiplikation(int zahl1, int zahl2) {
ergebnis = zahl1 * zahl2;
erg.setText(ergebnis);
}
public void Division(int zahl1, int zahl2) {
ergebnis = zahl1 - zahl2;
erg.setText(ergebnis);
}
}
(Ja ich weiß, die Methode onClickButton wird noch nicht aufgerufen, darum kümmere ich mich später )
Und meine activity_main.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Zahl1:"
android:id="@+id/textZahl1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Zahl2:"
android:id="@+id/textZahl2"
android:layout_marginTop="42dp"
android:layout_below="@+id/textZahl1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="25mm"
android:layout_height="wrap_content"
android:id="@+id/eingabeZahl1"
android:inputType="number"
android:layout_alignBottom="@+id/textZahl2"
android:layout_toRightOf="@+id/textZahl2"
android:layout_toEndOf="@+id/textZahl2"
android:editable="false" />
<EditText
android:inputType="number"
android:layout_width="25mm"
android:layout_height="wrap_content"
android:id="@+id/eingabeZahl2"
android:layout_above="@+id/textZahl2"
android:layout_toRightOf="@+id/textZahl1"
android:layout_alignParentStart="false"
android:onClick="setContentView"
android:editable="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Addition"
android:id="@+id/radioAdd"
android:layout_marginTop="43dp"
android:checked="false"
android:layout_below="@+id/eingabeZahl1"
android:layout_alignRight="@+id/eingabeZahl1"
android:layout_alignEnd="@+id/eingabeZahl1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subtraktion"
android:id="@+id/radioSub"
android:checked="false"
android:layout_below="@+id/radioAdd"
android:layout_alignLeft="@+id/radioAdd"
android:layout_alignStart="@+id/radioAdd" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Multiplikation"
android:id="@+id/radioMulti"
android:checked="false"
android:layout_below="@+id/radioSub"
android:layout_alignLeft="@+id/radioSub"
android:layout_alignStart="@+id/radioSub" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Division"
android:id="@+id/radioDiv"
android:checked="false"
android:layout_below="@+id/radioMulti"
android:layout_alignLeft="@+id/radioMulti"
android:layout_alignStart="@+id/radioMulti" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Berechnen"
android:id="@+id/btnBerechnen"
android:layout_below="@+id/radioDiv"
android:layout_alignRight="@+id/radioMulti"
android:layout_alignEnd="@+id/radioMulti"
android:layout_marginTop="29dp"
android:onClick="setContentView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Ergebnis"
android:id="@+id/txtErgebnis"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="45dp" />
</RelativeLayout>
Ich hoffe dass ich hier nicht ein zweites Thema gleichen Problems erstellt hab, falls ja tut es mir leid, aber hab bisher noch nichts derart gefunden und bin auch noch ziemlich neu. :blushing:
Vielen Dank schon mal im Voraus!