点击事件,类似于 JavaScript 的事件

huangapple 未分类评论43阅读模式
英文:

OnClick JS like event

问题

我刚开始使用Android Studio,不知道为什么这段代码不起作用。

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

I'm new to Android Studio, and I don't know why is this code not working.

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

答案1

得分: 0

你必须向布局中添加点击功能。你可以尝试以下代码。

<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>
英文:

You must add click feature to the layouts. You can try this.

&lt;GridLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:clickable=&quot;true&quot;
    tools:context=&quot;.MainActivity&quot;&gt;
&lt;/GridLayout&gt;

huangapple
  • 本文由 发表于 2020年4月3日 22:10:52
  • 转载请务必保留本文链接:https://java.coder-hub.com/61013774.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定