【问题标题】:Accessing content in a TableLayout访问 TableLayout 中的内容
【发布时间】:2011-04-25 08:40:02
【问题描述】:

我正在用数据(字符串)填充 TableLayout(即行和列)。

当我单击一个单元格时,我希望该单元格中存储的数据显示在我的控制台中。

我该怎么做?除了查看ID还有其他方法吗?

【问题讨论】:

  • 这个问题到底是关于什么的?苹果手机?航天飞机?
  • 对不起,是关于Android开发的

标签: android tablelayout


【解决方案1】:

http://developer.android.com/resources/tutorials/views/hello-tablelayout.html 中所述,android 中没有 Column 或 TD(表数据)或 Cell 等效项。除非另有说明,否则每个元素都被视为一个单独的单元格。

有这个在我的,考虑到你没有指定你在你的行中使用什么样的视图,我猜它是一个按钮,你当然可以用这样的东西点击它:

Button b = (Button)findViewById(R.id.oneOfMyButtons); // You could create this "on the go"
b.setOnClickListener(new View.OnClickListener{
    public void onClick(View v){
        System.out.println(v.getText());
    }
}

希望这会有所帮助。

【讨论】:

  • 虽然我没有使用按钮,但 TextViews,感谢您的帮助,我想出了如何从单元格中获取数据。非常感谢!
【解决方案2】:

TableLayout 扩展了 LinearLayout,它没有 OnItemClickListener 方法。您将需要在子 Views 中实现 OnClickListener

你可以做的是使用GridView,它实现了AdapterView,因此你可以使用OnItemClickListener

http://developer.android.com/resources/tutorials/views/hello-gridview.html

http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener%28android.widget.AdapterView.OnItemClickListener%29

abstract void onItemClick(AdapterView<?> parent, View view, int position, long id)

单击此 AdapterView 中的项目时调用的回调方法。

【讨论】:

  • 非常感谢您的回答,但我尝试使用 GridView,但这并不是我的应用所需要的。
猜你喜欢
  • 2019-08-14
  • 1970-01-01
  • 2012-08-28
  • 1970-01-01
  • 2013-06-25
  • 2021-08-15
  • 2012-05-28
  • 1970-01-01
  • 2016-05-03
相关资源
最近更新 更多