想在 editText.setOnTouchListener 中只做一次而不是每次

want to do some task in editText.setOnTouchListener only once and not each time

我想做一些事情,比如当用户点击 editText 然后它可以被编辑(默认情况下它在 xml 中被禁用)并且 toast(和更多按钮)应该只出现一次,不是每次。

我为此创建了以下逻辑:-

edt_title.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (MotionEvent.ACTION_UP == event.getAction()) {

Crouton.showText(ShowActivity.this, "Editing Mode Enabled",Style.INFO);

welcome.animate(getApplicationContext(), gallery, R.anim.fab_jump_from_down,  300);
                edt_title.setFocusableInTouchMode(true);
            }
            return false;}
    });

问题是每次用户点击 editText 时都会调用它, 单击 editText 时,有什么方法可以只执行一次。 可能可以使用 sharedPreference 但如果我可以不使用 sharedPreference 就更好了。

提前致谢:) 尼舒

一个简单的解决方案是:

edt_title.setOnTouchListener(new View.OnTouchListener() {
    boolean called = false;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (!called && MotionEvent.ACTION_UP == event.getAction()) {
            Crouton.showText(ShowActivity.this, "Editing Mode Enabled",Style.INFO);
            welcome.animate(getApplicationContext(), gallery, R.anim.fab_jump_from_down,  300);
            edt_title.setFocusableInTouchMode(true);

            called = true;
        }
        return false;
    }
});

声明一个你像标志一样使用的全局变量并使用一次

boolean flagOneTime = true;

并将其添加到您的侦听器中

if( flagOneTime )
{
   // do something and disable flag
   flagOneTime = false;
}