自定义视图从Button中窃取焦点

时间:2013-05-18 08:56:54

标签: android focus android-custom-view

Layout of the file

这是我的布局的xml文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/idtop"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#00ff00"
    android:gravity="center_horizontal" >

    <TextView
        android:id="@+id/idalarmtime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="30dip"
        android:paddingTop="30dip"
        android:textColor="@color/white"
        android:textSize="100sp" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/lclock"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ff0000" >

    <Button
        android:id="@+id/idsnooze"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="30dip"
        android:background="@color/black"
        android:paddingBottom="30dip"
        android:text="Snooze"
        android:textColor="@color/white" />

    <Button
        android:id="@+id/iddismiss"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="30dip"
        android:background="@color/black"
        android:paddingBottom="30dip"
        android:text="Dismiss"
        android:textColor="@color/white" />

    <LinearLayout
        android:id="@+id/layc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:clickable="false"
        android:focusableInTouchMode="false"
        android:scaleType="fitCenter" >
    </LinearLayout>
</RelativeLayout>

</LinearLayout>

自定义视图的代码     包com.exercise.AndroidAlarmService;

import android.content.Context;
import android.graphics.Canvas;
 import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Vibrator;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class DrawView extends View {
static AlarmClock a;
static int makemove = 0;
int snooze_co[];
int dismiss_co[];
Vibrator v;

public DrawView(Context context) {
    super(context);
    setFocusable(true); // necessary for getting the touch events

    // setting the start point for the balls
    Point point1 = new Point();
    point1.x = 150;
    point1.y = 20;

    snooze_co = new int[2];
    dismiss_co = new int[2];
    snooze_co[0] = (int) (AlarmDialogActivity.screenWidth * 0.10);
    snooze_co[1] = (int) (AlarmDialogActivity.screenHeight * 0.70 * 0.5);
    dismiss_co[0] = (int) (AlarmDialogActivity.screenWidth * 0.70);
    dismiss_co[1] = (int) (AlarmDialogActivity.screenHeight * 0.70 * 0.5);
    v = (Vibrator) AlarmDialogActivity.who
            .getSystemService(Context.VIBRATOR_SERVICE);
    long[] pattern = { 0, 200, 500 };
    v.vibrate(pattern, 0);

    // declare each ball with the ColorBall class
    Drawable dr = context.getResources().getDrawable(R.drawable.clock);
    dr.setBounds(100, 50, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());
    a = new AlarmClock(context, R.drawable.clock , point1);

    Log.w("where", "Positions: " + snooze_co[0] + " " + snooze_co[1] + " "
            + dismiss_co[0] + " " + dismiss_co[1]);

}


// the method that draws the balls
@Override
protected void onDraw(Canvas canvas) {
    // canvas.drawColor(0xFFCCCCCC); //if you want another background color

    // draw the balls on the canvas
    canvas.drawBitmap(a.getBitmap(), a.getX(), a.getY(), null);

}


// events when touching the screen
public boolean onTouchEvent(MotionEvent event) {
    int eventaction = event.getAction();

    int X = (int) event.getX();
    int Y = (int) event.getY();

    switch (eventaction) {

    case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on
                                    // a ball
        Log.w("where", "makemove" + makemove);
        int centerX = a.getX() + 50;
        int centerY = a.getY() + 50;

        // calculate the radius from the touch to the center of the ball
        double radCircle = Math
                .sqrt((double) (((centerX - X) * (centerX - X)) + (centerY - Y)
                        * (centerY - Y)));
        if (radCircle < 45) {
            makemove = 1;
            break;
        }

        break;

    case MotionEvent.ACTION_MOVE: // touch drag with the ball
        // move the balls the same as the finger
        Log.w("where",
                "makemove" + makemove + " " + a.getX() + " " + a.getY());
        if (makemove == 1) {
            a.setX(X - 50);
            a.setY(Y - 50);
            int cenX = a.getX();
            int cenY = a.getY();

            double rad1 = Math
                    .sqrt((double) (((cenX - dismiss_co[0]) * (cenX - dismiss_co[0])) + (cenY - dismiss_co[1])
                            * (cenY - dismiss_co[1])));
            double rad2 = Math
                    .sqrt((double) (((cenX - snooze_co[0]) * (cenX - snooze_co[0])) + (cenY - snooze_co[1])
                            * (cenY - snooze_co[1])));

            // if the radius is smaller then 23 (radius of a ball is 22),
            // then
            // it must be on the ball
            if (rad1 < 40) {
                Log.w("where", "Dismiss");
                v.cancel();

            }
            if (rad2 < 40) {
                Log.w("where", "Snooze");
            }

        }

        break;

    case MotionEvent.ACTION_UP:
        makemove = 0;
        Log.w("where", "makemove" + makemove);
        break;
    }
    // redraw the canvas
    invalidate();
    return true;

}
}

我想做什么 - 我有一个ID为'layc'的线性布局。我正在创建一个新的View,我将使用addView添加到此LinearLayout。此custon视图是可拖动的。当我将其拖动到关闭时,将调用一个函数。

问题 - 添加新视图后,暂停和关闭按钮未收到onTouch / onClick事件

0 个答案:

没有答案