본문 바로가기

ANDROID

숫자 버튼 만들기

○ 숫자 버튼을 만들고 toast를 통해 표시, 맨 앞에 0이 나오지 않게 만듦

     - 단순히 앞에 있는 버튼 예제와 같은 버튼을 생성한 후 각각의 버튼에 대응하는 변수를 생성해서 소스코드를 작성하였다.

 

import androidx.appcompat.app.AppCompatActivity;

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

public class Number_0420 extends AppCompatActivity {

    private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {


        Button btn [] = new Button[12];

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_number_0420);


        btn[0] = findViewById(R.id.btn0);
        btn[1] = findViewById(R.id.btn1);
        btn[2] = findViewById(R.id.btn2);
        btn[3] = findViewById(R.id.btn3);
        btn[4] = findViewById(R.id.btn4);
        btn[5] = findViewById(R.id.btn5);
        btn[6] = findViewById(R.id.btn6);
        btn[7] = findViewById(R.id.btn7);
        btn[8] = findViewById(R.id.btn8);
        btn[9] = findViewById(R.id.btn9);
        btn[10] = findViewById(R.id.btn_ok);
        btn[11] = findViewById(R.id.btn_clear);
        editText = findViewById(R.id.displayNumber);

        for (int i=0; i<12;i++) {
            btn[i].setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Button btn = (Button) v;
                    editText.append(btn.getText().toString());
                    if (editText.getText().toString().equals("0")) {
                        editText.setText("");
                    }
                    if (btn.getText().toString().equals("Clear")) {
                        editText.setText("");
                    }
                    if (btn.getText().toString().equals("Ok")) {
                        String str = editText.getText().toString();
                        editText.setText(str.substring(0, str.length() - 2));
                        Toast.makeText(getApplicationContext(), editText.getText(), Toast.LENGTH_SHORT).show();
                        editText.setText("");
                    }
                }
            });
        }
    }
}

<앞에 0이 나오지 않게 하는 java 소스코드>

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".Number_0420">

    <EditText
        android:id="@+id/displayNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:gravity="right"
        android:layout_margin="10dp"
        android:paddingTop="50dp"
        android:inputType="textPersonName"
        android:hint="입력기" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40dp"
            android:layout_margin="2dp"
            android:text="1" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40dp"
            android:layout_margin="2dp"
            android:text="2" />

        <Button
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40dp"
            android:layout_margin="2dp"
            android:text="3" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40dp"
            android:layout_margin="2dp"
            android:text="4" />

        <Button
            android:id="@+id/btn5"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40dp"
            android:layout_margin="2dp"
            android:text="5" />

        <Button
            android:id="@+id/btn6"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40dp"
            android:layout_margin="2dp"
            android:text="6" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn7"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40dp"
            android:layout_margin="2dp"
            android:text="7" />

        <Button
            android:id="@+id/btn8"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40dp"
            android:layout_margin="2dp"
            android:text="8" />

        <Button
            android:id="@+id/btn9"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40dp"
            android:layout_margin="2dp"
            android:text="9" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:paddingLeft="2dp"
        android:paddingRight="2dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/empty"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_margin="2dp"
            android:layout_weight="1"
            android:visibility="invisible"
            android:shadowColor="#FFFFFF" />
        <Button
            android:id="@+id/btn0"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40dp"
            android:layout_margin="2dp"
            android:text="0" />

        <Button
            android:id="@+id/btn_clear"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="15dp"
            android:layout_margin="2dp"
            android:text="Clear" />
    </LinearLayout>

    <Button
        android:id="@+id/btn_ok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="35dp"
        android:layout_weight="1"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:text="Ok" />
</LinearLayout>

<화면 설정하는 xml파일>

 

     - 결과 화면

'ANDROID' 카테고리의 다른 글

숫자 입력을 통한 연산  (0) 2021.06.05
INTENT  (0) 2021.06.05
Layout2  (0) 2021.06.03
Layout1  (0) 2021.06.03
버튼 클릭  (0) 2021.06.03