xml 디자인
Kotlin 코드
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var resultNum:Int = 0
var flag:Boolean = false
val btn: Array<TextView> = arrayOf(btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9)
for(btn_s in btn){
btn_s.setOnClickListener {
if(result.text.toString()== "0" || flag) {
result.text = btn_s.text.toString()
flag=false
}
else
result.text = result.text.toString()+btn_s.text.toString()
}
}
btn_plus.setOnClickListener {
resultNum += result.text.toString().toInt()
result.text=resultNum.toString()
flag = true
}
btn_CA.setOnClickListener {
resultNum = 0
result.text="0"
}
}
}
xml 코드
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100"
tools:context=".MainActivity">
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:background="#212DB1"
android:gravity="center_vertical|right"
android:text="0"
android:textColor="#fff"
android:textSize="30dp"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="92"
android:orientation="vertical"
android:weightSum="3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/btn_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#215D37"
android:gravity="center"
android:text="1"
android:textColor="#fff"
android:textSize="50dp" />
<TextView
android:id="@+id/btn_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#215D37"
android:gravity="center"
android:text="2"
android:textColor="#fff"
android:textSize="50dp" />
<TextView
android:id="@+id/btn_3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#215D37"
android:gravity="center"
android:text="3"
android:textColor="#fff"
android:textSize="50dp" />
<TextView
android:id="@+id/btn_CA"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#f00"
android:gravity="center"
android:text="CA"
android:textColor="#fff"
android:textSize="50dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/btn_4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#215D37"
android:gravity="center"
android:text="4"
android:textColor="#fff"
android:textSize="50dp" />
<TextView
android:id="@+id/btn_5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#215D37"
android:gravity="center"
android:text="5"
android:textColor="#fff"
android:textSize="50dp" />
<TextView
android:id="@+id/btn_6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#215D37"
android:gravity="center"
android:text="6"
android:textColor="#fff"
android:textSize="50dp" />
<TextView
android:id="@+id/btn_plus"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#f00"
android:gravity="center"
android:text="+"
android:textColor="#fff"
android:textSize="50dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/btn_7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#215D37"
android:gravity="center"
android:text="7"
android:textColor="#fff"
android:textSize="50dp" />
<TextView
android:id="@+id/btn_8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#215D37"
android:gravity="center"
android:text="8"
android:textColor="#fff"
android:textSize="50dp" />
<TextView
android:id="@+id/btn_9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#215D37"
android:gravity="center"
android:text="9"
android:textColor="#fff"
android:textSize="50dp" />
<TextView
android:id="@+id/btn_0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff"
android:gravity="center"
android:text="0"
android:textSize="50dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>