Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags more
Archives
Today
Total
관리 메뉴

기록

안드로이드 스튜디오 전화번호 누르기 본문

JAVA

안드로이드 스튜디오 전화번호 누르기

9400 2022. 12. 29. 16:30

화면단 

<?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=".MainActivity8">


    <LinearLayout
        android:id="@+id/LinearLayout"
        android:layout_width="409dp"
        android:layout_height="729dp"
        android:orientation="vertical"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="1dp">


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

            <EditText
                android:id="@+id/et"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="text"
                android:textStyle="italic" />

        </LinearLayout>


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

            <Button
                android:id="@+id/btn1"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="1" />

            <Button
                android:id="@+id/btn2"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="2" />

            <Button
                android:id="@+id/btn3"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="3" />
        </LinearLayout>


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

            <Button
                android:id="@+id/btn4"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="4" />

            <Button
                android:id="@+id/btn5"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="5" />

            <Button
                android:id="@+id/btn6"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="6" />
        </LinearLayout>



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

            <Button
                android:id="@+id/btn7"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="7" />

            <Button
                android:id="@+id/btn8"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="8" />

            <Button
                android:id="@+id/btn9"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="9" />
        </LinearLayout>


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

            <Button
                android:id="@+id/btn0"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="0" />

            <Button
                android:id="@+id/btnCall"
                android:layout_width="200dp"
                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.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity8 extends AppCompatActivity {

    EditText et;

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

        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 btnCall = findViewById(R.id.btnCall);
        et = findViewById(R.id.et);



        btn1.setOnClickListener(view -> {myclick(view); });
        btn2.setOnClickListener(view -> {myclick(view); });
        btn3.setOnClickListener(view -> {myclick(view); });
        btn4.setOnClickListener(view -> {myclick(view); });
        btn5.setOnClickListener(view -> {myclick(view); });
        btn6.setOnClickListener(view -> {myclick(view); });
        btn7.setOnClickListener(view -> {myclick(view); });
        btn8.setOnClickListener(view -> {myclick(view); });
        btn9.setOnClickListener(view -> {myclick(view); });
        btn0.setOnClickListener(view -> {myclick(view); });
        btnCall.setOnClickListener(view -> {
            mycall();
        });

    }

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

    public void myclick(View v){
        Button imsi = (Button) v;
        String str_new = imsi.getText().toString();
        String str_old = et.getText().toString();
        et.setText(str_old+str_new);
    }

}

 

btn1.setOnClickListener(view -> {myclick(view); });

여기서 view는 내가 누른것! 

view는 모든 것의 조상이기대문에 형변환이 가능하다.

 

    public void myclick(View v){
        Button imsi = (Button) v;
        String str_new = imsi.getText().toString();
        String str_old = et.getText().toString();
        et.setText(str_old+str_new);
    }

Button imsi = (Button) v;

로 형변환! 

형변환하여 버튼의 값을 가져와서 출력창에 붙여준다 ! 

 

 

출력 완료 

Comments