java - 如何处理android中的长按

标签 java android

我想通过长按使布局可见,并通过再次单击使布局不可见,这对我不起作用。 请在这件事上给予我帮助。 代码如下。 当我单击一次时,布局可见。当我再次单击时,布局需要消失,但仍然可见。

     public class ReminderstestExplaination extends Fragment {

    RecyclerView testexplntion_recycle;
    CustomRecyclerview customRecyclerview;
    LinearLayout reminderpage_addbutton,extraremlay,del_editlay;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.remindertest_explaination,container,false);

        extraremlay = v.findViewById (R.id.extraremlay);

        extraremlay.setOnLongClickListener (new View.OnLongClickListener () {
                                                @Override
                                                public boolean onLongClick(View view) {

                                                    del_editlay.setVisibility (View.VISIBLE);

                                                    return true;

                                                }
                                            });
}

最佳答案

执行此操作,使用 boolean 变量跟踪可见性,并在每次长按完成时更改它。

public class ReminderstestExplaination extends Fragment {
boolean isVisible = false;
RecyclerView testexplntion_recycle;
CustomRecyclerview customRecyclerview;
LinearLayout reminderpage_addbutton,extraremlay,del_editlay;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.remindertest_explaination,container,false);

    extraremlay = v.findViewById (R.id.extraremlay);

    extraremlay.setOnLongClickListener (new View.OnLongClickListener () {
                                            @Override
                                            public boolean onLongClick(View view) {

                                               if(isVisible) {
                                                isVisible = false;
                                               del_editlay.setVisibility 
                                              (View.INVISIBLE);
                                              }
                                               }
                                               else {
                                             isVisible = true;
                                             del_editlay.setVisibility 
                                              (View.VISIBLE);}

                                                return true;

                                            }
                                        });

}

关于java - 如何处理android中的长按,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56353951/

相关文章:

java - 使用 Java 邮件 API 连接 Hotmail 帐户时 session 超时

java - 为什么我在此 Java 代码中收到 "dead code"警告?

java - 是否可以使用lombok向静态工厂方法添加注释

android - 微调器文字大小不改变?

android - 将不同大小的字符串/文本应用于 TextView

android - 如何从 Preference Activity 中为 Listpreference 编写点击事件?

java - Sonarqube 6.7x 的安全插件

java - 带有 LIKE 条件的 Spring Data JPA 查询在运行时抛出异常

android - 有人知道我可以如何运行android示例应用程序吗?

Android,半滚动然后 float