gpt4 book ai didi

Android:按钮的 GridView 但按钮没有响应

转载 作者:行者123 更新时间:2023-11-29 14:28:22 27 4
gpt4 key购买 nike

我在 GridView 上有一个非常基本的程序,其中包含一些按钮,如下面的代码所示。程序运行良好,按钮显示在网格中,但单击时它们没有响应。如果我将我的代码更改为图像的 GridView ,它工作正常。我相信我在 gridView.setOnItemClickListenerpublic View getView() 的某个地方错了。有什么建议吗?谢谢

public class MainActivity extends Activity {
public String[] filenames ={"B1","B2","B3","B4","B5","B6"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new ButtonAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent,
View v, int position, long id){
Toast.makeText(getBaseContext(),
"pic" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();
}
});
}

public class ButtonAdapter extends BaseAdapter {
private Context context;
public ButtonAdapter(Context c){
context = c;
}
public int getCount() {
return filenames.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
Button btn;
if (convertView == null) {
btn = new Button(context);
btn.setLayoutParams(new GridView.LayoutParams(100, 100));
btn.setPadding(8, 8, 8, 8);
}else {
btn = (Button) convertView;
}
btn.setText(filenames[position]);
btn.setTextColor(Color.WHITE);
btn.setId(position);
return btn;
}
}
}

最佳答案

您需要将 Buttons 属性 focusable 和 clickable 设置为 false。因为在当前情况下焦点和点击事件是由按钮捕获的,而不是在 GridView 项上。将您的代码更改为以下内容:

public class MainActivity extends Activity {
public String[] filenames ={"B1","B2","B3","B4","B5","B6"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new ButtonAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent,
View v, int position, long id){
Toast.makeText(getBaseContext(),
"pic" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();
}
});
}

public class ButtonAdapter extends BaseAdapter {
private Context context;
public ButtonAdapter(Context c){
context = c;
}
public int getCount() {
return filenames.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
Button btn;
if (convertView == null) {
btn = new Button(context);
btn.setLayoutParams(new GridView.LayoutParams(100, 100));
btn.setPadding(8, 8, 8, 8);
btn.setFOcusable(false);
btn.setClickable(false);
}else {
btn = (Button) convertView;
}
btn.setText(filenames[position]);
btn.setTextColor(Color.WHITE);
btn.setId(position);
return btn;
}
}
}

关于Android:按钮的 GridView 但按钮没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11752074/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com