2013-05-15 3 views
2

저는 Android를 처음 접하면서 클릭하면 어떻게 다른 이미지를 가질 수 있는지 알아 내려고합니다. 두 이미지가 있습니다. 하나는 정상 상태이고 다른 하나는 onclick 상태입니다. onclick 상태 이미지를 설정할 수 없습니다.클릭시 이미지 버튼을 변경하는 방법

<ImageButton 
    android:id="@+id/button_information" 
    android:layout_width="90dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:onClick="@drawable/em_info_active" 
    android:background="@drawable/em_info_active" 
    android:src="@drawable/em_info" /> 

<ImageButton 
    android:id="@+id/button_settings" 
    android:layout_width="90dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_toLeftOf="@+id/button_information" 
    android:background="@drawable/em_prefs_active" 
    android:src="@drawable/em_prefs" /> 

하고 해당 자바 코드입니다 :

public void onClick(final View v) { 
    switch (v.getId()) { 

     case R.id.button_information: 
      leaveMainActivity(); 
      InformationActivity.newInstance(getActivity()); 
      break; 
     case R.id.button_settings: 
      leaveMainActivity(); 
      PreferencesActivity.newInstance(getActivity()); 
      break; 
    } 
} 

저도 같은 대해 갈 수있는 방법을 단서

여기 코드는? 감사합니다. 저스틴

+0

state_pressed에 drawable을 정의 할 배경으로 selector를 사용하십시오. –

답변

3

는 imagebtn.xml 같은 그릴 수있는 xml 파일을 만들고 붙여이 코드

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_focused="true" android:drawable="@drawable/your_imagename_while_focused"/> 
<item android:state_pressed="true" android:drawable="@drawable/your_imagename_while_pressed" /> 
<item android:drawable="@drawable/image_name_while_notpressed" /> 
</selector> 

과 집중 및 프레스에 활동에

button_information.setBackgroundResource(R.drawable.imagebtn); 

배경 이미지 변경이 시도. 이것이 작동하는지 알려주세요.

+0

다른 단추 이미지를 만들 때마다 선택기를 사용하여 imagebtn 클래스를 만들어야합니다. 그렇지 않으면 item1,2 등을 들여 쓰고 호출 할 수 있습니다. 활동 등급? \ –

+0

당신은 이미지 버튼에이 파일에 src를 설정할 수 있습니다. –

관련 문제