代码之家  ›  专栏  ›  技术社区  ›  Patricia

如何从对话框中获取视图id?

  •  0
  • Patricia  · 技术社区  · 9 年前

    我有一个对话框窗口,其中有一个微调器。根据在所述微调器中选择的值,我必须将一些元素或其他元素加载到布局中。我有这个方法来做,但是.setVisibility不起作用:

            Spinner spinnerTypeQuestion = (Spinner) dialog.findViewById(R.id.spinnerQuestionType);
        spinnerTypeQuestion.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                TextView wrapperRange;
                View viewQuestion = LayoutInflater.from(parent.getContext()).inflate(R.layout.question_dialog, null);
                switch (position){
                    case 0: // Range question
                        wrapperRange = (TextView) viewQuestion.findViewById(R.id.wrapperRange);
                        wrapperRange.setVisibility(View.VISIBLE);
                        break;
                    case 1: // Open answer
                        wrapperRange = (TextView) viewQuestion.findViewById(R.id.wrapperRange);
                        wrapperRange.setVisibility(View.GONE);
                        break; 
                    default:
                        break;
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {}
        });
    

    谢谢

    1 回复  |  直到 9 年前
        1
  •  0
  •   Patricia    9 年前

    看来我解决了自己的问题!我正在发布它,以防有人面临同样的问题。

    而不是使用 就像我做的那样:

    wrapperRange = (TextView) viewQuestion.findViewById(R.id.wrapperRange);
    

    你必须使用你的 对话

    wrapperRange = (TextView) dialog.findViewById(R.id.wrapperRange);