[Android]ImageButton|Button|TextViewクリックとタッチ効果実現
2553 ワード
ImageButton------------------------------------------------>
一、Javaコード:
drawableディレクトリに新しいxmlファイルを追加するbutton_add_x.xmlimageButton.setOnTouchListener(@Override public boolean onTouch(Viewv,MotionEvent event){if(event.getAction()==MotionEvent.ACTION_DOWN){//押下時の背景画像v.setBackgroundResource(R.drawable.pressed); }else if(event.getAction() == MotionEvent.ACTION_UP){//持ち上げ時の画像v.setBackgroundResource(R.drawable.released); } return false; } });二、1)
drawableディレクトリに新しいxmlファイルを追加するbutton_add_x.xml
Button
------------------------------------------------>
Javaコード:
TextView
------------------------------------------------>
フォントの色を変更:Buttonとの違いはtextColorプロパティを変更し、selectorファイルはcolor-->1に定義されています.android:textColor='@color/textview_color'; 2.colorディレクトリに新しいxmlファイルtextview_を追加color.xmlとTextViewの様々な状態での色値の指定
一、Javaコード:
drawableディレクトリに新しいxmlファイルを追加するbutton_add_x.xmlimageButton.setOnTouchListener(@Override public boolean onTouch(Viewv,MotionEvent event){if(event.getAction()==MotionEvent.ACTION_DOWN){//押下時の背景画像v.setBackgroundResource(R.drawable.pressed); }else if(event.getAction() == MotionEvent.ACTION_UP){//持ち上げ時の画像v.setBackgroundResource(R.drawable.released); } return false; } });二、1)
drawableディレクトリに新しいxmlファイルを追加するbutton_add_x.xml
-
-
-
-
)レイアウトファイルに書き込み
Button
------------------------------------------------>
Javaコード:
mBtn = (Button) findViewById(R.id.btn); //btn layout Button ID
mBtn.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View arg0,MotionEvent arg1) {
if(arg1.getAction() == MotionEvent.ACTION_DOWN) {
arg0.setBackgroundResource(R.drawable.pressed); // pressed
} else if(arg1.getAction() == MotionEvent.ACTION_UP) {
arg0.setBackgroundResource(R.drawable.normal); // normal
}
else if() // MotionEvent.ACTION_MOVE MotionEvent.ACTION_CANCEL
return false;
}
});
TextView
------------------------------------------------>
フォントの色を変更:Buttonとの違いはtextColorプロパティを変更し、selectorファイルはcolor-->1に定義されています.android:textColor='@color/textview_color'; 2.colorディレクトリに新しいxmlファイルtextview_を追加color.xmlとTextViewの様々な状態での色値の指定
-
-
-
-