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)
First, start android studio and create a new project with ** Empty Activity **.
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.
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);
}
});
}
}
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>
** 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.
How to arrange parts
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