PDA

View Full Version : چطوری این کد رو واسه یه دکمه بنویسم؟؟؟



hamid20live
جمعه 11 بهمن 1392, 14:13 عصر
لطفا کمک کنید این کد رو چطوری فقط برای یه دکمه بنویسم؟؟؟



@Override
public boolean onTouchEvent(MotionEvent event)
{
boolean[] newButtonStates = new boolean[36];

int action = event.getAction();
boolean isDownAction = (action & 0x36) == 0x36 || action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE;

for (int touchIndex = 0; touchIndex < event.getPointerCount(); touchIndex++)
{
// find button for location
int x = (int) event.getX(touchIndex);
int y = (int) event.getY(touchIndex);

for (int buttonIndex = 0; buttonIndex < buttons.size(); buttonIndex++)
{
View button = buttons.get(buttonIndex);
int[] location = new int[2];
button.getLocationOnScreen(location);
int buttonX = location[0];
int buttonY = location[1];

Rect rect = new Rect(buttonX, buttonY, buttonX + button.getWidth(), buttonY + button.getHeight());
if (rect.contains(x, y))
{
newButtonStates[buttonIndex] = isDownAction;
break;
}
}
}

for (int index = 0; index < newButtonStates.length; index++)
{
if (buttonStates[index] != newButtonStates[index])
{
buttonStates[index] = newButtonStates[index];
View button = buttons.get(index);
toggleButtonSound(button, newButtonStates[index]);
}
}

return true;

rubiks.kde
جمعه 11 بهمن 1392, 14:25 عصر
یه کلاس از Button مشتق کنید و این تابع رو براش بنویسید و بعد استفاده کنید.

public class Mybutton extends Button {

public Mybutton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}

public Mybutton(Context context, AttributeSet attrs,int i) {
super(context, attrs, i);
// TODO Auto-generated constructor stub
}

public Mybutton(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

@Override
public boolean onTouchEvent(MotionEvent event)
{
boolean[] newButtonStates = new boolean[36];

int action = event.getAction();
boolean isDownAction = (action & 0x36) == 0x36 || action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE;

for (int touchIndex = 0; touchIndex < event.getPointerCount(); touchIndex++)
{
// find button for location
int x = (int) event.getX(touchIndex);
int y = (int) event.getY(touchIndex);

for (int buttonIndex = 0; buttonIndex < buttons.size(); buttonIndex++)
{
View button = buttons.get(buttonIndex);
int[] location = new int[2];
button.getLocationOnScreen(location);
int buttonX = location[0];
int buttonY = location[1];

Rect rect = new Rect(buttonX, buttonY, buttonX + button.getWidth(), buttonY + button.getHeight());
if (rect.contains(x, y))
{
newButtonStates[buttonIndex] = isDownAction;
break;
}
}
}

for (int index = 0; index < newButtonStates.length; index++)
{
if (buttonStates[index] != newButtonStates[index])
{
buttonStates[index] = newButtonStates[index];
View button = buttons.get(index);
toggleButtonSound(button, newButtonStates[index]);
}
}

return true;
}
}

hamid20live
جمعه 11 بهمن 1392, 15:06 عصر
اگه امکانش هست یه خورده بیشتر توضیح بدبد

buttonStates[index]
رو چیکار کنم پس؟؟

rubiks.kde
جمعه 11 بهمن 1392, 19:36 عصر
این کدتون کامل نیست.از هرجا گرفتید دوباره مراجعه کنید و کد کامل رو بگیرید.

hamid20live
شنبه 12 بهمن 1392, 14:41 عصر
منظورم این شکلیه :


116257