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

안드로이드(자바) 별찍기 예제

by 미눅스[멘토] 2023. 6. 27.
728x90

 

 

package kr.co.aiai.app;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity7 extends AppCompatActivity {

    EditText et_first;
    EditText et_last;
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main7);

        et_first = findViewById(R.id.et_first);
        et_last = findViewById(R.id.et_last);
        tv= findViewById(R.id.tv);
        Button btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myclick();
            }
        });
    }

        public String getStar(int cnt){
        String ret="";
        for(int i=0; i<cnt; i++){
            ret +="*";
        }
            ret+="\n";
            return ret;
        }

        private void myclick(){
            String a =et_first.getText().toString();
            String b =et_last.getText().toString();

            int aa= Integer.parseInt(a);
            int bb= Integer.parseInt(b);

            String txt="";
            for(int i=aa; i<=bb; i++) {
                    txt+=getStar(i);
                }

             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/tv1"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="첫별수:"
                android:textSize="25dp" />

            <EditText
                android:id="@+id/et_first"
                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="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv2"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="끝별수:"
                android:textSize="25dp" />

            <EditText
                android:id="@+id/et_last"
                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>