Android Tutorial - Part 4 "Dem Code näher kommen"

in #android7 years ago

Willkommen zu Teil 4 unseres Android Tutorials! Nachdem wir im letztem Teil die Eingabe eines Nutzers ausgelesen haben, werden wir nun die Eingabe mithilfe von Java Code verändern.

<h2>Vorbereitung<br /> Zur Vorbereitung verändern wir unseren String<p> <pre><code><string name="eingabe">Hier kommt Text rein!</string> <p dir="auto">zu <pre><code><string name="eingabe">Dein Name</string> <p dir="auto"><h2>Eingabe verändern<br /> Wir erwarten nun also, dass der Nutzer in das Eingabefeld seinen Namen einträgt. Jetzt könnten wir, anstatt einfach den Inhalt des Eingabefelds zurück zu geben, ein "Willkommen in meiner App !" zurück geben. Dazu suchen wir die Stelle in der wir den Toast ausgeben:<p> <pre><code>Toast.makeText(MainActivity.this, eingabe.getText(), Toast.LENGTH_LONG).show(); <p dir="auto">Der mittlere Bereich, also der Bereich nach dem ersten Komma, gibt den String an, welcher zurück gegeben werden soll. Jetzt möchten wir zu dem Wert des Textfeldes noch zwei Strings hinzufügen, davor "Willkommen in meiner App " und dahinter "!". Der Befehl zur Verknüpfung mehrerer Strings lautet einfach "+". So können wir unseren Toast nun so umschreiben: <pre><code>Toast.makeText(MainActivity.this, "Willkommen in meiner App "+eingabe.getText()+"!", Toast.LENGTH_LONG).show(); <p dir="auto"><h2>Variablen<br /> Wir werden im späteren Verlauf viel mit Variablen arbeiten. Dies sind kurze "Wörter", in denen wir Werte einspeichern. Sie haben den Vorteil, dass wir sie einmal definieren müssen und schon werden sie im gesamten Code verwendet.<p> <p dir="auto">In Java besitzen Variablen einen bestimmten Datentyp. Die Variablen können daraufhin nur Werte von diesem Datentyp einspeichern. Für uns sind folgende Datentypen erst einmal wichtig: <p dir="auto"><strong>String - Ein String, also ein Text.<br /> Int - Eine Ganzzahl, also ohne Kommas!<br /> Double - Eine Kommazahl.<br /> Boolean - Ein Wahrheitswert, also "true" oder "false" bzw. "1" oder "0". <p dir="auto">Eine Variable definieren wir folgendermaßen: <pre><code>String variable; <p dir="auto">um der Variable einen Wert zu geben verwenden wir nun: <pre><code>variable = "Hallo Welt!"; <p dir="auto">Den Datentyp weisen wir der Variable oberhalb der "onCreate" Funktion, aber innerhalb der Klasse, zu.<br /> Nun werden wir unseren Toast zu <pre><code>Toast.makeText(MainActivity.this, willkommen, Toast.LENGTH_LONG).show(); <p dir="auto">verändern. Versucht einmal die Variable "willkommen" zu definieren und bei jedem Knopfdruck zu unserem String "Willkommen in meiner App "+eingabe.getText()+"!" zu ändern. Die Lösung findet ihr natürlich wieder in den Lösungen. Das ganze kommt euch jetzt wahrscheinlich sehr Umständlich vor, aber wenn wir später sehr viel größere Programme schreiben nimmt uns dieses Prinzip eine große Menge Arbeit ab! <p dir="auto"><strong>Ich hoffe der vierte Teil unseres Android Tutorials hat euch gefallen! Schreibt eure Gedanken und Fragen wie immer in die Kommentare!<br /> <h2>Lösungen<p> <pre><code>package city.univer.blindjerobine.meineapp; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { Button btn; EditText eingabe; String willkommen; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); eingabe = (EditText)findViewById(R.id.eingabe); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { willkommen = "Willkommen in meiner App "+eingabe.getText()+"!"; Toast.makeText(MainActivity.this, willkommen, Toast.LENGTH_LONG).show(); } }); } }
Sort:  

Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
https://univer.city/programmierung/android-tutorial-part-4-dem-code-naeher-kommen/

Congratulations @blindjerobine! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

<p dir="auto"><a href="http://steemitboard.com/@blindjerobine" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link"><img src="https://images.hive.blog/768x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/votes.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/votes.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/votes.png 2x" /> Award for the number of upvotes <p dir="auto"><sub><em>Click on the badge to view your Board of Honor.<br /> <sub><em>If you no longer want to receive notifications, reply to this comment with the word <code>STOP <blockquote> <p dir="auto">Do you like <a href="https://steemit.com/@steemitboard" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">SteemitBoard's project? Then <strong><a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Vote for its witness and <strong>get one more award!

Congratulations @blindjerobine! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

