본문 바로가기
안드로이드

[Android] 숫자 +/-

by 엘딘 2022. 4. 5.
activity_main.xml
<TextView
        android:id="@+id/counter_num"
        android:text="0"
        android:gravity="center"
        android:layout_width="500dp"
        android:layout_height="90dp"
        android:textSize="50dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="224dp"/>
<Button
        android:text="+"
        android:layout_width="230dp"
        android:textSize="50dp"
        android:layout_height="150dp"
        android:id="@+id/plusBtn"
        android:layout_marginTop="44dp"
        app:layout_constraintTop_toBottomOf="@+id/counter_num"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintHorizontal_bias="0.135"/>
<Button
        android:text="-"
        android:textSize="50dp"
        android:layout_width="230dp"
        android:layout_height="150dp"
        android:id="@+id/minusBtn"
        android:layout_marginTop="44dp"
        app:layout_constraintTop_toBottomOf="@+id/counter_num"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintHorizontal_bias="0.864"/>

<Button
        android:text="reset"
        android:textSize="40dp"
        android:layout_width="500dp"
        android:layout_height="100dp"
        android:id="@+id/resetBtn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginTop="48dp"
        app:layout_constraintTop_toBottomOf="@+id/plusBtn"/>
<EditText
        android:id="@+id/editText"
        android:layout_width="330dp"
        android:layout_height="100dp"
        android:inputType="number"
        android:text="0"
        android:ems="10"
        android:textSize="40dp"
        android:gravity="center"
        android:layout_marginTop="28dp"
        app:layout_constraintTop_toBottomOf="@+id/resetBtn"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="48dp"/>
<Button
        android:id="@+id/setBtn"
        android:text="set"
        android:textSize="35dp"
        android:layout_width="150dp"
        android:layout_height="100dp"
        android:onClick="onClick"
        app:layout_constraintStart_toEndOf="@+id/editText"
        android:layout_marginTop="28dp"
        android:layout_marginStart="8dp"
        app:layout_constraintTop_toBottomOf="@+id/resetBtn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.218"/>

 

 

MainActivity
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var counter:Int = 0

        val counter_num = findViewById<TextView>(R.id.counter_num)
        val plusBtn = findViewById<Button>(R.id.plusBtn)
        val minusBtn = findViewById<Button>(R.id.minusBtn)
        val resetBtn = findViewById<Button>(R.id.resetBtn)

        plusBtn.setOnClickListener{
        	// counter 숫자에서 ++(+1씩 증가)
            counter++
            // TextView에 숫자 출력
            counter_num.text = counter.toString()
        }
        minusBtn.setOnClickListener {
            counter--
            counter_num.text = counter.toString()
        }
        resetBtn.setOnClickListener {
            counter = 0
            counter_num.text = "0"
        }

        val editText = findViewById<EditText>(R.id.editText)
        val setBtn = findViewById<Button>(R.id.setBtn)

        setBtn.setOnClickListener {
        	// 원하는 숫자 입력
            counter_num.text = editText.text
            counter = editText.text.toString().toInt()
        }
    }

}

'안드로이드' 카테고리의 다른 글

[Android] 숫자 야구  (0) 2022.04.06
[Android] GoogleMapsFragment  (0) 2022.04.05
[Android] Geocoding  (0) 2022.04.03
[안드로이드] Camera  (0) 2022.04.03
[Android] 동적버튼  (0) 2022.04.03

댓글