안드로이드 개발 질문/답변
(글 수 45,052)
AbsoluteLayout 에서 버튼을 배치하고 프로그램에서 랜덤으로 재배치 했는데
TextView 의 TEXT 를 변경하려 하는 배치가 초기로 다시 돌아 옵니다.
연습삼아 1~9까지 짝마추는 카드게임을 연습삼아 만들고 있는데요.
카드 맞추는것 까지 버튼의 택스트를 지우고 넣고 는 정상작동 되는데.
TextView의 TEXT 를 변경하려면 꼭 초기 main.xml 에 배치해놓은 상태로 돌아가는 현상이 나타나네요.
혹시나 해서 TextView 를 LinearLayout 에다 배치해보고 해도 안되고.
물론 해결책은 버튼을 LinearLayout 에 버튼을 3개씩 배치하고 숫자나 태그를 변경하는 방법으로 해도 되지만.
이미 해 놓은거 해결을 보고 싶거든요.
소스입니다.
main.xml
cardGame.java
해결이 안될까요.ㅠㅠ?
TextView 의 TEXT 를 변경하려 하는 배치가 초기로 다시 돌아 옵니다.
연습삼아 1~9까지 짝마추는 카드게임을 연습삼아 만들고 있는데요.
카드 맞추는것 까지 버튼의 택스트를 지우고 넣고 는 정상작동 되는데.
TextView의 TEXT 를 변경하려면 꼭 초기 main.xml 에 배치해놓은 상태로 돌아가는 현상이 나타나네요.
혹시나 해서 TextView 를 LinearLayout 에다 배치해보고 해도 안되고.
물론 해결책은 버튼을 LinearLayout 에 버튼을 3개씩 배치하고 숫자나 태그를 변경하는 방법으로 해도 되지만.
이미 해 놓은거 해결을 보고 싶거든요.
소스입니다.
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:id="@+id/LinearLayout01" android:layout_weight="1" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="100dip"> <Button android:text="Start" android:layout_gravity="center_vertical" android:id="@+id/on_Start" android:layout_width="80px" android:layout_height="48px" android:tag="start"/>
</LinearLayout> <AbsoluteLayout android:id="@+id/LinearLayout02" android:layout_weight="1" android:layout_gravity="center" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:text="1" android:id="@+id/Button01" android:layout_width="48px" android:layout_height="48px" android:layout_x="100px" android:layout_y="0px" android:tag="1"/> <Button android:text="2" android:id="@+id/Button02" android:layout_width="48px" android:layout_height="48px" android:layout_x="148px" android:layout_y="0px" android:tag="2"/> <Button android:text="3" android:id="@+id/Button03" android:layout_width="48px" android:layout_height="48px" android:layout_x="196px" android:layout_y="0px" android:tag="3"/> <Button android:text="4" android:id="@+id/Button04" android:layout_width="48px" android:layout_height="48px" android:layout_x="100px" android:layout_y="48px" android:tag="4"/> <Button android:text="5" android:id="@+id/Button05" android:layout_width="48px" android:layout_height="48px" android:layout_x="148px" android:layout_y="48px" android:tag="5"/> <Button android:text="6" android:id="@+id/Button06" android:layout_width="48px" android:layout_height="48px" android:layout_x="196px" android:layout_y="48px" android:tag="6"/> <Button android:text="7" android:id="@+id/Button07" android:layout_width="48px" android:layout_height="48px" android:layout_x="100px" android:layout_y="96px" android:tag="7"/> <Button android:text="8" android:id="@+id/Button08" android:layout_width="48px" android:layout_height="48px" android:layout_x="148px" android:layout_y="96px" android:tag="8"/> <Button android:text="9" android:id="@+id/Button09" android:layout_width="48px" android:layout_height="48px" android:layout_x="196px" android:layout_y="96px" android:tag="9"/> <Button android:text="1" android:id="@+id/Button10" android:layout_width="48px" android:layout_height="48px" android:layout_x="100px" android:layout_y="144px" android:tag="1"/> <Button android:text="2" android:id="@+id/Button11" android:layout_width="48px" android:layout_height="48px" android:layout_x="148px" android:layout_y="144px" android:tag="2"/> <Button android:text="3" android:id="@+id/Button12" android:layout_width="48px" android:layout_height="48px" android:layout_x="196px" android:layout_y="144px" android:tag="3"/> <Button android:text="4" android:id="@+id/Button13" android:layout_width="48px" android:layout_height="48px" android:layout_x="100px" android:layout_y="192px" android:tag="4"/> <Button android:text="5" android:id="@+id/Button14" android:layout_width="48px" android:layout_height="48px" android:layout_x="148px" android:layout_y="192px" android:tag="5"/> <Button android:text="6" android:id="@+id/Button15" android:layout_width="48px" android:layout_height="48px" android:layout_x="196px" android:layout_y="192px" android:tag="6"/> <Button android:text="7" android:id="@+id/Button16" android:layout_width="48px" android:layout_height="48px" android:layout_x="100px" android:layout_y="240px" android:tag="7"/> <Button android:text="8" android:id="@+id/Button17" android:layout_width="48px" android:layout_height="48px" android:layout_x="148px" android:layout_y="240px" android:tag="8"/> <Button android:text="9" android:id="@+id/Button18" android:layout_width="48px" android:layout_height="48px" android:layout_x="196px" android:layout_y="240px" android:tag="9"/> <TextView android:text="현재시도 : " android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:textSize="16sp" android:layout_marginLeft="10dip" android:layout_x="0px" android:layout_y="336px" android:layout_height="wrap_content"/> <TextView android:text="@string/clickCount" android:id="@+id/TextView01" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:textSize="16sp" android:layout_marginLeft="10dip" android:layout_x="90px" android:layout_y="336px" android:layout_height="wrap_content"/> <TextView android:text="최고 : " android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:textSize="16sp" android:layout_marginLeft="50dip" android:layout_x="200px" android:layout_y="336px" android:layout_height="wrap_content"/> <TextView android:text="@string/clickTotal" android:id="@+id/TextView02" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:textSize="16sp" android:layout_marginLeft="10dip" android:layout_x="290px" android:layout_y="336px" android:layout_height="wrap_content"/> </AbsoluteLayout>
</LinearLayout>
cardGame.java
package com.cardgame;
import java.util.Random; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.TextView;
public class CardGame extends Activity { /** Called when the activity is first created. */ private Button _Button[] = new Button[18]; private int one = 0; private int tow = 0; private int click = 0; private int total = 0; private Button _Buttoncheck[] = new Button[2]; private boolean _isRun = true; private boolean _isStart = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); _Button[0] = (Button) findViewById(R.id.Button01); _Button[1] = (Button) findViewById(R.id.Button02); _Button[2] = (Button) findViewById(R.id.Button03); _Button[3] = (Button) findViewById(R.id.Button04); _Button[4] = (Button) findViewById(R.id.Button05); _Button[5] = (Button) findViewById(R.id.Button06); _Button[6] = (Button) findViewById(R.id.Button07); _Button[7] = (Button) findViewById(R.id.Button08); _Button[8] = (Button) findViewById(R.id.Button09); _Button[9] = (Button) findViewById(R.id.Button10); _Button[10] = (Button) findViewById(R.id.Button11); _Button[11] = (Button) findViewById(R.id.Button12); _Button[12] = (Button) findViewById(R.id.Button13); _Button[13] = (Button) findViewById(R.id.Button14); _Button[14] = (Button) findViewById(R.id.Button15); _Button[15] = (Button) findViewById(R.id.Button16); _Button[16] = (Button) findViewById(R.id.Button17); _Button[17] = (Button) findViewById(R.id.Button18); findViewById(R.id.on_Start).setOnClickListener(MyButtonListener); for(int i=0; i<18; i++){ _Button[i].setOnClickListener(MyButtonListener); }
} private void initGame(){ click = 0; ((TextView)findViewById(R.id.TextView01)).setText(""+click+""); _isStart = true; } private View.OnClickListener on_Click = new View.OnClickListener() { public void onClick(View v){ Button A = (Button) v; Button Blank = (Button) findViewById(R.id.Button01); if (is_Vert(A, Blank) || is_Hori(A, Blank)) do_SwapButtons(A, Blank); } }; private void do_SwapButtons(Button A, Button B) { int Left = A.getLeft(); int Top = A.getTop(); int Right = A.getRight(); int Bottom = A.getBottom(); A.layout(B.getLeft(), B.getTop(), B.getRight(), B.getBottom()); B.layout(Left, Top, Right, Bottom); } private boolean is_Vert(Button A,Button B){ return (A.getLeft() == B.getLeft()) && (Math.abs(A.getTop()-B.getTop()) < 50); }
private boolean is_Hori(Button A,Button B){ return (A.getTop() == B.getTop()) && (Math.abs(A.getLeft()-B.getLeft()) < 50); }
Button.OnClickListener MyButtonListener = new Button.OnClickListener() { public void onClick(View v) { if(v.getTag().toString().equals("start")){ _isStart = false; } if (_isStart == false){ Random _Random = new Random(); int i; for (i=0; i<1000; i++) on_Click.onClick(_Button[_Random.nextInt(17)]); for (i=0; i<18; i++) _Button[i].setText(""); initGame(); }else{ if(one == 0){ _Buttoncheck[0] = (Button)findViewById(v.getId()); if (_Buttoncheck[0].getText().equals("")){ one = Integer.parseInt(v.getTag().toString()); _Buttoncheck[0].setText(""+one+""); click++; } }else{ _Buttoncheck[1] = (Button)findViewById(v.getId()); if(_Buttoncheck[1].getText().equals("")){ tow = Integer.parseInt(v.getTag().toString()); _Buttoncheck[1].setText(""+tow+""); if(one == tow){ one = 0; tow = 0; }else{ one = 0; tow = 0; _isRun = true; mHandler.sendEmptyMessageDelayed(0, 300); } click++; } } score(); } } }; public void score(){ ((TextView)findViewById(R.id.TextView01)).setText(String.valueOf(click)); int panjung = 1; int i; for(i = 0; i<18; i++){ if(_Button[i].getText().equals("")){ panjung = 1; break; }else{ panjung = 0; } } if(panjung == 0){ if(total != 0){ if(total > click){ total = click; } }else{ total = click; } ((TextView)findViewById(R.id.TextView02)).setText(""+total+""); _isStart = false; }
} Handler mHandler = new Handler() { public void handleMessage(Message msg) { if (_isRun == false) return; _Buttoncheck[0].setText(""); _Buttoncheck[1].setText(""); _isRun = false; } }; }
해결이 안될까요.ㅠㅠ?