숫자입력기 앱 만들기
<?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=".MainActivity" >
<EditText
android:id="@+id/editTextNumberDecimal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="Input your Number"
android:gravity="bottom|right"
android:textSize="20dp"
android:inputType="numberDecimal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="@+id/btn_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textSize="20dp"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="1" />
<Button
android:id="@+id/btn_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textSize="20dp"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="2" />
<Button
android:id="@+id/btn_3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textSize="20dp"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="@+id/btn_4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textSize="20dp"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="4" />
<Button
android:id="@+id/btn_5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textSize="20dp"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="5" />
<Button
android:id="@+id/btn_6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textSize="20dp"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="@+id/btn_7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textSize="20dp"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="7" />
<Button
android:id="@+id/btn_8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textSize="20dp"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="8" />
<Button
android:id="@+id/btn_9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textSize="20dp"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="9" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="" />
<Button
android:id="@+id/btn_0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textSize="20dp"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="0" />
<Button
android:id="@+id/btn_clear"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="clear" />
</LinearLayout>
<Button
android:id="@+id/btn_ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OK" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity{
EditText editTextNumberDecimal;
String numString="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextNumberDecimal = findViewById(R.id.editTextNumberDecimal);
Button.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_0:
if(numString.equals("")){
numString = "";
}
else {
numString += "0";
}
break;
case R.id.btn_1:
numString += "1";
break;
case R.id.btn_2:
numString += "2";
break;
case R.id.btn_3:
numString += "3";
break;
case R.id.btn_4:
numString += "4";
break;
case R.id.btn_5:
numString += "5";
break;
case R.id.btn_6:
numString += "6";
break;
case R.id.btn_7:
numString += "7";
break;
case R.id.btn_8:
numString += "8";
break;
case R.id.btn_9:
numString += "9";
break;
case R.id.btn_clear:
numString = "";
break;
case R.id.btn_ok:
Toast.makeText(getApplicationContext(),numString,Toast.LENGTH_SHORT).show();
numString = "";
break;
default:
break;
}
//editTextNumberDecimal.setGravity(Gravity.RIGHT | Gravity.BOTTOM);
editTextNumberDecimal.setText(numString);
}
};
Button btn_0 = findViewById(R.id.btn_0);
btn_0.setOnClickListener(onClickListener);
Button btn_1 = findViewById(R.id.btn_1);
btn_1.setOnClickListener(onClickListener);
Button btn_2 = findViewById(R.id.btn_2);
btn_2.setOnClickListener(onClickListener);
Button btn_3 = findViewById(R.id.btn_3);
btn_3.setOnClickListener(onClickListener);
Button btn_4 = findViewById(R.id.btn_4);
btn_4.setOnClickListener(onClickListener);
Button btn_5 = findViewById(R.id.btn_5);
btn_5.setOnClickListener(onClickListener);
Button btn_6 = findViewById(R.id.btn_6);
btn_6.setOnClickListener(onClickListener);
Button btn_7 = findViewById(R.id.btn_7);
btn_7.setOnClickListener(onClickListener);
Button btn_8 = findViewById(R.id.btn_8);
btn_8.setOnClickListener(onClickListener);
Button btn_9 = findViewById(R.id.btn_9);
btn_9.setOnClickListener(onClickListener);
Button btn_clear = findViewById(R.id.btn_clear);
btn_clear.setOnClickListener(onClickListener);
Button btn_ok = findViewById(R.id.btn_ok);
btn_ok.setOnClickListener(onClickListener);
}
}
'PROGRAM > Android' 카테고리의 다른 글
안드로이드스튜디오 Toast 메시지 안뜰 때 (0) | 2021.04.23 |
---|---|
MyCalc App(2) (0) | 2021.04.21 |
EditText, TextView, Button and Toast (0) | 2021.04.20 |
MyAlarm (0) | 2020.10.27 |
App 처음 만들기 - HelloAndroid (0) | 2020.10.21 |