Android app: Try to summarize events and listeners

Last time A newcomer tries to summarize Android views (beginner Android application development)

Continued. .. ..

Introduction

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. ..

Words coming this time

--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

Anyway sample

environment

androidstudio 3.6.2 openjdk version "11.0.6"

Screen to make

Create an app that simply displays the entered characters with a button tap.

キャプチャ.PNG

Button tap = event Processing to display the entered characters below = event handler Watch if tapped android function = listener

sample

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");
            }
        }
    }

Important points for beginners

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.

Linking java file and screen parts

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.

Listener settings and event handlers

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);

At the end

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

Android app: Try to summarize events and listeners
Android app to select and display images from the gallery
Try to introduce OpenCV to Android application
Try to create a server-client app
Introduction to Android App Development 1 Installing JDK and Android Studio for mac
Trial and error to display national holidays in Android app development. Part 2
Try deploying Rails app to EC2-Part 2 (Deploy)-
Java to C and C to Java in Android Studio
Android development-WEB access (POST) Try to communicate with the outside and send data. ~
I'm making an Android app and I'm stuck with errors and how to solve it
Try to link Ruby and Java with Dapr
[Android] Despaired story with App UserId and SharedUserId
[Raspberry Pi] Try to link Apache2 and Tomcat
(Android) Try to display static text using DataBinding
Try to summarize the common layout with rails
Try App Clips
Android weather app
Try to quit Eclipse and switch to Visual Studio Code
App development beginners tried to make an Android calculator app
Try to implement tagging function using rails and js