<table><tr><td><span><img src="https://images.hive.blog/768x0/https://steemitimages.com/60x70/http://steemitboard.com/@blindjerobine/votes.png?201811200249" srcset="https://images.hive.blog/768x0/https://steemitimages.com/60x70/http://steemitboard.com/@blindjerobine/votes.png?201811200249 1x, https://images.hive.blog/1536x0/https://steemitimages.com/60x70/http://steemitboard.com/@blindjerobine/votes.png?201811200249 2x" /><td>You made more than 400 upvotes. Your next target is to reach 500 upvotes. <p dir="auto"><sub><em><a href="https://steemitboard.com/@blindjerobine" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Click here to view your Board of Honor<br /> <sub><em>If you no longer want to receive notifications, reply to this comment with the word <code>STOP <p dir="auto"><strong><span>Do not miss the last post from <a href="/@steemitboard">@steemitboard: <table><tr><td><a href="https://steemit.com/steemfest/@steemitboard/meet-the-steemians-contest-the-results-the-winners-and-the-prizes" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link"><img src="https://images.hive.blog/768x0/https://steemitimages.com/64x128/https://cdn.steemitimages.com/DQmeLukvNFRsa7RURqsFpiLGEZZD49MiU52JtWmjS5S2wtW/image.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/64x128/https://cdn.steemitimages.com/DQmeLukvNFRsa7RURqsFpiLGEZZD49MiU52JtWmjS5S2wtW/image.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/64x128/https://cdn.steemitimages.com/DQmeLukvNFRsa7RURqsFpiLGEZZD49MiU52JtWmjS5S2wtW/image.png 2x" /><td><a href="https://steemit.com/steemfest/@steemitboard/meet-the-steemians-contest-the-results-the-winners-and-the-prizes" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Meet the Steemians Contest - The results, the winners and the prizes<tr><td><a href="https://steemit.com/steemfest/@steemitboard/meet-the-steemians-contest-special-attendees-revealed" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link"><img src="https://images.hive.blog/768x0/https://steemitimages.com/64x128/https://cdn.steemitimages.com/DQmeLukvNFRsa7RURqsFpiLGEZZD49MiU52JtWmjS5S2wtW/image.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/64x128/https://cdn.steemitimages.com/DQmeLukvNFRsa7RURqsFpiLGEZZD49MiU52JtWmjS5S2wtW/image.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/64x128/https://cdn.steemitimages.com/DQmeLukvNFRsa7RURqsFpiLGEZZD49MiU52JtWmjS5S2wtW/image.png 2x" /><td><a href="https://steemit.com/steemfest/@steemitboard/meet-the-steemians-contest-special-attendees-revealed" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Meet the Steemians Contest - Special attendees revealed<tr><td><a href="https://steemit.com/steemfest/@steemitboard/meet-the-steemians-contest-intermediate-results" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link"><img src="https://images.hive.blog/768x0/https://steemitimages.com/64x128/https://cdn.steemitimages.com/DQmeLukvNFRsa7RURqsFpiLGEZZD49MiU52JtWmjS5S2wtW/image.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/64x128/https://cdn.steemitimages.com/DQmeLukvNFRsa7RURqsFpiLGEZZD49MiU52JtWmjS5S2wtW/image.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/64x128/https://cdn.steemitimages.com/DQmeLukvNFRsa7RURqsFpiLGEZZD49MiU52JtWmjS5S2wtW/image.png 2x" /><td><a href="https://steemit.com/steemfest/@steemitboard/meet-the-steemians-contest-intermediate-results" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Meet the Steemians Contest - Intermediate results <blockquote> <p dir="auto">Support <a href="https://steemit.com/@steemitboard" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">SteemitBoard's project! <strong><a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Vote for its witness and <strong>get one more award!

Congratulations @blindjerobine! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

<table><tr><td><span><img src="https://images.hive.blog/768x0/https://steemitimages.com/60x70/http://steemitboard.com/@blindjerobine/votes.png?201812240923" srcset="https://images.hive.blog/768x0/https://steemitimages.com/60x70/http://steemitboard.com/@blindjerobine/votes.png?201812240923 1x, https://images.hive.blog/1536x0/https://steemitimages.com/60x70/http://steemitboard.com/@blindjerobine/votes.png?201812240923 2x" /><td>You made more than 500 upvotes. Your next target is to reach 600 upvotes. <p dir="auto"><sub><em><a href="https://steemitboard.com/@blindjerobine" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Click here to view your Board<br /> <sub><em>If you no longer want to receive notifications, reply to this comment with the word <code>STOP <p dir="auto"><strong><span>Do not miss the last post from <a href="/@steemitboard">@steemitboard: <table><tr><td><a href="https://steemit.com/christmas/@steemitboard/christmas-challenge-send-a-gift-to-to-your-friends" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link"><img src="https://images.hive.blog/768x0/https://steemitimages.com/64x128/http://i.cubeupload.com/kf4SJb.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/64x128/http://i.cubeupload.com/kf4SJb.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/64x128/http://i.cubeupload.com/kf4SJb.png 2x" /><td><a href="https://steemit.com/christmas/@steemitboard/christmas-challenge-send-a-gift-to-to-your-friends" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Christmas Challenge - Send a gift to to your friends <blockquote> <p dir="auto">Support <a href="https://steemit.com/@steemitboard" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">SteemitBoard's project! <strong><a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Vote for its witness and <strong>get one more award!

Congratulations @blindjerobine! You received a personal award!

1 Year on Steemit

Click here to view your Board

Do not miss the last post from @steemitboard:

SteemWhales has officially moved to SteemitBoard Ranking
SteemitBoard - Witness Update

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @blindjerobine! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!