android gridview中的CheckBox无法在第一次点击时工作

时间:2015-08-04 11:28:27

标签: android checkbox android-gridview

我正在做一个Android应用程序,在JSON Web服务的网格视图中加载复选框。到目前为止它完美运作。当我第一次点击复选框时,它没有触发事件。但第二次它正常工作。如果有人知道,请帮帮我

我的java代码:

var disableNgAnimate = function() {
  angular.module('disableNgAnimate', []).run(['$animate', function($animate) {
    $animate.enabled(false);
  }]);
};
browser.addMockModule('disableNgAnimate',disableNgAnimate);

我的布局XML:

@SuppressWarnings("deprecation")
    public void showFolderPopUPforEdit() {

        int count = 0;
        String folderID = null;
        String selectedfolderID = null;

        LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                .getSystemService(LAYOUT_INFLATER_SERVICE);
        editpopupView = layoutInflater.inflate(R.layout.edit_folders, null);
        editpopupWindow = new PopupWindow(editpopupView,
                LayoutParams.MATCH_PARENT, 1060);
        editpopupWindow.showAtLocation(editpopupView, Gravity.BOTTOM, 0, 0);

        try {
            leadna_leadsFolderjsonaray = leadna_leadFoldersjson
                    .getJSONArray("folderList");
            for (int i = 0; i < leadna_leadsFolderjsonaray.length(); i++) {
                cb = new CheckBox(this);
                cb.setChecked(false);
                cb.setFocusable(false);
                cb.setFocusableInTouchMode(false);              
                cb.setBackground(getResources()
                        .getDrawable(R.drawable.uncheck1));
                cb.setButtonDrawable(android.R.color.transparent);
                JSONObject c = leadna_leadsFolderjsonaray.getJSONObject(i);
                folderID = c.getString("category_id");
                for (int j = 0; j < leadna_leadsSelectedFolderjsonaray.length(); j++) {
                    JSONObject arr2 = leadna_leadsSelectedFolderjsonaray
                            .getJSONObject(j);
                    selectedfolderID = arr2.getString("attach_category");
                    if (folderID.equalsIgnoreCase(selectedfolderID)) {
                        cb.setChecked(true);
                        cb.setBackground(getResources().getDrawable(
                                R.drawable.check_btn));
                        cb.setFocusable(false);
                        cb.setFocusableInTouchMode(false);  
                        cb.setButtonDrawable(android.R.color.transparent);
                        break;
                    }
                }
                cb.setText(c.getString("category_name"));
                cb.setTextColor(Color.BLACK);
                cb.setTextSize((float) 14);
                cb.setTag(c.getString("category_id"));
                cb.setLayoutParams(new GridView.LayoutParams(346, 90));
                cb.setPadding(54, 0, 0, 0);
                cb.setOnClickListener(this);
                cb.setId(i);
                cb.requestFocus();
                meditButtons.add(cb);
            }

            GridView gridView = (GridView) editpopupView
                    .findViewById(R.id.gridEditView1);          
            gridView.setAdapter(new CustomAdapter(this, meditButtons));


        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        Button btnSaveFolders = (Button) editpopupView
                .findViewById(R.id.btnAddActivity);
        btnSaveFolders.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });

        Button btnDismiss = (Button) editpopupView.findViewById(R.id.button6);
        btnDismiss.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                editpopupWindow.dismiss();
            }
        });
    }   


    @Override
    public void onClick(View v) {
        CheckBox selection = (CheckBox)v;
        //selection.setCompoundDrawablesWithIntrinsicBounds(R.drawable.clickfolders,0, 0, 0);       
        selection.requestFocus();       
        if (selection.isChecked() == true) {
            selection.setBackground(getResources().getDrawable(
                    R.drawable.uncheck1));
            //selection.setButtonDrawable(android.R.color.transparent);
        } else {
            selection.setBackground(getResources().getDrawable(
                    R.drawable.check_btn));
            //selection.setButtonDrawable(android.R.color.transparent);
        }
        //Toast.makeText(getBaseContext(),  selection.getTag()+ " was pressed!", Toast.LENGTH_SHORT).show();
    }

1 个答案:

答案 0 :(得分:1)

尝试使用

checkbox.setOnCheckedChangeListener( new OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(), "Check box "+arg0.getText().toString()+" is "+String.valueOf(arg1) , Toast.LENGTH_LONG).show();
   }
  } );

而不是clickListener ..