Android 사용자 정의 button 클릭 효과 의 두 가지 방법
사용자 정의 클릭 효 과 는 두 가지 방식 이 있 습 니 다.하 나 는 xml 에서 정의 하고 다른 하 나 는 코드 에서 정의 합 니 다.
우선 xml 에서 어떻게 정의 하 는 지 봅 시다.
drawable 에서 새 selector.xml 파일 을 만 듭 니 다:
<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
 
  <item android:drawable="@drawable/button_press" android:state_pressed="true"/> 
  <item android:drawable="@drawable/button_nomal" android:state_focused="false" android:state_pressed="false"/> 
  <item android:drawable="@drawable/button_focus" android:state_focused="true"/> 
  <item android:drawable="@drawable/button_nomal" android:state_focused="false"/> 
 
</selector> 
drawable 은 각각 이 세 장의 그림 을 인용 했다.
 
       그리고 main.xml 에 button 단 추 를 추가 합 니 다.
  <Button 
 android:id="@+id/button1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="button    " 
 android:background="@drawable/selector" />  
Button button1=(Button) this.findViewById(R.id.button1); 
    button1.setOnClickListener(new View.OnClickListener() { 
       
      @Override 
      public void onClick(View v) { 
        // TODO Auto-generated method stub 
        Toast.makeText(getApplicationContext(), "    button  ", Toast.LENGTH_SHORT).show(); 
      } 
    }); 
button 클릭 전:
 
 단추 단 추 를 눌 렀 을 때:
 
        다음은 두 번 째 실현 방식 을 보고 코드 에서 실현 합 니 다.
우선 main.xml 에 추가:
   <Button 
   android:id="@+id/button2" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:text="button    " 
   android:background="@drawable/button_nomal"/> 
  Button button2=(Button) this.findViewById(R.id.button2); 
    button2.setOnTouchListener(new OnTouchListener() { 
     
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      if(event.getAction()==MotionEvent.ACTION_DOWN){ 
        v.setBackgroundResource(R.drawable.button_press); 
      }else if(event.getAction()==MotionEvent.ACTION_UP){ 
        v.setBackgroundResource(R.drawable.button_nomal); 
      } 
      return false; 
    } 
  }); 
이상 은 안 드 로 이 드 사용자 정의 button 클릭 효과 구현 방식 의 모든 내용 입 니 다.참고 해 주시 고 많은 응원 부 탁 드 리 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.