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

안드로이드(자바) 전화번호 찍기 예제

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

 

 

 

package kr.co.aiai.app;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity8 extends AppCompatActivity {

    Button[] btn = new Button[10];
    Integer[] btn_id = {R.id.btn0,
                        R.id.btn1,
                        R.id.btn2,
                        R.id.btn3,
                        R.id.btn4,
                        R.id.btn5,
                        R.id.btn6,
                        R.id.btn7,
                        R.id.btn8,
                        R.id.btn9
                        };
    Button shortToastButton;
    TextView tv;
//    Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btncall;

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

        tv = findViewById(R.id.tv);
        shortToastButton = findViewById(R.id.btncall);

        shortToastButton.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Toast toast = Toast.makeText(getApplicationContext(),"Calling"+ tv.getText(), Toast.LENGTH_SHORT);
                toast.show();
                tv.setText("");
            }
        });

        for(int i=0; i<btn_id.length; i++){
            btn[i] = (Button) findViewById(btn_id[i]);
            int finalI = i;
            btn[i].setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    setStr(finalI);
                }
            });
        }
    }

    public void setStr(int finalI){
        String tvStr = (String)tv.getText();
        String btnStr =(String)btn[finalI].getText();

        tv.setText(tvStr + btnStr);
    }
}

 

 

 

<?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/tv"
                android:layout_width="360dp"
                android:layout_height="wrap_content"
                android:text=""
                android:textSize="25dp" />

        </LinearLayout>

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

            <Button
                android:id="@+id/btn1"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:text="1" />
            <Button
                android:id="@+id/btn2"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:text="2" />
            <Button
                android:id="@+id/btn3"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:text="3" />
        </LinearLayout>

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

            <Button
                android:id="@+id/btn4"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:text="4" />
            <Button
                android:id="@+id/btn5"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:text="5" />
            <Button
                android:id="@+id/btn6"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:text="6" />
        </LinearLayout>

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

            <Button
                android:id="@+id/btn7"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:text="7" />
            <Button
                android:id="@+id/btn8"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:text="8" />
            <Button
                android:id="@+id/btn9"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:text="9" />
        </LinearLayout>

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

            <Button
                android:id="@+id/btn0"
                android:layout_width="120dp"
                android:layout_height="wrap_content"
                android:text="0" />
            <Button
                android:id="@+id/btncall"
                android:layout_width="240dp"
                android:layout_height="wrap_content"
                android:text="call" />

        </LinearLayout>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

 

결과

 

 

 

 


선생님 풀이

 

package kr.co.aiai.app;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity8 extends AppCompatActivity {

    TextView tv;

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

        tv = findViewById(R.id.tv);
        Button btn1 = findViewById(R.id.btn1);
        Button btn2 = findViewById(R.id.btn2);
        Button btn3 = findViewById(R.id.btn3);
        Button btn4 = findViewById(R.id.btn4);
        Button btn5 = findViewById(R.id.btn5);

        Button btn6 = findViewById(R.id.btn6);
        Button btn7 = findViewById(R.id.btn7);
        Button btn8 = findViewById(R.id.btn8);
        Button btn9 = findViewById(R.id.btn9);
        Button btn0 = findViewById(R.id.btn0);

        Button btn_call = findViewById(R.id.btn_call);

        btn1.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {myclick(v);}});
        btn2.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {myclick(v);}});
        btn3.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {myclick(v);}});
        btn4.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {myclick(v);}});
        btn5.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {myclick(v);}});

        btn6.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {myclick(v);}});
        btn7.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {myclick(v);}});
        btn8.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {myclick(v);}});
        btn9.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {myclick(v);}});
        btn0.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {myclick(v);}});

        btn_call.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {mycall();}});

    }
    public void mycall() {
        String str_tel = tv.getText().toString();
        Toast myToast = Toast.makeText(this.getApplicationContext(),"CALLING " + str_tel, Toast.LENGTH_SHORT);
        myToast.show();
    }

    public void myclick(View v) {
        Button imsi = (Button) v;
        String str_old = tv.getText().toString();
        String str_new = imsi.getText().toString();
        tv.setText(str_old+str_new);
        Log.d("Lee",imsi.getText().toString());
    }

}