首页 > 解决方案 > OnClick JS 类似事件

问题描述

我是 Android Studio 的新手,我不知道为什么这段代码不起作用。

GridLayout divNum1 = findViewById(R.id.DivNum1);
divNum1.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    showItemPage(v);
  }
});

标签: javaandroid

解决方案


您必须将点击功能添加到布局中。你可以试试这个。

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    tools:context=".MainActivity">
</GridLayout>

推荐阅读