OnClick 侦听器未按预期运行

时间:2023-01-15
本文介绍了OnClick 侦听器未按预期运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 TextView (mReadAgainButton) 上设置了一个 onClickListener,它应该从头开始重新加载一组指令(最初显示通过 Button 上的 onClickListener 调用 mAssistUpdateButton) 但是当点击 TextView mReadAgainButton 似乎什么也没发生.

I have an onClickListener set on a TextView (mReadAgainButton) which is supposed to reload a set of instructions from the beginning (initially displayed via an onClickListener on a Button called mAssistUpdateButton) however when clicking on the TextView mReadAgainButton nothing seems to happen.

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 (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);
            }
        } 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();
        } 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();

        } 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);
            }
        }
    }

完整来源:

public class ConfigFinalActivity extends Activity implements OnClickListener {
    private static final String TAG = "ConfigFinalActivity";
    TelephonyManager tm;
    AlertDialog mErrorAlert = null;
    private Notification mNotification = null;
    private Button mXButton = null;
    private ImageView mAssistUpdateButton = null;
    private Button mAssistInstrButton = null;
    private TextView mReadAgainButton = null;
    private int mInstructionNumber = 0;
    public static ArrayList<String> NameArr = new ArrayList<String>();
    public static ArrayList<String> ValueArr = new ArrayList<String>();
    public static ArrayList<String> nameArr = new ArrayList<String>();
    public static ArrayList<String> ApnArr = new ArrayList<String>();
    public static ArrayList<String> mmscArr = new ArrayList<String>();
    public static ArrayList<String> mmsportArr = new ArrayList<String>();
    public static ArrayList<String> mmsproxyArr = new ArrayList<String>();
    public static ArrayList<String> portArr = new ArrayList<String>();
    public static ArrayList<String> proxyArr = new ArrayList<String>();
    public static int count;
    public static int TotalSteps = 10;
    int i, g = 0, result = 0;

    public static ContentValues Values = new ContentValues();

    public static final Uri APN_TABLE_URI = Uri
            .parse("content://telephony/carriers");
    public static InputStream stream = null;
    UpdateActivity update;
    public static String status, queryResult = "";

    /** Called when the activity is first created. */
    @SuppressLint("NewApi")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int version = android.os.Build.VERSION.SDK_INT;
        tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        update = new UpdateActivity();
        getArrayLists();

        if (ApnArr.isEmpty() || mmscArr.isEmpty()) {

            tryagain();
        } else if (version < VERSION_CODES.ICE_CREAM_SANDWICH) {

            SettingsDbAdapter.setArrayLists(nameArr, ApnArr, mmscArr,
                    mmsproxyArr, mmsportArr, proxyArr, portArr, count,
                    ConfigFinalActivity.this);
            // Update APN table
            try {
                result = updateTable();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }// Settings updated with this atomic call
            catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            if (result != -1) {
                status = "success";

            } else {
                status = "failure";
            }

            if (status.equals("success")) {
                completeUpdate();
            } else if (status.equals("failure")) {
                tryagain();

            }

        } else {// ICS and later versions

            // Reduce number of steps to 10
            TotalSteps = 10;
            setContentView(R.layout.assist_update);
            String assistUpdate = getString(R.string.instructions_1);
            mAssistUpdateButton = (ImageView) findViewById(R.id.assist_update_btn);
            mAssistUpdateButton.setOnClickListener(this);

        }
    }

    private void getArrayLists() {
        nameArr = update.getnameArr();
        ApnArr = update.getApnArr();
        mmscArr = update.getMMSCArr();
        mmsproxyArr = update.getMmscProxyArr();
        mmsportArr = update.getMmsPortArr();
        proxyArr = update.getProxyArr();
        portArr = update.getPortArr();
        count = update.getCount();
        queryResult = update.getResult();
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.assist_update_btn:
            switch (mInstructionNumber) {

            }
            // 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 (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);
            }
        /