OnClick侦听器不会按预期更改文本

时间:2013-09-26 19:12:33

标签: java android onclick onclicklistener

我正在尝试在mAssistUpdateButton上使用onclick侦听器以及mReadAgainButton,但似乎都没有更改可见文本。

对于我可能做错的任何建议都非常感谢。

来源:

public void onClick(View v) {

        if (v == mAssistUpdateButton) {

        // Update button for ICS and up is selected
        // Get the TextView in the Assist Update UI

        TextView tv = (TextView) findViewById(R.id.apn_app_text_cta2);
        String text = "";
        CharSequence styledText = text;
        switch(v.getId()) {

        //case R.id.read_again_btn {

        case 0:
            // Retrieve the instruction string resource corresponding the
            // 2nd set of instructions
            text = String.format(getString(R.string.apn_app_text_instr),
                    TotalSteps);
            styledText = Html.fromHtml(text);
            // Update the TextView with the correct set of instructions
            tv.setText(styledText);
            // Increment instruction number so the correct instructions
            // string resource can be retrieve the next time the update
            // button is pressed
            mInstructionNumber++;
            break;
        case 1:
            text = getString(R.string.apn_app_text_instr2);
            styledText = Html.fromHtml(text);
            tv.setText(styledText);
            // Increment instruction number so the correct instructions
            // string resource can be retrieve the next time the update
            // button is pressed
            mInstructionNumber++;
            break;
        case 2:
            // Final set of instructions-Change to the corresponding layout

            setContentView(R.layout.assist_instructions);
            String assistUpdateInstr = String.format(
                    getString(R.string.apn_app_text_instr3), TotalSteps);
            styledText = Html.fromHtml(assistUpdateInstr);
            TextView assistInstrText = (TextView) findViewById(R.id.updated_text);
            assistInstrText.setText(styledText);
            mAssistInstrButton = (Button) findViewById(R.id.assist_instr_btn);
            mAssistInstrButton.setOnClickListener(this);
            mReadAgainButton = (TextView) findViewById(R.id.read_again_btn);
            mReadAgainButton.setOnClickListener(this);
        }// end switch

        }// end if
        else if (v == mAssistInstrButton) {
        // "LET'S DO THIS" Button in final instructions screen for ICS and
        // up is selected
        Values = getValues();
        startActivity(new Intent(Settings.ACTION_APN_SETTINGS));
        try {
            showNotification();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finish();
        }// end if
        else if (v == mAssistInstrButton) {
        startActivity(new Intent(Settings.ACTION_APN_SETTINGS));
        try {
            showNotification();
        } catch (SAXException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (ParserConfigurationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            showNotification();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finish();

        }// end if
        else if (v == mReadAgainButton) {
        // go back to set of instructions if read again is selected

        TextView tv = (TextView) findViewById(R.id.apn_app_text_cta2);
        String text = "";
        CharSequence styledText = text;

        switch (mInstructionNumber) {

        case 0:
            // Retrieve the instruction string resource corresponding the
            // 2nd set of instructions
            text = String.format(getString(R.string.apn_app_text_instr),
                    TotalSteps);
            styledText = Html.fromHtml(text);
            // Update the TextView with the correct set of instructions
            tv.setText(styledText);
            // Increment instruction number so the correct instructions
            // string resource can be retrieve the next time the update
            // button is pressed
            mInstructionNumber++;
            break;
        case 1:
            text = getString(R.string.apn_app_text_instr2);
            styledText = Html.fromHtml(text);
            tv.setText(styledText);
            // Increment instruction number so the correct instructions
            // string resource can be retrieve the next time the update
            // button is pressed
            mInstructionNumber++;
            break;
        case 2:
            // Final set of instructions-Change to the corresponding layout

            setContentView(R.layout.assist_instructions);
            String assistUpdateInstr = String.format(
                    getString(R.string.apn_app_text_instr3), TotalSteps);
            styledText = Html.fromHtml(assistUpdateInstr);
            TextView assistInstrText = (TextView) findViewById(R.id.updated_text);
            assistInstrText.setText(styledText);
            mAssistInstrButton = (Button) findViewById(R.id.assist_instr_btn);
            mAssistInstrButton.setOnClickListener(this);
            mReadAgainButton = (TextView) findViewById(R.id.read_again_btn);
            mReadAgainButton.setOnClickListener(this);
            break;
             }// end switch
           }
        }

1 个答案:

答案 0 :(得分:2)

v.getId()会返回传递给id的{​​{1}}(View)的v

您正在检查onClick()的{​​{1}}是否为0,1,2等......

id

您的View语句应为case 0: 的{​​{1}}。

case

我不确定您的期望是什么,但请检查Here

修改

现在您已经解释了您要执行的操作,您可以创建一个作为计数器的成员变量并将其声明为id。然后,您可以保留View语句,但将case R.id.mAssistUpdateButton: // do your work if that button was clicked 更改为

int

请阅读我发布的链接,以便了解case语句,但您要做的是将switch中的变量与switch (counterVariable){ 选项进行比较,以确定要运行的代码块。

每次输入switch时,您显然希望将计数器变量增加一。