简体   繁体   English

Android TableLayout单元格内容单击侦听器

[英]Android tablelayout click listener for cell content

I have a 3x3 gridview with custom linearlayouts within. 我有一个3x3网格视图,其中包含自定义的线性布局。 But I want something like this layout and as I search on the internet it's not possible with gridview because of the column span. 但是我想要这样的布局 ,当我在互联网上搜索时,由于列跨度,使用gridview是不可能的。 I used gridview because of the onClickListener method: when the user click on one of the grid cell, a new activity starts(not the same activity, so it's like a main menu). 我之所以使用gridview是因为onClickListener方法:当用户单击某个网格单元格时,将启动一个新活动(不是同一活动,因此它就像一个主菜单)。 Is it possible to do in a TableLayout? 可以在TableLayout中进行吗? So if I click on a cell even if it spanned can I call an onClick method? 因此,即使我单击了一个单元格,即使它跨越了单元格,也可以调用onClick方法吗? I've searched the web for some solutions, but all of what I've found is clicking on a tablerow(which is not good for me if there is 3 custom layouts in one row). 我已经在网上搜索了一些解决方案,但是我发现的全部是单击一个表格行(如果一行中有3个自定义布局,这对我来说是不好的)。

My layout for the TableLayout: 我的TableLayout布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/background_light"
android:orientation="vertical" >

<ImageView
    android:id="@+id/headerPicture"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@string/imageString"
    android:src="@drawable/bicaj" />

<TableLayout
    android:id="@+id/mainGrid"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:numColumns="3" >

    <TableRow
        android:id="@+id/mainFirstRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dip" >

        <LinearLayout
            android:id="@+id/mainOnline"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <ImageView
                android:id="@+id/mainIconOnline"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/main_page_icon_descp" />

            <TextView
                android:id="@+id/mainTextOnline"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="12sp" />
        </LinearLayout>
    </TableRow>
.
.
.
.
    </TableLayout>
</LinearLayout>

You can add clickListeners to the ImageView 您可以将clickListeners添加到ImageView

.....
    <LinearLayout
                android:id="@+id/mainOnline"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
                <ImageView
                    android:id="@+id/mainIconOnline"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:contentDescription="@string/main_page_icon_descp" />
......

And in your Activity 在你的活动中

ImageView butt1 = (ImageView) findViewById(R.id.mainIconOnline);

        butt1.setOnClickListener(this);

@Override
    public void onClick(View v) {
        if (v.getId() == R.id.mainIconOnline) {

            Intent i = new Intent(this, SecondActivityclass);
        startActivity(i);

        }

    }

This should work 这应该工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM