如何进出来看触摸时改变TextView的背景颜色?颜色、背景、进出、TextView

2023-09-06 15:29:34 作者:懵鸟

这已经是一个很简单的解决,但我似乎无法弄清楚。我有我的计划,以改变背景颜色改变的onClick 和onTouch与 ACTION_DOWN ACTION_UP 。但我需要它来改变颜色触摸屏幕和进出的的TextView 时。我需要它像一个鼠标悬停/ mouseOut事件起作用。有没有办法做到这一点在Android?还是我坚持使用 onTouch ,那里的行动必须从在的TextView 开始?

This has to be a very simple fix, but I can't seem to figure it out. I have got my program to change the background color to change onClick, and onTouch with ACTION_DOWN and ACTION_UP. But I need it to change the color when touching the screen and going in and out of the TextView. I need it to function like a mouseOver/mouseOut event. Is there a way to do this on android? Or am I stuck with onTouch, where the action has to start from within the TextView?

现在我设置了 onTouchListener 的TextView 本身。我应该把它在其他地方,然后检查x和y是的TextView 之内?或者是有,我应该使用另一个事件监听器?我是新来的Andr​​oid,任何帮助将是AP preciated。谢谢你。

Right now I set the onTouchListener on the TextView itself. Should I set it somewhere else and then check if the x and y are within the Textview? Or is there another event listener I should be using? I am new to Android, any help would be appreciated. Thanks.

迈克

推荐答案

我就贯彻落实 OnTouchListener 您的应用程序,然后在 onTouch 方法,继续检查如果触摸事件的当前位置的的边框的视图的范围内。如果是,则应用新的背景,如果它是不适用的原始

I would implement the OnTouchListener on your application and then on the onTouch method, keep checking if the current position of the touch event is within the bounds of the bounding box of the view. If it is, apply the new background and if it isn't apply the original one.

由于所有的意见落实 setBackgroundColor 我没有做任何铸造的TextView ,但这个例子就足够了,至少作为一个起点,进一步开发应用程序。

Since all the views implement the setBackgroundColor I didn't do any casting to TextView but the example should suffice, at least as a starting point to develop your application further.

满code这是以下内容:

The full code for this is the following:

public class Main extends Activity implements OnTouchListener{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Set the listener for the parent/container view
        findViewById(R.id.cont).setOnTouchListener(this);

        //Get a hold of the view and create the Rect for the bounds
        View target = findViewById(R.id.target);
        Rect b = new Rect();

        //Get the bounding box of the target view into `b`
        target.getGlobalVisibleRect(b);
    }

    public boolean onTouch(View v, MotionEvent event) {

        //Check if it's within the bounds or not
        if(b.contains((int)event.getRawX(),(int) event.getRawY())){
            target.setBackgroundColor(0xffff0000);
        }else{
            target.setBackgroundColor(0xff000000);
        }

        //You need to return true to keep on checking the event
        return true;
    }
}

作为用户接口,为previous code,它只是一个ID 线性布局的图(的TextView 你的情况)与ID 目标。剩下的就是完全由默认值,以便在我里面有没有点这里粘贴。 注意我只测试了这个模拟器上和因人而异的时,它试图在真实设备上,但据我能想到的,它应该是罚款。相关文章:

As for the user interface for the previous code, it's just a linear layout with an ID cont and a view (a TextView in your case) with an ID target. The rest is totally by default so there is no point in me pasting it here. Note I only tested this on an emulator and ymmv when trying it on real devices, but as far as I can think of, it should be fine. Relevant documentation:

getGlobalVisibleRect方法 onTouchListener getGlobalVisibleRect method onTouchListener