마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

버튼 누르면 바뀌었던 배경이 다시 바뀌게 하는 방법좀 알려주세요

–1 추천
LinearLayout의 배경을 photo1로 지정해놓고

 

버튼을 누르면 배경이 photo2로 바뀌게 하는 것 까지는 되는데

 

다시한번 버튼을 누르면 photo1로 바뀌는 방법을 좀 알려주셨으면 합니다 ..ㅠ
시파르라마 (170 포인트) 님이 2013년 5월 2일 질문

2개의 답변

0 추천
togglebutton 을 쓰세요 그리고 checked 를 이용하세요
aucd29 (218,390 포인트) 님이 2013년 5월 3일 답변
0 추천

////////////////////////////////////////////////  MainActivity /////////////////////////////////////////////////////

package com.example.backgroundexam;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

 public boolean SWITCH = false;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  Button btnBackground = (Button)findViewById(R.id.btnBackground);
  btnBackground.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    LinearLayout background = (LinearLayout)findViewById(R.id.background);
    if(SWITCH)
    {
     background.setBackgroundResource(R.drawable.photo1);
     SWITCH = false;
    }
    else{
     background.setBackgroundResource(R.drawable.photo2);
     SWITCH = true;
    }
     
   }
  });
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}

<!-----------------------------activity_main.xml ---------------------------------------------->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/photo1"
    android:id="@+id/background">

    <Button
        android:id="@+id/btnBackground"
        android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:text="백그라운드 변경"/>
   
</LinearLayout>

 

 

boolean 값으로 switch를 만들어서 사용하시면 됩니다.

설정을 저장하고 싶으시다면, preference를 검색해 보세요.

아니면 http://blog.naver.com/ijoos?Redirect=Log&logNo=60134077256 이곳을 한번 보세요.

센스가이 (2,010 포인트) 님이 2013년 5월 3일 답변
센스가이님이 2013년 5월 3일 수정
...