java Android 上的可点击 TableRow

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13184484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:48:32  来源:igfitidea点击:

Clickable TableRow on Android

javaandroidtablerow

提问by user1505181

I have a simple program i have made in java for android but am having difficulty in making the tablerow clickable. When the user clicks a row i wish for a certain picture to be displayed in the imageview below. Here is my xml code:

我有一个简单的程序,我在 java 中为 android 制作了一个简单的程序,但是很难使 tablerow 可点击。当用户单击一行时,我希望某张图片显示在下面的图像视图中。这是我的 xml 代码:

     <?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FF909090"
    android:stretchColumns="1" >

    <TableRow>

        <TextView
            android:layout_margin="2dip"
            android:layout_marginRight="2dip"
            android:background="#0000ff"
            android:padding="3dip"
            android:text="I" />

        <TextView
            android:layout_margin="2dip"
            android:layout_marginLeft="1dip"
            android:background="#0000ff"
            android:gravity="center"
            android:padding="3dip"
            android:text="A"
            android:onClick="Achord" />
    </TableRow>

    <TableRow>

        <TextView
            android:layout_column="0"
            android:layout_margin="2dip"
            android:background="#0000ff"
            android:padding="3dip"
            android:text="II" />

        <TextView
            android:layout_margin="2dip"
            android:background="#0000ff"
            android:gravity="center"
            android:padding="3dip"
            android:text="Bm" />
    </TableRow>

    <TableRow>

        <TextView
            android:layout_column="0"
            android:layout_margin="2dip"
            android:background="#0000ff"
            android:padding="3dip"
            android:text="III" />

        <TextView
            android:layout_margin="2dip"
            android:background="#0000ff"
            android:gravity="center"
            android:padding="3dip"
            android:text="C#m" />
    </TableRow>

    <View
        android:layout_height="2dip"
        android:background="#FF909090" />

    <TableRow android:background="#0000ff" >
    </TableRow>

    <TableRow>

        <TextView
            android:layout_margin="2dip"
            android:background="#0000ff"
            android:padding="3dip"
            android:text="IV" />

        <TextView
            android:layout_margin="2dip"
            android:background="#0000ff"
            android:gravity="center"
            android:padding="3dip"
            android:text="D" />
    </TableRow>

    <View
        android:layout_height="2dip"
        android:background="#FF909090" />

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableRow>

    <TableRow>

        <TextView
            android:layout_margin="2dip"
            android:layout_marginRight="2dip"
            android:background="#0000ff"
            android:padding="3dip"
            android:text="V" />

        <TextView
            android:layout_margin="2dip"
            android:layout_marginLeft="1dip"
            android:background="#0000ff"
            android:gravity="center"
            android:padding="3dip"
            android:text="E" />
    </TableRow>

    <TableRow>

        <TextView
            android:layout_margin="2dip"
            android:layout_marginRight="2dip"
            android:background="#0000ff"
            android:padding="3dip"
            android:text="VI" />

        <TextView
            android:layout_margin="2dip"
            android:layout_marginLeft="1dip"
            android:background="#0000ff"
            android:gravity="center"
            android:padding="3dip"
            android:text="F#m" />
    </TableRow>

    <TableRow>

        <TextView
            android:layout_margin="2dip"
            android:layout_marginRight="2dip"
            android:background="#0000ff"
            android:padding="3dip"
            android:text="VII" />

        <TextView
            android:layout_margin="2dip"
            android:layout_marginLeft="1dip"
            android:background="#0000ff"
            android:gravity="center"
            android:padding="3dip"
            android:text="G#dim" />
    </TableRow>




    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.05"
        android:background="#000000"
        android:maxLines="10"
        android:text="The relative minor of the Major key of A is F#m. The 12 bar blues progression in A is: \n A | A | A | A | D | D | A | A | E | D | A | E" />


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.03"
        android:src="@drawable/android_focused" />

and here is my java code:

这是我的java代码:

