是否可以为 Android TableLayout 设置 Focus 或设置 OnClick?

Is it possible to set Focus or set OnClick for an Android TableLayout?

问题: 我一直在尝试在 TableLayout 上设置 setOnClickListenersetOnFocusChangeListener 但我没有收到任何响应没有我可以识别的调试错误或问题。

我正在尝试做的事情: 当有人点击 tablelayout 添加作者时,单个作者的 EditText 会填充一个如果表格布局有多个使用 getChildCount().

的子项,则标准消息(可能会禁用它)

我的尝试: 正如我提到的,我已经尝试设置 setOnClickListenersetOnFocusChangeListener 事件,但似乎没有任何反应。我尝试设置 Toast 消息以查看它是否被激活,但我什么也没收到。我不确定我可能做错了什么。我已经为许多对象设置了 OnClick 和 Focus,但这是第一个表格布局,我遇到了麻烦。

tableLayoutAuthors.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus){
            Toast.makeText(AddNote.this,tableLayoutAuthors.getChildCount(), Toast.LENGTH_LONG);
            if(tableLayoutAuthors.getChildCount()>1)
                author.setText(R.string.add_author_phrase);
            else if(tableLayoutAuthors.getChildCount()==1)
                populateSourceDetails(DBQueryTools.getSourcesByTitle(sourceTitle.getText().toString()));
        }
    }
});

只需将这些 XML 属性添加到您的 TableLayout 因为它们默认设置为 false

android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"

我找到了我的解决方案...

一旦我选择了 addOnLayoutChangeListener,table 就会像我希望的那样响应。这可能不是我的最终设计响应,但它适用于以下内容:

View tblLayAut = tableLayoutAuthors;
tblLayAut.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        if(tableLayoutAuthors.getChildCount()>1)
            author.setText(R.string.add_author_phrase);
        else if(tableLayoutAuthors.getChildCount()==1)
            populateSourceDetails(DBQueryTools.getSourcesByTitle(sourceTitle.getText().toString()));
    }
});

}