在多个 Textview 上触摸侦听器? [英] Touch listener on multiple Textviews?

查看:89
本文介绍了在多个 Textview 上触摸侦听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 ... 或者有一种方法可以将它们全部分组?

Hi ! I would like to the same Motion Event (same actions) on 5 other Textview's, so i have to create 5 others public class CustomTouchListener1,2,3,4,5 ... or there's a way to group them all ?

推荐答案

您不需要制作自定义触摸列表器.

You don't need to make Custom Touch Listner.

可以通过以下代码实现

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;
        }
    };

希望对你有用.

这篇关于在多个 Textview 上触摸侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