0

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

非常感谢任何关于我可能做错了什么的建议。

资源:

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
           }
        }
4

1 回答 1

2

v.getId()返回传递给 的id( View)的。vonClick()

您正在检查您idView是否为 0、1、2 等...

case 0:

你的case陈述应该是id一个View

case R.id.mAssistUpdateButton:
    // do your work if that button was clicked

我不确定您期望它们是什么,但请查看此处

编辑

现在您已经解释了您要做什么,您可以创建一个作为计数器的成员变量并将其声明为int. 然后,您可以保留这些case陈述,但将其更改switch

switch (counterVariable){

请阅读我发布的链接,以便您理解switch语句,但您所做的是将变量中的变量switchcase选项进行比较以确定要运行的代码块。

您显然希望每次输入onClick().

于 2013-09-26T19:16:11.110 回答