[JAVA] Beginners try using android studio Part 2 (event processing)

Event processing with android app

Next, let's make a simple event processing application with android studio. See below for the Hello World app.

Beginners try using android studio Part 1 (Hello World)

launch android studio

First, start android studio and create a new project with ** Empty Activity **.

説明_01.jpg

Operation screen

The operation result looks like this. Enter characters in the input field and press the send button to display the characters in the output field.

説明_03_2.jpg

説明_04_1.jpg

Event side coding

Describes the operation when the button is pressed.

MainActivity.java



package com.example.personal.sample_1_event;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    //object
    private Button   btnSend;
    private TextView textInput;
    private TextView textOutput;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Layout loading
        setContentView(R.layout.activity_main);

        //Object acquisition
        btnSend    = findViewById(R.id.btnSend);
        textInput  = findViewById(R.id.textInput);
        textOutput = findViewById(R.id.textOutput);

        /////////////////////////////////////////////////////////////////////
        //Listener
        /////////////////////////////////////////////////////////////////////

        //button
        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //Get text
                //CharSequence text = textInput.getText();
                String text = textInput.getText().toString();

                //Text settings
                textOutput.setText(text);
            }
        });

    }

}

Coding of strings used on the screen

Register the character strings used on the screen in ** strings.xml **. Instead of writing the string directly on the screen, enter variables such as textBtnSend.

strings.xml



<resources>
    <string name="app_name">Sample_1_event</string>

    <!--Postscript-->
    <string name="textBtnSend"        >Send</string>
    <string name="textTextLabelInput" >input</string>
    <string name="textTextLabelOutput">output</string>
    <string name="textTextInput"      >Input</string>
    <string name="textTextOutput"     >Output</string>

</resources>

Screen-side coding

** Basically, I don't code directly (I think). ** ** Place parts such as text fields and buttons by dragging and dropping. The screen after placement looks like this.

説明_05.jpg

How to arrange parts

    1. Select a button etc.
  1. Place it on the screen by dragging and dropping.
    1. Select the placed part
  2. Enter the ID at the top right of the screen (unique)
  3. Press the + button at the position below the ID to determine the reference position. → Here, the left wall and upper wall are used as the standard.
  4. In TextView-> text, enter the variable registered in strings.xml.

For the time being, I will also post the sauce.

activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:layout_editor_absoluteY="81dp">

    <Button
        android:id="@+id/btnSend"
        android:layout_width="@dimen/btnSendWidth"
        android:layout_height="@dimen/btnSendHeight"
        android:layout_marginLeft="40dp"
        android:layout_marginStart="40dp"
        android:layout_marginTop="45dp"
        android:text="@string/textBtnSend"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textOutput" />

    <TextView
        android:id="@+id/textLabelInput"
        android:layout_width="40dp"
        android:layout_height="20dp"
        android:layout_marginLeft="40dp"
        android:layout_marginStart="40dp"
        android:layout_marginTop="50dp"
        android:text="@string/textTextLabelInput"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textLabelOutput"
        android:layout_width="40dp"
        android:layout_height="20dp"
        android:layout_marginLeft="40dp"
        android:layout_marginStart="40dp"
        android:layout_marginTop="40dp"
        android:text="@string/textTextLabelOutput"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textLabelInput" />

    <EditText
        android:id="@+id/textInput"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_marginLeft="20dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="40dp"
        android:ems="10"
        android:hint="@string/textTextInput"
        android:inputType="textPersonName"
        android:text=""
        app:layout_constraintStart_toEndOf="@+id/textLabelInput"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/textOutput"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_marginLeft="20dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:hint="@string/textTextOutput"
        android:inputType="textPersonName"
        android:text=""
        app:layout_constraintStart_toEndOf="@+id/textLabelOutput"
        app:layout_constraintTop_toBottomOf="@+id/textInput" />

</android.support.constraint.ConstraintLayout>

Recommended Posts

Beginners try using android studio Part 2 (event processing)
Beginners try using android studio Part 1 (Hello World)
Try using Talend Part 2
Try using Talend Part 1
Defeating Android Studio Part 3-6
Defeating Android Studio Part 1 & Part 2
[Processing] Try using GT Force.
Make an executable jar using Android Studio
Try using the service on Android Oreo
Try using the Emotion API from Android
Try using Firebase Cloud Functions on Android (Java)
(Android) Try to display static text using DataBinding
Android Studio development for the first time (for beginners)
Try communication using gRPC on Android + Java server
Try using libGDX
Try using powermock-mockito2-2.0.2
Try using GraalVM
Try using jmockit 1.48
Try using sql-migrate
Try using SwiftLint
Try using Log4j 2.0
About products using crud processing and devise (for beginners)
Binary tree memorandum for beginners (Part 3 Realization using class)
Asynchronous processing and Web API integration in Android Studio
Try image classification using TensorFlow Lite on Android (JAVA)