2013-01-08 31 views
0

我的要求是,我有一個多行的表,其中通過選擇單行應該在屏幕上顯示其內容。我想通過爲每行寫onClick()來實現它,但是爲多行寫多個onClick()是不可行的。那麼有沒有什麼辦法可以在不寫多個onClick()的情況下實現這一點。有沒有解決方案,我可以在Android中爲表中的所有行編寫單個onClick方法?

這是我的表中的XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativelayout1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 
<ScrollView android:id="@+id/ScrollView01" 
     android:layout_height="100dp" android:layout_width="wrap_content"> 
    <TableLayout 
     android:id="@+id/tableLayout1" 
     android:layout_width="wrap_content" 
     android:background="#000000" 
     android:stretchColumns="*" 
     android:layout_height="100dp" 
     android:scrollbars="horizontal" 
     > 

     <TableRow 
      android:layout_margin="0.2dip" 
      android:background="#000000" > 

      <TextView 
       android:layout_margin="0.2dip" 
       android:background="#FFFFFF" 
       android:text="@string/code" /> 

      <TextView 
       android:layout_margin="0.2dip" 
       android:background="#FFFFFF" 
       android:text="@string/name" /> 
     </TableRow> 

     <TableRow 
      android:layout_margin="0.2dip" 
      android:background="#000000" 
      android:focusable="true" 
      android:onClick="showData" > 

      <TextView 
       android:id="@+id/code1" 
       android:layout_margin="0.2dip" 
       android:background="#FFFFFF" 
       android:text="@string/code1" /> 

      <TextView 
       android:id="@+id/code2" 
       android:layout_margin="0.2dip" 
       android:background="#FFFFFF" 
       android:text="@string/name1" /> 
     </TableRow> 

我寫的onClick了第二排。

這是的onClick()在活動類:

public void showData(View view){ 
     text = new EditText(this); 

     //text = (EditText) findViewById(R.id.displayData); 
     TextView tx1 = (TextView) findViewById(R.id.code1); 
     TextView tx2 = (TextView) findViewById(R.id.code2); 
     TextView tx3 = (TextView) findViewById(R.id.displayData); 
     tx3 = new TextView(this); 
     String str = tx1.getText().toString(); 
     String str1 = tx2.getText().toString(); 
     tx3.setText("CODE : "+str+" "+"NAME :"+str1); 
     //text.setText("CODE : "+str+" "+"NAME :"+str1); 
     //text.setText("NAME :"+str1); 
     setContentView(tx3); 
    } 
+0

你的意思是你不想寫' android:onClick =「showData」爲XML中的每個表格行嗎?爲什麼這是「不可行」? –

+0

我可以爲每個表格行編寫android:onClick =「showData」...但是如何在活動類中管理它.. –

回答

1

您可以簡單地使用view.findViewById(R.id.code1)等(而不是findViewById沒有view.)找到指定給各行的文本視圖。這樣,所有行都可以對所有行使用相同的android:id值和單個onClick方法。 (這是類似於ListView是如何工作的。)

public void showData(View view){ 
    TextView tx1 = (TextView) view.findViewById(R.id.code1); 
    TextView tx2 = (TextView) view.findViewById(R.id.code2); 
    . . . 
} 

如果你想減少對XML的龐大規模,你可以把屬性成風格。

+0

謝謝!它的作品像魅力! –

1

這可能不是高效的代碼,但你可以利用這一點, 讓你的表ID

tableLayoutGrid = (TableLayout) findViewById(R.id.yourtableid); 
     int count=tableLayoutGrid.getChildCount(); 
     for(int i=0;i<count;i++) 
     { 
      TableRow row=(TableRow) tableLayoutGrid.getChildAt(i); 
      row.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
         TextView tx1 = (TextView) row.getChildAt(0); 
         TextView tx2 = (TextView) row.getChildAt(1); 
         TextView tx3 = (TextView) findViewById(R.id.displayData); 
         tx3 = new TextView(this); 
         String str = tx1.getText().toString(); 
         String str1 = tx2.getText().toString(); 
         tx3.setText("CODE : "+str+" "+"NAME :"+str1); 

       } 
      }); 
     } 

我希望這將幫助你..

+0

謝謝Pragnani! –

相關問題