package com.coreservlets.widgets;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TableRow;
import com.welly.keychords.R;





 public class keya extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.keya);
}
}

what i want to happen is that imageview1 will display a different image depending on what row is clicked. Any help would be greatly appreciated

我想要发生的是 imageview1 将根据单击的行显示不同的图像。任何帮助将不胜感激

As sam suggested i have ammended my code to this:

正如山姆所建议的那样,我已将代码修改为:

public class keya extends Activity {

    ImageView imagev = (ImageView) findViewById(R.id.imageView1);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.keya);
    }

    public void rowClick(View view) {
        switch(view.getId()) {
        case R.id.one:
            // Load image from Drawable folder
            imagev.setImageResource(R.drawable.android_normal); // example file for now
            break;
        }
    }
   }

but now i have a null pointer exception on running that class. all compiles fine though

但现在我在运行该类时出现空指针异常。虽然所有编译都很好

回答by Sam

Simply give each TableRow element a unique idand define an onClickmethod:

简单地给每个 TableRow 元素一个唯一的id并定义一个onClick方法:

<TableRow
    android:id="@+id/one"
    android:onClick="rowClick">

Add a class variable imageViewthat references the ImageView in your layout. Then load the image:

imageView在布局中添加一个引用 ImageView的类变量。然后加载图像:

public void rowClick(View view) {
    switch(view.getId()) {
    case R.id.one:
        // Load image from Drawable folder
        imageView.setImageResource(R.id.imageOne);
        break;
    }
}

Understand that any elements inside a TableRow with their own onClickmethod will call its own method. For instance the TextView with android:onClick="Achord"will call Achord()not rowClick().

了解 TableRow 内的任何具有自己onClick方法的元素都将调用自己的方法。例如 TextView withandroid:onClick="Achord"将调用Achord()not rowClick()



Addition
You need to call findViewById()afterdeclaring your layout with setContentView()in onCreate().

添加
您需要findViewById()使用setContentView()in声明布局调用onCreate()

ImageView imagev;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.keya);
    imagev = (ImageView) findViewById(R.id.imageView1);
}

回答by Ralgha

Give each row an ID, then you can assign a listener to each one in your code:

给每一行一个 ID,然后你可以为你的代码中的每一行分配一个监听器:

TableRow row = (TableRow)findViewById( R.id.row1 );
row.setOnClickListener( new OnClickListener() {
    @Override
    public void onClick( View v ) {
        //Do Stuff
    }
} );

回答by David C. Sainte-Claire

You need to call setOnClickListener() for each of your TableRow objects so that framework knows what to do when a row is clicked. In addition your Activity should implement View.OnClickListener() so that you can control what happens when a row gets clicked. Right now you aren't doing anything when the View gets clicked; at least according to your code you aren't registering a callback on any of the Views' onClickListeners

您需要为每个 TableRow 对象调用 setOnClickListener() 以便框架知道单击行时要执行的操作。此外,您的 Activity 应该实现 View.OnClickListener() 以便您可以控制单击行时发生的情况。现在,当 View 被点击时,你什么都不做;至少根据您的代码,您没有在任何视图的 onClickListeners 上注册回调

回答by Imran Ali

Assign id to Tablerow just like row1

像 row1 一样将 id 分配给 Tablerow

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FF909090"
        android:stretchColumns="1" >

        <TableRow
            android:id="@+id/row1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"

            >

            <TextView
                android:layout_margin="2dip"
                android:layout_marginRight="2dip"
                android:background="#0000ff"
                android:padding="3dip"
                android:text="I" />

            <TextView
                android:layout_margin="2dip"
                android:layout_marginLeft="1dip"
                android:background="#0000ff"
                android:gravity="center"
                android:padding="3dip"
                android:text="A"
                android:onClick="Achord" />
        </TableRow>

        <TableRow>

            <TextView
                android:layout_column="0"
                android:layout_margin="2dip"
                android:background="#0000ff"
                android:padding="3dip"
                android:text="II" />

            <TextView
                android:layout_margin="2dip"
                android:background="#0000ff"
                android:gravity="center"
                android:padding="3dip"
                android:text="Bm" />
        </TableRow>

        <TableRow>

            <TextView
                android:layout_column="0"
                android:layout_margin="2dip"
                android:background="#0000ff"
                android:padding="3dip"
                android:text="III" />

            <TextView
                android:layout_margin="2dip"
                android:background="#0000ff"
                android:gravity="center"
                android:padding="3dip"
                android:text="C#m" />
        </TableRow>

        <View
            android:layout_height="2dip"
            android:background="#FF909090" />

        <TableRow android:background="#0000ff" >
        </TableRow>

        <TableRow>

            <TextView
                android:layout_margin="2dip"
                android:background="#0000ff"
                android:padding="3dip"
                android:text="IV" />

            <TextView
                android:layout_margin="2dip"
                android:background="#0000ff"
                android:gravity="center"
                android:padding="3dip"
                android:text="D" />
        </TableRow>

        <View
            android:layout_height="2dip"
            android:background="#FF909090" />

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TableRow>

        <TableRow>

            <TextView
                android:layout_margin="2dip"
                android:layout_marginRight="2dip"
                android:background="#0000ff"
                android:padding="3dip"
                android:text="V" />

            <TextView
                android:layout_margin="2dip"
                android:layout_marginLeft="1dip"
                android:background="#0000ff"
                android:gravity="center"
                android:padding="3dip"
                android:text="E" />
        </TableRow>

        <TableRow>

            <TextView
                android:layout_margin="2dip"
                android:layout_marginRight="2dip"
                android:background="#0000ff"
                android:padding="3dip"
                android:text="VI" />

            <TextView
                android:layout_margin="2dip"
                android:layout_marginLeft="1dip"
                android:background="#0000ff"
                android:gravity="center"
                android:padding="3dip"
                android:text="F#m" />
        </TableRow>

        <TableRow>

            <TextView
                android:layout_margin="2dip"
                android:layout_marginRight="2dip"
                android:background="#0000ff"
                android:padding="3dip"
                android:text="VII" />

            <TextView
                android:layout_margin="2dip"
                android:layout_marginLeft="1dip"
                android:background="#0000ff"
                android:gravity="center"
                android:padding="3dip"
                android:text="G#dim" />
        </TableRow>




        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.05"
            android:background="#000000"
            android:maxLines="10"
            android:text="The relative minor of the Major key of A is F#m. The 12 bar blues progression in A is: \n A | A | A | A | D | D | A | A | E | D | A | E" />


        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.03" />
        </TableLayout>

Call it in activity like this.

在这样的活动中调用它。

        import android.app.Activity;
        import android.content.Intent;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.TableRow;
        import com.welly.keychords.R;

        public class keya extends Activity {
            TableRow row1;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.keya);
        row1 = (TableRow) findViewById(R.id.row1);
                row1.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        Log.e("Click ", "Row 1");
                    }
                });

        }
        }

回答by user1505181

i got it working by declaring the imageview variable within the public void rowclick

我通过在 public void rowclick 中声明 imageview 变量来让它工作

回答by Pablo Alfonso

  1. Assign an id to Tablerow in xml layout file:

     <TableRow
            android:id="@+id/rowL1"
            ...
     </TableRow>
    
  2. Add the following in your java file:

    private TableRow rowL1;  
    
    //In onCreateView...
    rowL1 = (TableRow) rootView.findViewById(R.id.rowL1);
    rowL1.setOnClickListener(v -> {
        //Your code here...
    });
    
  1. 在 xml 布局文件中为 Tablerow 分配一个 id:

     <TableRow
            android:id="@+id/rowL1"
            ...
     </TableRow>
    
  2. 在您的 java 文件中添加以下内容:

    private TableRow rowL1;  
    
    //In onCreateView...
    rowL1 = (TableRow) rootView.findViewById(R.id.rowL1);
    rowL1.setOnClickListener(v -> {
        //Your code here...
    });