Last time A newcomer tries to summarize Android views (beginner Android application development)
Continued. .. ..
It's been three months since I joined the company. I am studying android application development and I am summarizing it.
This time, about events and listeners. Since it is a demon super basic, I would appreciate it if you think that it is for super beginners. ..
--Event If you tap the youtube app icon, youtube will open. The user operation of tapping this app is called an event.
--Listener android keeps an eye on youtube so that it can be touched at any time. Keeping an eye on this is called a listener.
Last words
--View Screen parts
--Activity The screen itself
androidstudio 3.6.2 openjdk version "11.0.6"
Create an app that simply displays the entered characters with a button tap.
Button tap = event Processing to display the entered characters below = event handler Watch if tapped android function = listener
The import statement is omitted because it will be long.
activity_main.xml
<LinearLayout ~~ Omitted ~~>
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
<Button
android:id="@+id/btClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GoGo!"/>
<TextView
android:id="@+id/tvOutput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text=""
android:textSize="25dp"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Get Button object which is a display button
Button btClick = findViewById(R.id.btClick);
//Create an instance of the listener class
HelloListener listener = new HelloListener();
//Set listener on display button
btClick.setOnClickListener(listener);
}
private class HelloListener implements View.OnClickListener{
@Override
public void onClick(View view){
//Get EditText object which is a name input field
EditText input = findViewById(R.id.etName);
//Get a TextView object to display a message
TextView output = findViewById(R.id.tvOutput);
//Store the input name string in inputStr
String inputStr = input.getText().toString();
//Show message
output.setText(inputStr + "← Entered text");
}
}
}
Unlike the last time, it is divided into java class (MainActivity) and XML class (activity_main). Think of the Java class as creating the listener settings and processing, and the xml as creating the screen configuration (activity).
The important thing this time is ・ Where are the above two files linked? -How to set listeners and write event handlers
is.
MainActivity.java
//Get Button object which is a display button
Button btClick = findViewById(R.id.btClick);
activity_main.xml
<Button
android:id="@+id/btClick"
The usual pattern to get screen parts in java class. R doesn't care now. It can be obtained by findViewById (R.id. Screen part ID name). Store it in a variable of Button object.
Create a listener class → Set the listener in the prepared btnClick → Describe the event handler in the listener class
MainActivity.java
//Create an instance of the listener class
HelloListener listener = new HelloListener();
//Set listener on display button
btClick.setOnClickListener(listener);
With this description, we have gone as far as setting the listener for the button. Enter the event handler in the listener class below.
MainActivity.java
private class HelloListener implements View.OnClickListener{
@Override
public void onClick(View view){
//Get EditText object which is a name input field
EditText input = findViewById(R.id.etName);
//Get a TextView object to display a message
TextView output = findViewById(R.id.tvOutput);
//Store the input name string in inputStr
String inputStr = input.getText().toString();
//Show message
output.setText(inputStr + "← Entered text");
}
}
The screen parts are acquired by findViewId and displayed in TextView.
Please think that the code below, which is not explained, is magical for now. I will write it when I explain the life cycle of the activity later.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Writing a commentary will deepen your understanding. .. Whether or not it can be written in an easy-to-understand manner. .. ..
I will devote myself. Please forgive me that Japanese is messy.
Next time I will write a screen transition!
Recommended Posts