오늘은 계산기를 만들어보겠습니다.
이전에 배운내용 처럼
LinearLayout android:orientation="vertical" 옵션과 android:layout_weight 옵션을 사용 하여 화면을 굿헝합니다.
android_weight 는 비율로 생각하시면 됩니다.
4:1:1:1
<LinearLayout 세로 >
<LinearLayout 가로 > 가로 가로 가로 ... <LinearLayout >
<LinearLayout 가로 > 가로 가로 가로 ... <LinearLayout >
<LinearLayout 가로 > 가로 가로 가로 ... <LinearLayout >
...
</LinearLayout >
이런식으로 구성합니다.
res/layout/activity_calculator.xml
<!--?xml version="1.0" encoding="utf-8"?--> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="3"> <TextView android:id="@+id/text_result" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="right" android:textSize="30dip" > </TextView> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="1"> <Button android:id="@+id/button_back" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="←" android:layout_weight="1"/> <Button android:id="@+id/button_clear" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="C" android:layout_weight="1"/> <Button android:id="@+id/button_equal" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="=" android:layout_weight="1"/> <Button android:id="@+id/button_plus" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="+" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="1"> <Button android:id="@+id/button_7" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="7" android:layout_weight="1" /> <Button android:id="@+id/button_8" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="8" android:layout_weight="1"/> <Button android:id="@+id/button_9" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="9" android:layout_weight="1" /> <Button android:id="@+id/button_minus" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="-" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="1"> <Button android:id="@+id/button_4" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="4" android:layout_weight="1" /> <Button android:id="@+id/button_5" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="5" android:layout_weight="1"/> <Button android:id="@+id/button_6" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="6" android:layout_weight="1" /> <Button android:id="@+id/button_multi" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="*" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="1"> <Button android:id="@+id/button_1" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="1" android:layout_weight="1"/> <Button android:id="@+id/button_2" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="2" android:layout_weight="1"/> <Button android:id="@+id/button_3" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="3" android:layout_weight="1"/> <Button android:id="@+id/button_devide" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="/" android:layout_weight="1"/> </LinearLayout> </LinearLayout>
Layout_weight 를 사용할때 android:layout_height="wrap_content"를 사용해주세요
결과
public class CalculatorActivity extends Activity implements OnClickListener { private Button b1; private Button b2; private Button b3; private Button b4; private Button b5; private Button b6; private Button b7; private Button b8; private Button b9; private Button b_claer; private Button b_back; private Button b_devide; private Button b_plus; private Button b_minus; private Button b_multi; private Button b_equal; private TextView result; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_calculator ); b1 = (Button) findViewById( R.id.button_1 ); b1.setOnClickListener(this); b2 = (Button) findViewById( R.id.button_2 ); b2.setOnClickListener(this); b3 = (Button) findViewById( R.id.button_3 ); b3.setOnClickListener(this); b4 = (Button) findViewById( R.id.button_4 ); b4.setOnClickListener(this); b5 = (Button) findViewById( R.id.button_5 ); b5.setOnClickListener(this); b6 = (Button) findViewById( R.id.button_6 ); b6.setOnClickListener(this); b7 = (Button) findViewById( R.id.button_7 ); b7.setOnClickListener(this); b8 = (Button) findViewById( R.id.button_8 ); b8.setOnClickListener(this); b9 = (Button) findViewById( R.id.button_9 ); b9.setOnClickListener(this); b_claer = (Button) findViewById( R.id.button_clear ); b_claer.setOnClickListener(this); b_back = (Button) findViewById( R.id.button_back ); b_back.setOnClickListener(this); b_devide = (Button) findViewById( R.id.button_devide ); b_devide.setOnClickListener(this); b_plus = (Button) findViewById( R.id.button_plus ); b_plus.setOnClickListener(this); b_minus = (Button) findViewById( R.id.button_minus ); b_minus.setOnClickListener(this); b_multi = (Button) findViewById( R.id.button_multi ); b_multi.setOnClickListener(this); b_equal = (Button) findViewById( R.id.button_equal ); b_equal.setOnClickListener(this); result = (TextView) findViewById( R.id.text_result ); } @Override public void onClick(View v) { if( v.equals( b1 )){ if( result.getText().length() == 1 && "0".equals(result.getText())){ result.setText("1"); }else { result.setText( result.getText()+"1"); } }else if( v.equals( b2 )){ if( result.getText().length() == 1 && "0".equals(result.getText())){ result.setText("2"); }else { result.setText( result.getText()+"2"); } }else if( v.equals( b3 )){ if( result.getText().length() == 1 && "0".equals(result.getText())){ result.setText("3"); }else { result.setText( result.getText()+"3"); } }else if( v.equals( b4 )){ if( result.getText().length() == 1 && "0".equals(result.getText())){ result.setText("4"); }else { result.setText( result.getText()+"4"); } }else if( v.equals( b5 )){ if( result.getText().length() == 1 && "0".equals(result.getText())){ result.setText("5"); }else { result.setText( result.getText()+"5"); } }else if( v.equals( b6 )){ if( result.getText().length() == 1 && "0".equals(result.getText())){ result.setText("6"); }else { result.setText( result.getText()+"6"); } }else if( v.equals( b7 )){ if( result.getText().length() == 1 && "0".equals(result.getText())){ result.setText("7"); }else { result.setText( result.getText()+"7"); } }else if( v.equals( b8 )){ if( result.getText().length() == 1 && "0".equals(result.getText())){ result.setText("8"); }else { result.setText( result.getText()+"8"); } }else if( v.equals( b9 )){ if( result.getText().length() == 1 && "0".equals(result.getText())){ result.setText("9"); }else { result.setText( result.getText()+"9"); } }else if( v.equals( b_claer )){ result.setText( "0"); }else if( v.equals( b_back )){ if( result.getText().length() != 0 ) { result.setText( result.getText().subSequence( 0 , result.getText().length()-1)); } }else if( v.equals( b_devide )){ result.setText( result.getText()+"/"); }else if( v.equals( b_plus )){ result.setText( result.getText()+"+"); }else if( v.equals( b_minus )){ result.setText( result.getText()+"-"); }else if( v.equals( b_multi )){ result.setText( result.getText()+"X"); }else if( v.equals( b_equal )){ result.setText( calc() ); } } private String calc() { // 다음시간에 계속 return "계산"; } }
계산부분은 수정을 좀 해야하네요
'안드로이드' 카테고리의 다른 글
[안드로이드 강좌 예제 따라하기 6] 커스텀 뷰 및 Bitmap 표시 (0) | 2014.11.04 |
---|---|
[안드로이드 강좌 예제 따라하기 5] 계산기만들기 - 계산부분(2/2) (2) | 2014.11.04 |
[안드로이드 강좌 예제 따라하기 3] 액티비티 전환 다른액티비티 실행 (0) | 2014.11.04 |
[안드로이드 강좌 예제 따라하기 2] 기존 프로젝트 불러오기 (0) | 2014.11.04 |
티스토리 블로그 자바소스 코드 넣기 (0) | 2014.11.02 |