我的要求是,我有一個多行的表,其中通過選擇單行應該在屏幕上顯示其內容。我想通過爲每行寫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);
}
你的意思是你不想寫' android:onClick =「showData」爲XML中的每個表格行嗎?爲什麼這是「不可行」? –
我可以爲每個表格行編寫android:onClick =「showData」...但是如何在活動類中管理它.. –