romworld

Python10 - (Andoroid) Toast창 띄우기 본문

Python

Python10 - (Andoroid) Toast창 띄우기

inderrom 2022. 12. 29. 16:02

버튼이벤트

-전화걸고 Toast창 띄우기

 

<AndroidManifest.xml>

<activity_main8.xml>

<?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="409dp"
        android:layout_height="729dp"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="1dp"
        tools:ignore="MissingConstraints">


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

            <EditText
                android:id="@+id/et"
                android:layout_width="280dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="text"
                android:textAlignment="textEnd" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            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="40dp"
            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"
                tools:ignore="DuplicateIds" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            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="40dp"
            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>

디자인은 이렇게 나온다

<MainActivity8.java>

1.첫번째 방법(내가 푼 것)

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 android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

// View.onClickListener를 implements 한다.
public class MainActivity8 extends AppCompatActivity implements View.OnClickListener {
// 버튼과 출력부분을 전역변수로 만들어준다
    Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn0, btnCall;
    EditText et;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main8);
       
       et = findViewById(R.id.et);

        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn3 = findViewById(R.id.btn3);
        btn4 = findViewById(R.id.btn4);
        btn5 = findViewById(R.id.btn5);
        btn6 = findViewById(R.id.btn6);
        btn7 = findViewById(R.id.btn7);
        btn8 = findViewById(R.id.btn8);
        btn9 = findViewById(R.id.btn9);
        btn0 = findViewById(R.id.btn0);
        btnCall = findViewById(R.id.btnCall);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        btn9.setOnClickListener(this);
        btn0.setOnClickListener(this);
        btnCall.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnCall:
                Toast.makeText(getApplicationContext(),et.getText(),Toast.LENGTH_LONG).show();
                break;
            case R.id.btn1:
                et.append("1");
                break;
            case R.id.btn2:
                et.append("2");
                break;
            case R.id.btn3:
                et.append("3");
                break;
            case R.id.btn4:
                et.append("4");
                break;
            case R.id.btn5:
                et.append("5");
                break;
            case R.id.btn6:
                et.append("6");
                break;
            case R.id.btn7:
                et.append("7");
                break;
            case R.id.btn8:
                et.append("8");
                break;
            case R.id.btn9:
                et.append("9");
                break;
            case R.id.btn0:
                et.append("0");
                break;
        }
    }
}

 

2.두번째 방법

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 {
    Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn0, btnCall;
    EditText et;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main8);

        et = findViewById(R.id.et);

        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn3 = findViewById(R.id.btn3);
        btn4 = findViewById(R.id.btn4);
        btn5 = findViewById(R.id.btn5);
        btn6 = findViewById(R.id.btn6);
        btn7 = findViewById(R.id.btn7);
        btn8 = findViewById(R.id.btn8);
        btn9 = findViewById(R.id.btn9);
        btn0 = findViewById(R.id.btn0);

        btnCall = findViewById(R.id.btnCall);

        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(view);});
    }

    public void mycall(View v){
        String str_tel = et.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_new = imsi.getText().toString();
        String str_old = et.getText().toString();
        et.setText(str_old+str_new);
    }
}

 

결과

 

Comments