Bearbeiten von „Android Training/Starting Another Activity“
Die Bearbeitung kann rückgängig gemacht werden. Bitte prüfe den Vergleich unten, um sicherzustellen, dass du dies tun möchtest, und veröffentliche dann unten deine Änderungen, um die Bearbeitung rückgängig zu machen.
Aktuelle Version | Dein Text | ||
Zeile 1: | Zeile 1: | ||
{{Unvollständig}} | |||
After completing the [[Spezial:MyLanguage/Android Training/Building a Simple User Interface|previous lesson]], you have an app that shows an activity (a single screen) with a text field and a button. In this lesson, you’ll add some code to<code>MyActivity</code> that starts a new activity when the user clicks the Send button. | |||
After completing the [[Spezial:MyLanguage/Android Training/Building a Simple User Interface|previous lesson]], you have an app that shows an activity (a single screen) with a text field and a button. In this lesson, you’ll add some code to <code>MyActivity</code> that starts a new activity when the user clicks the Send button. | |||
== Respond to the Send Button == | == Respond to the Send Button == | ||
# In Android Studio, from the <code>res/layout</code> directory, edit the <code>content_my.xml</code> file. | # In Android Studio, from the <code>res/layout</code> directory, edit the<code>content_my.xml</code> file. | ||
# Add the <code>[http://developer.android.com/reference/android/view/View.html#attr_android:onClick android:onClick]</code> attribute to the <code>[http://developer.android.com/reference/android/widget/Button.html <Button>]</code> element. | # Add the <code>[http://developer.android.com/reference/android/view/View.html#attr_android:onClick android:onClick]</code> attribute to the <code>[http://developer.android.com/reference/android/widget/Button.html <Button>]</code> element. | ||
#: ''res/layout/content_my.xml''<syntaxhighlight lang="java"> | #: ''res/layout/content_my.xml''<syntaxhighlight lang="java"> | ||
<Button | <Button | ||
Zeile 13: | Zeile 11: | ||
android:text="@string/button_send" | android:text="@string/button_send" | ||
android:onClick="sendMessage" /> | android:onClick="sendMessage" /> | ||
</syntaxhighlight> | </syntaxhighlight>The <code>android:onClick</code> attribute’s value, <code>"sendMessage"</code>, is the name of a method in your activity that the system calls when the user clicks the button. | ||
The <code>android:onClick</code> attribute’s value, <code>"sendMessage"</code>, is the name of a method in your activity that the system calls when the user clicks the button. | |||
# In the <code>java/com.mycompany.myfirstapp</code> directory, open the <code>MyActivity.java</code> file. | # In the <code>java/com.mycompany.myfirstapp</code> directory, open the <code>MyActivity.java</code> file. | ||
# Within the <code>MyActivity</code> class, add the <code>sendMessage()</code> method stub shown below. | # Within the <code>MyActivity</code> class, add the <code>sendMessage()</code> method stub shown below. | ||
#: ''java/com.mycompany.myfirstapp/MyActivity.java''<syntaxhighlight lang="java"> | #: ''java/com.mycompany.myfirstapp/MyActivity.java''<syntaxhighlight lang="java"> | ||
/** Called when the user clicks the Send button */ | /** Called when the user clicks the Send button */ | ||
Zeile 23: | Zeile 19: | ||
// Do something in response to button | // Do something in response to button | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight>In order for the system to match this method to the method name given to <code>android:onClick</code>, the signature must be exactly as shown. Specifically, the method must: | ||
In order for the system to match this method to the method name given to <code>android:onClick</code>, the signature must be exactly as shown. Specifically, the method must: | |||
#* Be public | #* Be public | ||
#* Have a void return value | #* Have a void return value | ||
Zeile 31: | Zeile 25: | ||
Next, you’ll fill in this method to read the contents of the text field and deliver that text to another activity. | Next, you’ll fill in this method to read the contents of the text field and deliver that text to another activity. | ||
== Build an Intent == | == Build an Intent == | ||
# In <code>MyActivity.java</code>, inside the <code>sendMessage()</code> method, create an <code>Intent</code> to start an activity called <code>DisplayMessageActivity</code> with the following code: | # In <code>MyActivity.java</code>, inside the <code>sendMessage()</code> method, create an <code>Intent</code> to start an activity called<code>DisplayMessageActivity</code> with the following code: | ||
#: ''java/com.mycompany.myfirstapp/MyActivity.java''<syntaxhighlight lang="java"> | #: ''java/com.mycompany.myfirstapp/MyActivity.java''<syntaxhighlight lang="java"> | ||
public void sendMessage(View view) { | public void sendMessage(View view) { | ||
Zeile 38: | Zeile 32: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
#: | #: '''Note:''' The reference to <code>DisplayMessageActivity</code> will raise an error if you’re using an IDE such as Android Studio because the class doesn’t exist yet. Ignore the error for now; you’ll create the class soon. The constructor used here takes two parameters: | ||
'''Note:''' The reference to <code>DisplayMessageActivity</code> will raise an error if you’re using an IDE such as Android Studio because the class doesn’t exist yet. Ignore the error for now; you’ll create the class soon. The constructor used here takes two parameters: | |||
#* A <code>Context</code> as its first parameter (<code>this</code> is used because the <code>Activity</code> class is a subclass of <code>Context</code>) | #* A <code>Context</code> as its first parameter (<code>this</code> is used because the <code>Activity</code> class is a subclass of <code>Context</code>) | ||
#* The <code>Class</code> of the app component to which the system should deliver the <code>Intent</code> (in this case, the activity that should be started) Android Studio indicates that you must import the <code>Intent</code> class. | #* The <code>Class</code> of the app component to which the system should deliver the <code>Intent</code> (in this case, the activity that should be started) Android Studio indicates that you must import the <code>Intent</code> class. | ||
# At the top of the file, import the <code>Intent</code> class: | # At the top of the file, import the <code>Intent</code> class: | ||
#: ''java/com.mycompany.myfirstapp/MyActivity.java''<syntaxhighlight lang="java"> | #: ''java/com.mycompany.myfirstapp/MyActivity.java''<syntaxhighlight lang="java"> | ||
import android.content.Intent; | import android.content.Intent; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
#: | #: '''Tip:''' In Android Studio, press Alt + Enter (option + return on Mac) to import missing classes. | ||
'''Tip:''' In Android Studio, press Alt + Enter (option + return on Mac) to import missing classes. | # Inside the <code>sendMessage()</code> method, use <code>findViewById()</code> to get the <code>EditText</code> element. | ||
# Inside the <code>sendMessage()</code> method, use <code>findViewById()</code> to get the <code>EditText</code> element. | |||
#: ''java/com.mycompany.myfirstapp/MyActivity.java''<syntaxhighlight lang="java"> | #: ''java/com.mycompany.myfirstapp/MyActivity.java''<syntaxhighlight lang="java"> | ||
public void sendMessage(View view) { | public void sendMessage(View view) { | ||
Zeile 55: | Zeile 47: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
# | # At the top of the file, import the <code>EditText</code> class. In Android Studio, press Alt + Enter (option + return on Mac) to import missing classes. | ||
At the top of the file, import the <code>EditText</code> class. In Android Studio, press Alt + Enter (option + return on Mac) to import missing classes. | # Assign the text to a local <code>message</code> variable, and use the <code>putExtra()</code> method to add its text value to the intent. | ||
# Assign the text to a local <code>message</code> variable, and use the <code>putExtra()</code> method to add its text value to the intent. | |||
#: ''java/com.mycompany.myfirstapp/MyActivity.java''<syntaxhighlight lang="java"> | #: ''java/com.mycompany.myfirstapp/MyActivity.java''<syntaxhighlight lang="java"> | ||
public void sendMessage(View view) { | public void sendMessage(View view) { | ||
Zeile 66: | Zeile 57: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
#: | #: An <code>Intent</code> can carry data types as key-value pairs called ''extras''. The <code>putExtra()</code> method takes the key name in the first parameter and the value in the second parameter. | ||
An <code>Intent</code> can carry data types as key-value pairs called ''extras''. The <code>putExtra()</code> method takes the key name in the first parameter and the value in the second parameter. | # At the top of the <code>MyActivity</code> class, add the <code>EXTRA_MESSAGE</code> definition as follows: | ||
# At the top of the <code>MyActivity</code> class, add the <code>EXTRA_MESSAGE</code> definition as follows: | |||
#: ''java/com.mycompany.myfirstapp/MyActivity.java''<syntaxhighlight lang="java"> | #: ''java/com.mycompany.myfirstapp/MyActivity.java''<syntaxhighlight lang="java"> | ||
public class MyActivity extends AppCompatActivity { | public class MyActivity extends AppCompatActivity { | ||
Zeile 75: | Zeile 65: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
#: | #: For the next activity to query the extra data, you should define the key for your intent's extra using a public constant. It's generally a good practice to define keys for intent extras using your app's package name as a prefix. This ensures the keys are unique, in case your app interacts with other apps. | ||
For the next activity to query the extra data, you should define the key for your intent's extra using a public constant. It's generally a good practice to define keys for intent extras using your app's package name as a prefix. This ensures the keys are unique, in case your app interacts with other apps. | # In the <code>sendMessage()</code> method, to finish the intent, call the <code>startActivity()</code> method, passing it the<code>Intent</code> object created in step 1. | ||
# In the <code>sendMessage()</code> method, to finish the intent, call the <code>startActivity()</code> method, passing it the <code>Intent</code> object created in step 1. | |||
With this new code, the complete <code>sendMessage()</code> method that's invoked by the Send button now looks like this: | |||
With this new code, the complete <code>sendMessage()</code> method that's invoked by the Send button now looks like this: | |||
''java/com.mycompany.myfirstapp/MyActivity.java''<syntaxhighlight lang="java"> | ''java/com.mycompany.myfirstapp/MyActivity.java''<syntaxhighlight lang="java"> | ||
/** Called when the user clicks the Send button */ | /** Called when the user clicks the Send button */ | ||
Zeile 91: | Zeile 79: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
The system receives this call and starts an instance of the <code>Activity</code> specified by the <code>Intent</code>. Now you need to create the <code>DisplayMessageActivity</code> class in order for this to work. | The system receives this call and starts an instance of the <code>Activity</code> specified by the <code>Intent</code>. Now you need to create the <code>DisplayMessageActivity</code> class in order for this to work. | ||
{{TNT|Android_Training/Attribution}} | {{TNT|Android_Training/Attribution}} |