본문 바로가기
안드로이드/안드로이드 연습

안드로이드(자바) 구구단 출력 예제

by 미눅스[멘토] 2023. 6. 27.
728x90
package kr.co.aiai.app;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity6 extends AppCompatActivity {

    EditText et;
    TextView tv;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);

        et= findViewById(R.id.editTextNumber);
        tv = findViewById(R.id.tv);
        btn = findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myclick();
            }
        });
    }
    public void myclick(){
        String a= et.getText().toString();
        int aa= Integer.parseInt(a);
        String txt ="";

        txt +=aa+"*" +1 + "=" +(aa*1)+"\n";
        txt +=aa+"*" +2 + "=" +(aa*2)+"\n";
        txt +=aa+"*" +3 + "=" +(aa*3)+"\n";
        txt +=aa+"*" +4 + "=" +(aa*4)+"\n";
        txt +=aa+"*" +5 + "=" +(aa*5)+"\n";
        txt +=aa+"*" +6 + "=" +(aa*6)+"\n";
        txt +=aa+"*" +7 + "=" +(aa*7)+"\n";
        txt +=aa+"*" +8 + "=" +(aa*8)+"\n";
        txt +=aa+"*" +9 + "=" +(aa*9)+"\n";


        tv.setText(txt);
    }
}

 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="출력단수:"
                android:textSize="25dp" />

            <EditText
                android:id="@+id/editTextNumber"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="number" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btn"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:text="Button" />
        </LinearLayout>

        <TextView
            android:id="@+id/tv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="TextView" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

결과