我正在努力让网格视图中的点击按钮被禁用。这是我的代码;我正在使用自定义网格适配器。
public class MainActivity extends Activity {
private GridView gridView;
private final String[] items = new String[] { "A", "B", "C", "D", "E", "F",
"G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
"T", "U", "V", "W", "X", "Y", "Z" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) this.findViewById(R.id.myGridView);
CustomGridAdapter gridAdapter = new CustomGridAdapter(
MainActivity.this, items);
gridView.setAdapter(gridAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
(CharSequence) gridView.getItemAtPosition(position),
Toast.LENGTH_SHORT).show();
public class CustomGridAdapter extends BaseAdapter {
private Context context;
private String[] items;
LayoutInflater inflater;
public CustomGridAdapter(Context context, String[] items) {
this.context = context;
this.items = items;
inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.cell, null);
}
Button button = (Button) convertView.findViewById(R.id.grid_item);
button.setText(items[position]);
return convertView;
}
@Override
public int getCount() {
return items.length;
}
/**
* Returns the letter of the pushed button
*/
@Override
public Object getItem(int position) {
return items[position];
}
/**
* Returns the position in the gridview
*/
@Override
public long getItemId(int position) {
return position;
}
}
答案 0 :(得分:0)
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// grid list item was clicked, find the button
Button button = (Button) convertView.findViewById(R.id.grid_item);
// turn button off
button.setEnabled(false);
Toast.makeText(getApplicationContext(),
(CharSequence) gridView.getItemAtPosition(position),
Toast.LENGTH_SHORT).show();