我有一个设计布局,如下面的代码,我想为每个复选框编写一个代码,以便在选中时向我显示所选行的索引,但我不知道如何从我的网格布局中动态获取此属性,而不是通过在 Toast.makeText() 中手动键入索引。如果您在这个问题上帮助我,我将不胜感激。
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="5"
android:rowCount="4"
android:orientation="horizontal"
android:background="#FF1354"
android:id="@+id/Grd">
<TextView android:text=" R 1, C 1 "
android:background="@drawable/border" />
<TextView
android:text=" R 1, C 2 "
android:background="@drawable/border"/>
<TextView android:text=" R 1, C 3 "
android:background="@drawable/border" />
<TextView android:text=" R 1, C 4 "
android:background="@drawable/border" />
<CheckBox
android:id="@+id/chk1"
android:text=""
style="?android:attr/starStyle" />
<TextView android:text=" R 2, C 1 "
android:background="@drawable/border" />
<TextView android:text=" R 2, C 2 "
android:background="@drawable/border" />
<TextView android:text=" R 2, C 3 "
android:background="@drawable/border" />
<TextView android:text=" R 2, C 4 "
android:background="@drawable/border" />
<CheckBox
android:id="@+id/chk2"
android:text=""
style="?android:attr/starStyle" />
<TextView android:text=" R 3, C 1 "
android:background="@drawable/border" />
<TextView android:text=" R 3, C 2 "
android:background="@drawable/border" />
<TextView android:text=" R 3, C 3 "
android:background="@drawable/border" />
<TextView android:text=" R 3, C 4 "
android:background="@drawable/border"/>
<CheckBox
android:id="@+id/chk3"
android:text=""
style="?android:attr/starStyle"/>
<TextView android:text=" R 4, C 1 "
android:background="@drawable/border" />
<TextView android:text=" R 4, C 2 "
android:background="@drawable/border" />
<TextView android:text=" R 4, C 3 "
android:background="@drawable/border" />
<TextView android:text=" R 4, C 4 "
android:background="@drawable/border" />
<CheckBox
android:id="@+id/chk4"
android:text=""
style="?android:attr/starStyle" />
</GridLayout>
这是我的代码:
package com.example.hiiiiiiiiiiiiii;
import com.example.mynewtestoftablecheck.MyNewTestTableCheck;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.Toast;
public class Hiiiiiiiiiiiiiiiiiiiiiiii extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hiiiiiiiiiiiiiiiiiiiiiiii);
CheckBox chk1=(CheckBox)findViewById(R.id.chk1);
chk1.setOnClickListener(new View.OnClickListener() {
GridLayout Grd=(GridLayout)findViewById(R.id.Grd);
@Override
public void onClick(View arg0) {
{
if (((CheckBox) arg0).isChecked()) {
Toast.makeText(Hiiiiiiiiiiiiiiiiiiiiiiii.this,
"1st Row checked", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(Hiiiiiiiiiiiiiiiiiiiiiiii.this,
"1st Row Unchecked", Toast.LENGTH_LONG).show();
}
}
}
});
CheckBox chk2=(CheckBox)findViewById(R.id.chk2);
CheckBox chk3=(CheckBox)findViewById(R.id.chk3);
CheckBox chk4=(CheckBox)findViewById(R.id.chk4);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_hiiiiiiiiiiiiiiiiiiiiiiii, menu);
return true;
}
}