** The gateway to app development ** is a counter app (appropriate) Including confirmation of one's own understanding We will create a counter app that does not use global variables. The requirements are as follows.
As prerequisite knowledge
For people who have been touching JS until now but want to make a native application
First of all, it is a function, but for the time being it is in the res / layout directory Play with the XML file with the GUI (you can write it in code). Like this.
This is the code
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:id="@+id/MainView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
tools:context="com.example.name.app_test_android.MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="67dp"
android:layout_height="35dp"
android:layout_centerHorizontal="true"
android:layout_marginEnd="134dp"
android:layout_marginStart="183dp"
android:layout_marginTop="105dp"
android:text="0"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
style="@android:style/Widget.Button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="108dp"
android:layout_marginStart="54dp"
android:layout_marginTop="355dp"
android:onClick="plus" //Additional points
android:text="+"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
style="@android:style/Widget.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="108dp"
android:layout_marginEnd="56dp"
android:layout_marginTop="355dp"
android:onClick="minus" //Additional points
android:text="-"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
By arranging Buttons and TextViews appropriately in the GUI, A smart IDE will add object information to XML without permission.
However, this time to add an event. android: onClick = "plus or minus" is added, Other than that, you can play with the GUI. Next, let's write the definition of the method to be called in the Click event. From here, it may be difficult to understand without programming.
By not using global variables, Basically, it parses the value of TextView. ** View-related Value acquisition seems to be a little quirky **, It seems that the TextValue itself of the acquired TextView is converted by toString ().
MainActivity.java
package com.example.name.app_test_android;
import android.support.v7.app.AppCompatActivity;
import java.lang.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void plus(View view) {
TextView set_view = (TextView) findViewById(R.id.textView1);
//Convert Text Value to String type
String countView = new String((set_view.getText()).toString());
if(isNumber(countView) == true) {
int mCount = Integer.parseInt(countView);
mCount++;
set_view.setText(String.valueOf(mCount));
}
}
public void minus(View view) {
TextView set_view = (TextView) findViewById(R.id.textView1);
String countView = new String((set_view.getText()).toString());
if(isNumber(countView) == true) {
int mCount = Integer.parseInt(countView);
mCount--;
set_view.setText(String.valueOf(mCount));
}
}
//String integer judgment
private static boolean isNumber(String str) {
try {
Integer.parseInt(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
Click here for reference site https://qiita.com/hys-rabbit/items/1251e86d9d22507d309a https://qiita.com/gabu/items/6ed716bb9af7cc3c26a3
Recommended Posts