Last time Android app: Try to summarize events and listeners
Continued. .. ..
It's been three months since I joined the company. I am studying android application development. I will summarize it after studying.
I am chewing on my own introductory level knowledge. Please note that there are many parts that are broken.
This time, I will summarize the screen transition with a simple code. ・ I want to know the basics of screen transitions. ・ I want to understand the intent simply. For those who say.
What you create is an application that displays the entered text after the screen transition when you enter the text and press the send button.
--Intent
Simply put, it's like a container for carrying information from one activity to another. If you put the character string in the activity on screen 1 in a box called intent like this time, you can use the character string on screen 2 by calling the box on screen 2.
--Event
The moment of user screen operation such as pressing a button
--Listener
Keep an eye on the event so that it can happen at any time.
--Activity
The app screen itself.
androidstudio 3.6.2 openjdk version "11.0.6"
This is the screen to be created this time.
I will write so that the entered characters can be displayed at the transition destination when the button is pressed. Create two required java classes and two layout files.
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Set the submit button as a listener
Button btClick = findViewById(R.id.btOutput);
Listener listener = new Listener();
btClick.setOnClickListener(listener);
}
//Create a listener class
private class Listener implements View.OnClickListener{
@Override
public void onClick(View v) {
EditText input = findViewById(R.id.sendEditText);
String inputStr = input.getText().toString();
//Create an intent object
Intent intent = new Intent(MainActivity.this,OutputActivity.class);
//Store data to be sent to screen ②
intent.putExtra("inputText",inputStr);
//Launch screen
startActivity(intent);
}
}
}
OutputActivity.java
public class OutputActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sub_activity);
//Get the intent object created on screen ①
Intent intent = getIntent();
//Get character information and output to screen
TextView output = findViewById(R.id.outputText);
String outputText = intent.getStringExtra("inputText");
output.setText(outputText);
}
}
activity_main.xml
<LinearLayout ~~ Omitted ~~>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/title"/>
<EditText
android:id="@+id/sendEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btOutput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="To the next screen"/>
</LinearLayout>
sub_activity.xml
<LinearLayout ~~ Omitted ~~>
<TextView
android:id="@+id/outputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40dp"/>
</LinearLayout>
Application startup: MainActivity processing (the first screen is displayed by this processing) ↓ Send button event occurrence: OutputActivity processing (second screen is displayed)
In other words, when this submit button event occurs, the string data entered by the user is sent from the MainActivity to the OutputActivity using an intent. The intent will be prepared when the event occurs, so write it in the listener class.
The part that puts data in a box called an intent
MainActivity.java
//Create an intent object
Intent intent = new Intent(MainActivity.this,OutputActivity.class);
//Store data to be sent to screen ②
intent.putExtra("inputText",inputStr);
//Launch screen
startActivity(intent);
Create an intent object ↓ Store the data stored in the intent with putExtra ↓ Launch the next screen
When creating an intent object, describe the Activity class of the original screen in the first argument and the Activity class of the screen you want to send in the second argument.
SubActivity.java
//Get the intent object created on screen ①
Intent intent = getIntent();
//Get character information and output to screen
TextView output = findViewById(R.id.outputText);
String outputText = intent.getStringExtra("inputText");
output.setText(outputText);
The calling part on the second screen. You can call the intent object created on the initial screen with getIntent. All you have to do now is write the process to display the character string.
In the project folder, there is a file called AndroidManifest that contains the information required to set up Android.
If there are two or more screens, the app will not work unless you add the Activity name here. Here, add "
<application ~~ Omitted ~~>
<activity android:name=".MainActivity">
~~ Omitted ~~
</activity>
<activity android:name=".OutputActivity"></activity>
</application>
I finished two introductory books in about a month. Although my understanding gradually progresses, I'm still an amateur.
Next time, I will summarize about the life cycle
Recommended Posts