触摸多个Textviews上的监听器?

时间:2017-03-21 09:42:08

标签: android textview ontouchlistener

private TextView tv2;

tv2=(TextView)findViewById(R.id.textView2);
tv2.setOnTouchListener(new CustomTouchListener());

public class CustomTouchListener implements View.OnTouchListener {
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch(motionEvent.getAction()){

                case MotionEvent.ACTION_DOWN:
                    Hauteur.setTextSize(TypedValue.COMPLEX_UNIT_PX, 80);
                    Hauteur.startAnimation(AnimationUtils.loadAnimation(recuperationJson.this, android.R.anim.slide_in_left));
                    break;

                case MotionEvent.ACTION_UP:
                    // Action you you want on finger up
                    Hauteur.setTextSize(TypedValue.COMPLEX_UNIT_PX, 60);

                    break;
            }
            return true;
        }
    }
你好!我想在其他5个Textview上进行相同的动作事件(相同的动作),所以我必须创建另外5个公共类CustomTouchListener1,2,3,4,5 ......或者那里有一个方法将它们分组?

6 个答案:

答案 0 :(得分:0)

您可以使用RecycleView或ListView而不是TextView。

答案 1 :(得分:0)

而是创建Inner类,创建匿名类并直接在View上设置。

private final View.OnTouchListener myTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }
}

设置TextView

tv2.setOnTouchListener(myTouchListener)

答案 2 :(得分:0)

试试这个!创建一个匿名类

private final View.OnTouchListener myTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }
} 

现在将此触摸事件设置为所需的文本视图

private TextView tv1,tv2.tv3,tv4,tv5;
tv1=(TextView)findViewById(R.id.textView1);
tv2=(TextView)findViewById(R.id.textView2);
tv3=(TextView)findViewById(R.id.textView3);
tv4=(TextView)findViewById(R.id.textView4);
tv5=(TextView)findViewById(R.id.textView5);

现在设置触摸事件

tv1.setOnTouchListener(myTouchListener);
tv2.setOnTouchListener(myTouchListener);
tv3.setOnTouchListener(myTouchListener);
tv4.setOnTouchListener(myTouchListener);
tv5.setOnTouchListener(myTouchListener);

答案 3 :(得分:0)

将您的TextViews添加到列表中并将其传递给setListnerOnTextViews

private function setListnerOnTextViews(List<TextView> tvs){
   for(int i =0; i<tvs.size;i++){
     if(tvs.get(0) instance of TextView){
       tvs.get(0).setOnTouchListener(new CustomTouchListener());
     }
   }

}

答案 4 :(得分:0)

对于几个TextView使用相同的OnTouchListener,您不需要创建另一个类

        private TextView tv2, tv3, tv4, tv5;
        tv2 = (TextView) findViewById(R.id.textView2);
        tv3 = (TextView) findViewById(R.id.textView3);
        tv4 = (TextView) findViewById(R.id.textView4);
        tv5 = (TextView) findViewById(R.id.textView5);

        View.OnTouchListener listener = new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return false;
            }
        };
        tv2.setOnTouchListener(listener);
        tv3.setOnTouchListener(listener);
        tv4.setOnTouchListener(listener);
        tv5.setOnTouchListener(listener);

答案 5 :(得分:0)

您不需要制作自定义触控列表器。

您可以通过以下代码

来实现
private TextView tv2,tv1;

    tv1=(TextView)findViewById(R.id.textView1);
    tv1.setOnTouchListener(onTouchListener);

    tv2=(TextView)findViewById(R.id.textView2);
    tv2.setOnTouchListener(onTouchListener);



    View.OnTouchListener onTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {




            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    Hauteur.setTextSize(TypedValue.COMPLEX_UNIT_PX, 80);
                    Hauteur.startAnimation(AnimationUtils.loadAnimation(recuperationJson.this, android.R.anim.slide_in_left));

                    //Here if you want to know from wich touch this event has accured you can do following code
                    switch (v.getId()){
                        case R.id.textView1:
                            break;
                        case R.id.textView2:
                            break;
                        case R.id.textView3:
                            break;
                    }

                    break;
                case MotionEvent.ACTION_UP:
                    // Action you you want on finger up
                    Hauteur.setTextSize(TypedValue.COMPLEX_UNIT_PX, 60);

                    switch (v.getId()){
                        case R.id.textView1:
                            break;
                        case R.id.textView2:
                            break;
                        case R.id.textView3:
                            break;
                    }


                    break;
            }

            return true;
        }
    };

希望为你工作。