안드로이드 개발 질문/답변
(글 수 45,052)
레이어아웃에는 back키를 만들엇는데 소스부분에서 back키를 어떻게 어디에 생성해야하는지 모르겟습니다..ㅜㅜ
btnBack <-요키!!!
package com.example.alter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private Button[] BTNNUM = new Button[10];
private Button[] BTNOP = new Button[6];
private EditText EDITTEXT;
private int NUMBER1;
private int NUMBER2;
//연산자를 기억하는 변수
//no:무연산자 +:덧셈, -:뺄셈 *:곱셈 /:나눗셈, 초기값 0
//0:무연산자 1:덧셈, 2:뺄셈 3:곱셈 4:나눗셈, 초기값 0
private String OPSTR;
private int OPCODE;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//에디트텍스트를 선언하고 객체참조 선언
//에디트텍스트창에 직접적인 입력을 막기위해 키 리스너 파라미터로 null값을 준다.
EDITTEXT=(EditText)findViewById(R.id.input);
EDITTEXT.setKeyListener(null);
//시작전 초기화
clear();
//Initializing Number Buttons
BTNNUM[0] = (Button)findViewById(R.id.btn0);
BTNNUM[1] = (Button)findViewById(R.id.btn1);
BTNNUM[2] = (Button)findViewById(R.id.btn2);
BTNNUM[3] = (Button)findViewById(R.id.btn3);
BTNNUM[4] = (Button)findViewById(R.id.btn4);
BTNNUM[5] = (Button)findViewById(R.id.btn5);
BTNNUM[6] = (Button)findViewById(R.id.btn6);
BTNNUM[7] = (Button)findViewById(R.id.btn7);
BTNNUM[8] = (Button)findViewById(R.id.btn8);
BTNNUM[9] = (Button)findViewById(R.id.btn9);
//루프문을 이요하여 숫자 버튼의 각 TAG값으로 해당 버튼의 숫자를 저장하고
//OnClickListener선언
for(int i = 0;i<10;i++){
BTNNUM[i].setTag(i);
BTNNUM[i].setOnClickListener(this);
}
//Initializing Operation Buttons
BTNOP[0] = (Button)findViewById(R.id.btnPlus);
BTNOP[1] = (Button)findViewById(R.id.btnMinus);
BTNOP[2] = (Button)findViewById(R.id.btnMutiply);
BTNOP[3] = (Button)findViewById(R.id.bntDivide);
BTNOP[4] = (Button)findViewById(R.id.btnC);
BTNOP[5] = (Button)findViewById(R.id.btnResult);
//숫자 버튼과 마찬가지로 루프문을 이용하여 OnClickListener선언
for(int i = 0;i<6;i++){
BTNOP[i].setOnClickListener(this);
}
}
//각 버튼의 클릭 이벤트 처리
//스위치문의 파라미터로 각 버튼의 고유한 id 를 받아서 처리
//연산버튼 클릭할 경우, 연속적인 계산 과정 중 중간 값을 보여주기 위해 getResult() 메서드를 호출하여 중간 값 출력
public void onClick(View v) {
switch(v.getId()){
case R.id.btnPlus:
getResult();
OPCODE=1;
OPSTR="+";
EDITTEXT.setText(Integer.toString(NUMBER1)+OPSTR);
break;
case R.id.btnMinus:
getResult();
OPCODE=2;
OPSTR="-";
EDITTEXT.setText(Integer.toString(NUMBER1)+OPSTR);
break;
case R.id.btnMutiply:
getResult();
OPCODE=3;
OPSTR="*";
EDITTEXT.setText(Integer.toString(NUMBER1)+OPSTR);
break;
case R.id.bntDivide:
getResult();
OPCODE=4;
OPSTR="/";
EDITTEXT.setText(Integer.toString(NUMBER1)+OPSTR);
break;
case R.id.btnResult:
getResult();
break;
case R.id.btnC:
clear();
break;
default:
if(OPCODE==0){
NUMBER1=NUMBER1*10 + Integer.valueOf(v.getTag().toString());
EDITTEXT.setText(Integer.toString(NUMBER1));
}
else{
NUMBER2=NUMBER2*10 + Integer.valueOf(v.getTag().toString());
EDITTEXT.setText(Integer.toString(NUMBER1)+OPSTR+Integer.toString(NUMBER2));
}
break;
}
}
//연산 결과 NUMBER1 에 저장하여
//새로운 입력값을 저장하는 NUMBER2 와의 연산에 사용한다.
public void getResult(){
if(OPCODE!=0 && NUMBER2!=0){
//OPCODE 를 식별자로 이벤트처리
//1:덧셈 2:뺄셈 3:곱셈 4:나눗셈
switch(OPCODE){
case 1:
NUMBER1=NUMBER1+NUMBER2;
EDITTEXT.setText(""+Integer.toString(NUMBER1));
NUMBER2=0;
break;
case 2:
NUMBER1=NUMBER1-NUMBER2;
EDITTEXT.setText(""+Integer.toString(NUMBER1));
NUMBER2=0;
break;
case 3:
NUMBER1=NUMBER1*NUMBER2;
EDITTEXT.setText(""+Integer.toString(NUMBER1));
NUMBER2=0;
break;
case 4:
NUMBER1=NUMBER1/NUMBER2;
EDITTEXT.setText(""+Integer.toString(NUMBER1));
NUMBER2=0;
break;
}
}
}
//Initializing Calculator
public void clear(){
NUMBER1= 0;
NUMBER2= 0;
OPCODE= 0;
OPSTR="";
EDITTEXT.setText(Integer.toString(NUMBER1));
}
}




소스보면...숫자버튼하고 계산버튼 각각 배열로 생성해서 하셨는데...
백버튼도 이와 동일하게 배열로 생성해서 하셔도 되고, 1개니까 그냥 변수로 선언하여 해주시면 됩니다.
예를들어...
// 변수선언
private Button BTNBACK;
// onCreate에서
BTNBACK = (Button)findViewById(R.id.btnBack);
BTNBACK.setOnClickListener(this);
// public void onClick(View v) { 에서 케이스문 추가
case R.id.btnBack: