TextView OnTouch无法正常工作

时间:2013-07-25 10:09:17

标签: android

我有表格布局,我在其中动态添加文本视图 我在文本视图上设置OnTouch侦听器。
MotionEvent.ACTION_DOWN上显示弹出窗口并在MotionEvent.ACTION_UP上显示。我正在删除该弹出窗口,但我想如果从textview中删除了触摸,那么pop也应该删除了我已尝试使用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);

    }

1 个答案:

答案 0 :(得分:0)

典型错误 - 每次MotionEvent触发时都会创建弹出窗口。因此,之前创建的弹出窗口无法被删除,因为您丢失了它的链接 - 另一次程序进入onTouch()时,您将无法获得弹出窗口的链接(因为它在onTouch()中声明)。使弹出窗口成为OnTouchListener的一个字段。并且仅创建它。

步骤一步:

MotionEvent.ACTION_DOWN火 - 您创建并显示弹出窗口,它在屏幕上 MotionEvent.ACTION_UP火灾 - 你创建了新的弹出窗口并将其解雇,但不是旧的弹出窗口