
Feelix333
Stamm-User
- 349
Hallo zusammen,
Wie der Titel schon sagt möchte ich für Android eine einfache Währungsrechner - App machen.
Am Anfang mit einem intern festgelegtem Wechselkurs, später noch mit einem Eingabefeld für den aktuellen Kurs.
Leider komme ich im Moment nicht weiter.
Das Grundgerüst habe ich schon erstellt:
MainActivity.java
activity_main.xml
strings.xml
Bis jetzt ist es nur eine App, die per Klick auf den Button die eingegebene Zahl wieder ausgibt.
Mein alter Code für eine Umrechnung - was ja nur eine Multiplikation ist - lautet:
Leider komme ich aber jetzt überhaupt nicht weiter.
Ich habe es so versucht:
Bekomme dann aber die Meldung, dass die Variable text1 (duplicate local variable text1) doppelt ist.
Benenne ich sie um bekomme ich in der letzen Zeile den Fehler
"The method setText(CharSequence) in the type TextView ist not applicable for the arguments (double)".
Welchen Typ soll ich sonst nehmen oder habe irgendwo einen kompletten Denkfehler?
Bitte helft mir, das sind eigentlich zwei ganz einfache Programme, aber in bekomme sie einfach nicht unter einen Nenner :-/
Danke schon mal!
mfg
Felix
Wie der Titel schon sagt möchte ich für Android eine einfache Währungsrechner - App machen.
Am Anfang mit einem intern festgelegtem Wechselkurs, später noch mit einem Eingabefeld für den aktuellen Kurs.
Leider komme ich im Moment nicht weiter.
Das Grundgerüst habe ich schon erstellt:
MainActivity.java
Code:
package com.example.euro_dollar;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity implements OnClickListener
{
private Button button;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
public void onClick(View arg0)
{
EditText nameField = (EditText) findViewById(R.id.eingabe);
String name = nameField.getText().toString();
if (button.getId() == ((Button)arg0).getId())
{
int resourceId;
if (arg0 == button)
{
resourceId = R.string.response;
String text1 = getResources().getString(resourceId, name);
TextView ausgabe = (TextView)findViewById(R.id.text1);
ausgabe.setText(text1);
}
}
}
}
activity_main.xml
Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#008000"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/eingabe"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:text="@string/text3"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/eingabe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="75dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/button" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:text="@string/text1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text1"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="@string/text2"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
strings.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Euro-Dollar</string>
<string name="action_settings">Einstellungen</string>
<string name="button">umwandeln</string>
<string name="text1"><u></u></string>
<string name="response"><u>%s $</u></string>
<string name="text2">Aktueller Dollarkurs: 1,3087</string>
<string name="text3">Betrag in Euro eingeben:</string>
</resources>
Bis jetzt ist es nur eine App, die per Klick auf den Button die eingegebene Zahl wieder ausgibt.
Mein alter Code für eine Umrechnung - was ja nur eine Multiplikation ist - lautet:
Code:
public static void main(String[] args) {
String eingabe;
double ergebnis;
double euro;
eingabe = JOptionPane.showInputDialog("Betragt in Euro:");
euro = Double.parseDouble(eingabe);
ergebnis = euro * 1.22583; //kurs vom 15 Juni 2010
System.out.println("Ergebnis: " + ergebnis + " Dollar");
}
Leider komme ich aber jetzt überhaupt nicht weiter.
Ich habe es so versucht:
Code:
package com.example.euro_dollar;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity implements OnClickListener
{
private Button button;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
public void onClick(View arg0)
{
EditText nameField = (EditText) findViewById(R.id.eingabe);
String name = nameField.getText().toString();
if (button.getId() == ((Button)arg0).getId())
{
int resourceId;
String input = "5";
double text1;
double euro;
euro = Double.parseDouble(input);
text1 = euro * 1.3087;
if (arg0 == button)
{
resourceId = R.string.text1;
String text1 = getResources().getString(resourceId, name);
TextView ausgabe = (TextView)findViewById(R.id.text1);
ausgabe.setText(text1);
}
}
}
}
Bekomme dann aber die Meldung, dass die Variable text1 (duplicate local variable text1) doppelt ist.
Benenne ich sie um bekomme ich in der letzen Zeile den Fehler
"The method setText(CharSequence) in the type TextView ist not applicable for the arguments (double)".
Welchen Typ soll ich sonst nehmen oder habe irgendwo einen kompletten Denkfehler?
Bitte helft mir, das sind eigentlich zwei ganz einfache Programme, aber in bekomme sie einfach nicht unter einen Nenner :-/
Danke schon mal!
mfg
Felix
Zuletzt bearbeitet: