2012-11-01 16 views
2

我有一个简单的程序,我在java中为android做出的,但是在使得tablerow可点击时遇到困难。当用户点击一行时,我希望在下面的图像视图中显示某个图片。这里是我的XML代码:Android上的可点击桌面行

 <?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" /> 

,这里是我的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); 
} 
} 

我希望发生的那是什么imageview1将显示这取决于被点击哪一行不同的图像。任何帮助将不胜感激

萨姆建议我ammended我的代码如下:

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; 
     } 
    } 
    } 

,但现在我已经在运行的类空指针异常。所有编译罚款虽然

回答

2

给每行一个ID,那么你就可以监听器分配给每一个在你的代码:

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

只需给每个的TableRow元素的独特id和定义onClick方法:

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

添加在您的布局中引用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; 
    } 
} 

明白,一个内部的TableRow的任何元素与自己onClick方法将调用它自己的方法。例如,带有android:onClick="Achord"的TextView将调用Achord()而不是rowClick()


加成
你需要调用findViewById()onCreate()setContentView()声明布局。

ImageView imagev; 

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

我已经改变了我的代码如下:公共类科亚延伸活动{ 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: imagev.setImageResource(R.drawable.android_normal); 休息; } } }但在运行时产生空指针异常 – user1505181

+1

请参阅我编辑的第一篇文章山姆,因为评论框不像代码 – user1505181

+0

是的,评论有最小的标记功能。感谢您将代码添加到您的问题中,我希望更新能够让答案清楚。 – Sam

0

您需要为每个TableRow对象调用setOnClickListener(),以便框架知道单击某行时该做什么。另外你的Activity应该实现View.OnClickListener(),这样你就可以控制点击某一行时会发生什么。现在,当视图被点击时你没有做任何事情;至少根据您的代码,你不能在任何的意见onClickListeners

0

给ID的注册的回调为tablerow就像ROW1

<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> 

说它像这样的活动。

 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"); 
        } 
       }); 

     } 
     } 
0

我得到它由公共无效rowclick内声明的ImageView变量工作

相关问题