如何以编程方式禁用函​​数onclick按钮

时间:2016-12-27 09:28:49

标签: javascript android for-loop button

这是我的编程按钮,logcat中存在错误空指针异常。

这是我循环按钮的方法。但是按钮的禁用按钮无法正常工作。当我点击“保存”按钮时,编程按钮将禁用。我禁用按钮的方式是不正确的?

 String CountQuery = "SELECT * FROM Category";
    db = new DBController(getActivity());
    SQLiteDatabase db2 = db.getReadableDatabase();
    Cursor cursor1 = db2.rawQuery(CountQuery, null);
    {
        int num = cursor1.getCount();
        Button[] valueB = new Button[num];
        for (int i = 1; i < num; i++) {

            String SelectQuery = "SELECT  * FROM Category where CategoryID='" + i + "'";
            db = new DBController(getActivity());
            SQLiteDatabase db1 = db.getReadableDatabase();
            Cursor cursor = db1.rawQuery(SelectQuery, null);

            if (cursor.moveToNext()) {

                String categoryName = cursor.getString(1);
                String coordinateX = cursor.getString(2);
                String coordinateY = cursor.getString(3);

                valueB[i] = new Button(getActivity());
                valueB[i].setText("" + categoryName);
                valueB[i].setId(i);
                valueB[i].setOnTouchListener(this);

                params = new RelativeLayout.LayoutParams(300, 100);
                params.leftMargin = Integer.parseInt(coordinateX);
                params.topMargin = Integer.parseInt(coordinateY);

                final int finalI = i;
                valueB[i].setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        Toast.makeText(getActivity(), "asdasd" + finalI, Toast.LENGTH_SHORT).show();
                        viewPager = (ViewPager) getActivity().findViewById(R.id.pager);
                        viewPager.setCurrentItem(finalI);
                    }
                });
                mRrootLayout.addView(valueB[i],params);
            }

            save.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    for (i[0] = 1; i[0] < num; i[0]++) {
                        valueB[i[0]].setOnTouchListener(null);
                    }

                }
            });
        }

    }

2 个答案:

答案 0 :(得分:0)

您可以使用valueB[i].setOnClickListener(null);

停用onclick侦听器

答案 1 :(得分:0)

您可以使用setClickable方法停用该按钮。

valueB[i].setClickable(false);

或者您也可以像

一样使用setEnabled方法
valueB[i].setEnabled(false);