通过v.getId()在ONTOUCH MOTIONEVENT.ACTION_MOVE中使用getTag()

时间:2013-06-04 12:57:48

标签: android ontouchlistener

1)我在垂直线性布局中编码了6个水平线性布局。

2)在每个水平线性布局中,有6个文本视图。以编程方式创建,没有xml静态引用。

3)因此,textview采用6乘6矩阵的形状。

4)在每个textView上调用setOnTouchListener。

5)每个textview都有由tv.setID();

指定的唯一ID

6)每个textView包含由tv.setTag();

指定的整数值

这是我的代码:

lilayParent=(LinearLayout)findViewById(R.id.lilayParent);

        lilayParent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        lilayParent.setOrientation(LinearLayout.VERTICAL);
        lilayParent.setOnTouchListener(this);

        LinearLayout lilayChild1=new LinearLayout(this);
        lilayChild1.setOrientation(LinearLayout.HORIZONTAL);
        lilayChild1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        lilayChild1.setGravity(Gravity.CENTER);

        LinearLayout lilayChild2=new LinearLayout(this);
        lilayChild2.setOrientation(LinearLayout.HORIZONTAL);
        lilayChild2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        lilayChild2.setGravity(Gravity.CENTER);

        LinearLayout lilayChild3=new LinearLayout(this);
        lilayChild3.setOrientation(LinearLayout.HORIZONTAL);
        lilayChild3.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        lilayChild3.setGravity(Gravity.CENTER);

        LinearLayout lilayChild4=new LinearLayout(this);
        lilayChild4.setOrientation(LinearLayout.HORIZONTAL);
        lilayChild4.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        lilayChild4.setGravity(Gravity.CENTER);

        LinearLayout lilayChild5=new LinearLayout(this);
        lilayChild5.setOrientation(LinearLayout.HORIZONTAL);
        lilayChild5.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        lilayChild5.setGravity(Gravity.CENTER);

        LinearLayout lilayChild6=new LinearLayout(this);
        lilayChild6.setOrientation(LinearLayout.HORIZONTAL);
        lilayChild6.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        lilayChild6.setGravity(Gravity.CENTER);




        for (int r=0; r<ROW_SIZE; r++)
        {

            for (int c=0; c<COL_SIZE; c++)
            {
                cell[r][c] = CellCollection.getCell(r,c);
                TextView tv=new TextView(this);
                //                  tv.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                tv.setLayoutParams(new LayoutParams(40,40));
                String id=String.valueOf(r)+String.valueOf(c);
                tv.setId(Integer.parseInt(id));
                Log.i("ID is",id);

                tv.setText(String.valueOf(cell[r][c].getValue()));
                tv.setTag(String.valueOf(cell[r][c].getValue()));
                int color=cell[r][c].getColor();

                if(color==0) color=R.drawable.btn_grey;
                if(color==1) color=R.drawable.btn1;
                if(color==2) color=R.drawable.btn2;
                if(color==3) color=R.drawable.btn3;
                if(color==4) color=R.drawable.btn4;
                if(color==5) color=R.drawable.btn5;
                if(color==6) color=R.drawable.btn6;
                if(color==7) color=R.drawable.btn7;
                if(color==8) color=R.drawable.btn8;

                tv.setGravity(Gravity.CENTER);
                tv.setBackgroundResource(color);
                if(r==0)lilayChild1.addView(tv);
                if(r==1)lilayChild2.addView(tv);
                if(r==2)lilayChild3.addView(tv);
                if(r==3)lilayChild4.addView(tv);
                if(r==4)lilayChild5.addView(tv);
                if(r==5)lilayChild6.addView(tv);

                tv.setOnTouchListener(this);
            }

            if(r==0)lilayParent.addView(lilayChild1);
            if(r==1)lilayParent.addView(lilayChild2);
            if(r==2)lilayParent.addView(lilayChild3);
            if(r==3)lilayParent.addView(lilayChild4);
            if(r==4)lilayParent.addView(lilayChild5);
            if(r==5)lilayParent.addView(lilayChild6);

        }

现在我被卡住的部分属于ONTOUCHLISTENER()

// inside MOTIONEVENT.ACTION_DOWN:

case MotionEvent.ACTION_DOWN:

int valueis=v.getId();

if(v.getId()>=0&&v.getId()<=55)
                    {
                        tag=Integer.parseInt((String) v.getTag()) ;
                        //      int tag=Integer.parseInt((String) v.getTag()) ;

                        Log.i("value and tag is",String.valueOf(valueis)+String.valueOf(tag));

//alFiftyFive is an ArrayList<Integer>
                        alFiftyFive.add(tag);
                        sumTotal=tag;
                        Toast.makeText(getApplicationContext(), String.valueOf(tag), 100).show();
                        tvSum.setText(String.valueOf(tag));

            return true;
                    }

case MotionEvent.ACTION_MOVE:
        Log.d("MOVE","MOVE");

            if(v.getId()>=00&&v.getId()<=55)
                    {
                        tag=Integer.parseInt((String) v.getTag()) ;
                        Toast.makeText(getApplicationContext(), String.valueOf(tag), 100).show();
                        alFiftyFive.add(tag);
                        Log.i("value and tag is",String.valueOf(valueis)+String.valueOf(tag));
                        Log.i("alFiftyFive",alFiftyFive.toString());
                        return true;
                    }



case MotionEvent.ACTION_UP:
        Log.d("UP","UP");
        for(int i=1;i<alFiftyFive.size();i++)
        {
            sumTotal= sumTotal +alFiftyFive.get(i);
        }
        tvSum.setText(String.valueOf(sumTotal));
    }

Expecetd行为:我想在触发ontouch移动事件时触发各种连续文本视图,并通过getTag跟踪它们的值,并将其添加到ArrayList中,这是目前没有发生的。

0 个答案:

没有答案
相关问题