重写按钮---attr属性的使用
?首先呢 我们重写两个按钮事件 一个图片button 一个用文字button
package com.pocketjourney.view;import com.pocketjourney.tutorials.R;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.widget.Button;public class TextOnlyButton extends Button {private int notFocusedTextColor, focusedTextColor, pressedTextColor;private boolean isTextPressed;public TextOnlyButton(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init(attrs);}public TextOnlyButton(Context context, AttributeSet attrs) {super(context, attrs);init(attrs);}public TextOnlyButton(Context context) {super(context);throw new RuntimeException("Valid colors (e.g. #ffffff) must be passed to this class via the XML parameters: pj:textColorNotFocused & pj:textColorFocused.");}private void init(AttributeSet attrs) {TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.TextOnlyButton);String textColorNotFocused = a.getString(R.styleable.TextOnlyButton_textColorNotFocused);String textColorFocused = a.getString(R.styleable.TextOnlyButton_textColorFocused);String textColorPressed = a.getString(R.styleable.TextOnlyButton_textColorPressed); if (textColorNotFocused != null && textColorFocused != null && textColorPressed != null ) { notFocusedTextColor = a.getColor(R.styleable.TextOnlyButton_textColorNotFocused, 0xFF000000); focusedTextColor = a.getColor(R.styleable.TextOnlyButton_textColorFocused, 0xFF000000); pressedTextColor = a.getColor(R.styleable.TextOnlyButton_textColorPressed, 0xFF000000); } else { throw new RuntimeException("Valid colors (e.g. #ffffff) must be passed to this class via the XML parameters: pj:textColorNotFocused, pj:textColorFocused, & pj:textColorPressed."); }}public void onDrawBackground(Canvascanvas) {// Override this method & do nothing. This prevents the parent.onDrawBackground(canvas) // from drawing the button's background.}/** * Capture mouse press events to update text state. */@Overridepublic boolean onTouchEvent(MotionEventevent){Log.d("TextOnlyButton",event.getAction()+"");if (event.getAction() == MotionEvent.ACTION_DOWN) {isTextPressed = true;// Request a redraw to update the text colorinvalidate();} else if (event.getAction() == MotionEvent.ACTION_UP) {isTextPressed = false;// Requesting focus doesn't work for some reason. If you find a solution to setting // the focus, please let me know so I can update the tutorialrequestFocus();// Request a redraw to update the text colorinvalidate();}return super.onTouchEvent(event);}@Overridepublic void onDraw(Canvas canvas) {if (isTextPressed) {setTextColor(pressedTextColor);}else if (isFocused()) {// Since this Button now has no background. We adjust the text color to indicate focus.setTextColor(focusedTextColor); } else {setTextColor(notFocusedTextColor); }super.onDraw(canvas);}}
?
package com.pocketjourney.view;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.widget.ImageButton;import com.pocketjourney.tutorials.R;public class ImageOnlyButton extends ImageButton {int imageResourceNotFocused, imageResourceFocused, imageResourcePressed;private boolean isButtonPressed;public ImageOnlyButton(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init(attrs);}public ImageOnlyButton(Context context, AttributeSet attrs) {super(context, attrs);init(attrs);}public ImageOnlyButton(Context context) {super(context);throw new RuntimeException("Valid image resource IDs must be passed to this class via the XML parameters: pj:resourceNotFocused & pj:resourceFocused.");}private void init(AttributeSet attrs) {TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.ImageOnlyButton); String notFocusedColorStr = a.getString(R.styleable.ImageOnlyButton_resourceNotFocused); String focusedColorStr = a.getString(R.styleable.ImageOnlyButton_resourceFocused); String pressedColorStr = a.getString(R.styleable.ImageOnlyButton_resourcePressed); if (notFocusedColorStr != null && focusedColorStr != null && pressedColorStr != null) { imageResourceFocused = a.getResourceId(R.styleable.ImageOnlyButton_resourceFocused, -1); imageResourceNotFocused = a.getResourceId(R.styleable.ImageOnlyButton_resourceNotFocused, -1); imageResourcePressed = a.getResourceId(R.styleable.ImageOnlyButton_resourcePressed, -1); } if (imageResourceFocused == -1 || imageResourceNotFocused == -1 || imageResourcePressed == -1) { throw new RuntimeException("Valid image resource IDs must be passed to this class via the XML parameters: pj:resourceNotFocused, pj:resourceFocused, & pj:resourcePressed."); }}/** * Capture mouse press events to update text state. */@Overridepublic boolean onTouchEvent(MotionEventevent){Log.d("TextOnlyButton",event.getAction()+"");if (event.getAction() == MotionEvent.ACTION_DOWN) {// Request a redraw to update the button colorisButtonPressed = true;invalidate();} else if (event.getAction() == MotionEvent.ACTION_UP) {isButtonPressed = false;// Requesting focus doesn't work for some reason. If you find a solution to setting // the focus, please let me know so I can update the tutorial//requestFocus();// Request a redraw to update the button colorinvalidate();}return super.onTouchEvent(event);}@Overridepublic void onDraw(Canvas canvas) {if (isButtonPressed) {setImageResource(imageResourcePressed);}else if (isFocused()) {// Since this Button now has no background. We must swap out the image to display //one that indicates it has focus.setImageResource(imageResourceFocused); } else {setImageResource(imageResourceNotFocused);}super.onDraw(canvas);}}
?这里最主要的是init(AttributeSet attrs) 方法,这里呢引用了资源
也就是自定义的属性,这个属性就是和我们平常一样的android:text一样。首先我们要在这个属性中定义我们添加的资源是什么 这里是整形 也就是id,然后通过上述方法引用 以便我们可以在ondraw里面画出我们的资源。当然text和图片button两个init方法不同 有一个多了一个if的判断 一个没有,多了的一个说明 这个属性是必须设定的 如果不设定就会出错。如果缺少if语句判定那么默认这个属性是可以没有的。
?
<com.pocketjourney.view.TextOnlyButton android:id="@+id/text_only_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="25px" style="?android:attr/buttonStyleSmall" android:text="Text Button" android:background="@drawable/empty" pj:textColorNotFocused="@drawable/white" pj:textColorFocused="@drawable/android_orange" pj:textColorPressed="@drawable/android_yellow"/> <com.pocketjourney.view.ImageOnlyButton android:id="@+id/image_only_button" android:src="@drawable/help" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15px" android:background="@drawable/empty" pj:resourceNotFocused="@drawable/help" pj:resourceFocused="@drawable/help_focused" pj:resourcePressed="@drawable/help_pressed"/>
?
然后呢就可以在主xml中引用我们自写的属性了,这里要注意亮点
第一个? android:background="@drawable/empty"
这个属性我们设置了一个啥都没有的图片以便我们自定义背景
?? pj是自定一的命名空间是必不可缺少的。
源文件可以从福建里面下载。