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

时间:2012-08-01 03:11:18

标签: android button gridview

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

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; 
        }
    }    
}

1 个答案:

答案 0 :(得分:9)

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

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; 
        }
    }    
}