1

我有表格布局,我在其中动态添加文本视图。
我在文本视图上设置 OnTouch 侦听器。
MotionEvent.ACTION_DOWN我显示弹出窗口和MotionEvent.ACTION_UP。我正在删除该弹出窗口,但我希望如果从文本视图中删除触摸,那么
我尝试过的弹出窗口也应该删除MotionEvent.ACTION_MOVEMotionEvent.ACTION_OUTSIDE但两者都不起作用

public void addButton_with_filter(final Test test,
        TableRow.LayoutParams rowParams, Activity context) {

    ArrayList<Integer> filteredquestionno;
    TableLayout tl = new TableLayout(context);
    tl.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));
    LinearLayout filterlayout = (LinearLayout) findViewById(R.id.filterlayout);
    filterlayout.removeAllViewsInLayout();
    filterlayout.setBackgroundResource(R.drawable.background1);
    int j = 0;
    int k = 0;

    filteredquestionno = getLayoutFilteredQuestionNumber(test
            .getQuestions());

    int size = filteredquestionno.size();
    if (size == 0) {
        Toast.makeText(this,
                "NO QUSTION IS AVAILABLE OF THIS FILTER TYPE !",
                Toast.LENGTH_LONG).show();
    }
    int norows = (size / 10) + 1;
    {
        for (int i = 0; i < norows; i++) {
            TableRow currentRow = new TableRow(FilterActivity.this);
            // currentRow.setBackgroundResource(R.drawable.background1);

            while (j < 10 && k < size) {
                j++;
                final TextView qindex = new TextView(FilterActivity.this);
                qindex.setGravity(Gravity.CENTER);
                int index = filteredquestionno.get(k);
                Question question = test.getQuestionAtIndex(index);
                if (question != null) {
                    if (submited == true) {
                        if (question.isSkipped()) {
                            qindex.setBackgroundResource(R.drawable.skipped_answer);
                        } else if (question.isCorrectlyAnswered()) {
                            qindex.setBackgroundResource(R.drawable.correct_answer);
                        } else if (question.isAttempted()) {
                            qindex.setBackgroundResource(R.drawable.wrong_answer);
                        } else {
                            qindex.setBackgroundResource(R.drawable.unattempted_question);
                        }
                    } else if (submited == false) {
                        if (question.isSkipped()) {
                            qindex.setBackgroundResource(R.drawable.skipped_answer);
                        } else if (question.isAttempted()) {
                            qindex.setBackgroundResource(R.drawable.attempted_question);
                        } else {
                            qindex.setBackgroundResource(R.drawable.unattempted_question);
                        }
        enter code here         }

                } else {
                    qindex.setBackgroundResource(R.drawable.unattempted_question);
                }

                qindex.setLayoutParams(new LayoutParams(
                        LayoutParams.MATCH_PARENT,
                        LayoutParams.MATCH_PARENT));
                qindex.setTextSize(11);

                if ((index + 1) < 10) {
                    qindex.setText("0" + (index + 1));
                } else {
                    qindex.setText("" + (index + 1));
                }
                k++;

                qindex.setId(k);

                qindex.setOnTouchListener(new OnTouchListener() {

                    @Override
                    public boolean onTouch(View arg0, MotionEvent event) {

                        String text = (String) qindex.getText();
                        String t = text.trim();
                        int questionindex = Integer.parseInt(t);
                        LayoutInflater questionbutton_inflater = (LayoutInflater) getBaseContext()
                                .getSystemService(LAYOUT_INFLATER_SERVICE);
                        View questionbutton_view = questionbutton_inflater
                                .inflate(R.layout.filterbutton, null);
                        final PopupWindow questiontext_popupwindow = new PopupWindow(
                                questionbutton_view,
                                LayoutParams.WRAP_CONTENT,
                                LayoutParams.WRAP_CONTENT);

                        questiontext_popupwindow
                                .setContentView(questionbutton_view);
                        TextView tv = (TextView) questionbutton_view
                                .findViewById(R.id.texttoadd);
                        tv.setText(text);

                        if (event.getAction() == MotionEvent.ACTION_DOWN) {

                            questiontext_popupwindow.showAsDropDown(qindex,
                                    0, -125);

                        }
                        if (event.getAction() == MotionEvent.ACTION_UP) {
                            questiontext_popupwindow.dismiss();
                            test.setLastQuestionAttempted(questionindex - 1);
                            FilterActivity.this.finish();
                        }
                        if (event.getAction() == MotionEvent.ACTION_MOVE) {
                            questiontext_popupwindow.dismiss();

                        }
                        return true;
                    }
                });

                currentRow.addView(qindex, rowParams);
            }
            j = 0;
            tl.addView(currentRow);
        }

        filterlayout.addView(tl);

    }
4

1 回答 1

0

典型错误 - 每次 MotionEvent 触发时都会创建弹出窗口。所以之前创建的弹出窗口不能被关闭,因为你丢失了它的链接 - 另一个时候你的程序进入 onTouch() 你将没有指向你的弹出窗口的链接(因为它是在 onTouch() 中声明的)。使您的弹出窗口成为 OnTouchListener 的字段。并且只创建一次。

一步步:

MotionEvent.ACTION_DOWNfires - 你创建并显示弹出窗口,它在屏幕上 MotionEvent.ACTION_UPfires - 你创建新的弹出窗口并关闭它,但不是旧的

于 2013-07-25T10:26:50.117 回答