滑动手指以选择多个按钮并从按钮获取文本

时间:2014-02-26 07:01:10

标签: android draggable slide multi-touch

我有多个Buttons而不是imageView,当我逐一启动slide on the button时,选中的按钮color should change也需要将buttons text转换为另一个文本视图。

我找到了其他一些答案,但它对我的要求没有用。

  public class Nine extends Activity implements OnClickListener {

protected Map<View, Rect> cells = new HashMap<View, Rect>();

protected boolean hasCoordinatesPopulated;
TextView t1, t2, t3, t4, t5, t6, t7, t8, t, tv;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nine);
    tv = (TextView) findViewById(R.id.textView10);

    // TextView t9=(TextView)findViewById(R.id.textView10);

    final RelativeLayout table = (RelativeLayout) findViewById(R.id.table);
    table.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (!hasCoordinatesPopulated) {

                        View view = table.findViewById(R.id.textView1);
                        Rect rect = getRawCoordinatesRect(view);
                        cells.put(view, rect);

                        view = table.findViewById(R.id.textView2);
                        rect = getRawCoordinatesRect(view);
                        cells.put(view, rect);

                        view = table.findViewById(R.id.textView3);
                        rect = getRawCoordinatesRect(view);
                        cells.put(view, rect);

                        view = table.findViewById(R.id.textView4);
                        rect = getRawCoordinatesRect(view);
                        cells.put(view, rect);

                        view = table.findViewById(R.id.textView5);
                        rect = getRawCoordinatesRect(view);
                        cells.put(view, rect);

                        view = table.findViewById(R.id.textView6);
                        rect = getRawCoordinatesRect(view);
                        cells.put(view, rect);

                        view = table.findViewById(R.id.textView7);
                        rect = getRawCoordinatesRect(view);
                        cells.put(view, rect);

                        view = table.findViewById(R.id.textView8);
                        rect = getRawCoordinatesRect(view);
                        cells.put(view, rect);

                        view = table.findViewById(R.id.textView9);
                        rect = getRawCoordinatesRect(view);
                        cells.put(view, rect);

                        hasCoordinatesPopulated = true;
                    }
                }

                private Rect getRawCoordinatesRect(final View view) {
                    int[] coords = new int[2];
                    view.getLocationOnScreen(coords);
                    Rect rect = new Rect();
                    rect.left = coords[0];
                    rect.top = coords[1];
                    rect.right = rect.left + view.getWidth();
                    rect.bottom = rect.top + view.getHeight();
                    return rect;
                }
            });
    table.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            View view = null;
            if (event.getAction() == MotionEvent.ACTION_DOWN
                    || event.getAction() == MotionEvent.ACTION_MOVE) {

                int x = (int) event.getRawX();
                int y = (int) event.getRawY();
                for (final java.util.Map.Entry<View, Rect> entry : cells
                        .entrySet()) {
                    view = entry.getKey();
                    final Rect rect = entry.getValue();
                    if (rect.contains(x, y)) {
                        view.setBackgroundColor(Color.CYAN);

                    } else {
                        // view.setBackgroundColor(Color.BLUE);
                        view.setBackgroundColor(Color.WHITE);
                        tv.setText("");
                    }

                }

                return true;
            }

            return false;
        }

    });
}

}

0 个答案:

没有答案