투케이2K

59. (AndroidStudio/android/java) setOnTouchListener 사용해 버튼 클릭 시 색상 변경 효과 주기 실시 본문

Android

59. (AndroidStudio/android/java) setOnTouchListener 사용해 버튼 클릭 시 색상 변경 효과 주기 실시

투케이2K 2021. 2. 24. 08:43

/* =========================== */

[ 개발 환경 설정 ]

개발 툴 : AndroidStudio

개발 언어 : java

/* =========================== */

/* =========================== */

[소스 코드]

 

//TODO ==== [xml 파일 원본 색상] ====
android:background="#ff9900"



//TODO ==== [java 버튼 클릭 시 색상 변경 효과] ====
button.setOnTouchListener(new View.OnTouchListener() {
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()){
			case MotionEvent.ACTION_DOWN:
				//TODO === 변경할 색상 ===
				button.setBackgroundColor(Color.parseColor("#ff0000")); 
				break;
			case MotionEvent.ACTION_UP:
				//TODO === 원본 색상 ===
				button.setBackgroundColor(Color.parseColor("#ff9900")); 

				//TODO === 로직 처리 ===
				Intent go = new Intent(A_Main.this, A_Sub.class);
				go.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivity(go);
				overridePendingTransition(0,0);
				break;
			case MotionEvent.ACTION_MOVE:
				break;
		}
		return false;
	}
});

/* =========================== */

반응형
Comments