Java – is my code too repetitive?

I'm studying my text adventure game and wonder if there's an easier way to write repeated code blocks, such as what I'm saying below

In this block, N, e, s and W of North, East, South and West are presented to the user. Therefore, I write each listener separately and include a try / catch block in each listener But now the whole code block seems to be repeated

This is a code block:

btnNorth.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            tvIntro.setText("You go north");
            String testString = "nothing";
            try {
                testString = Encounter.EncounterGeneratorText();
            } catch (Exception e) {
                    e.printStackTrace();
                }
            testString = Encounter.EncounterGeneratorText();
            tvIntro.setText(testString);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            tvIntro.setText(testString);            
        }
    });

    btnEast.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            tvIntro.setText("You go east");
            String testString = "nothing";
            try {
                testString = Encounter.EncounterGeneratorText();
            } catch (Exception e) {
                    e.printStackTrace();
                }
            testString = Encounter.EncounterGeneratorText();
            tvIntro.setText(testString);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            tvIntro.setText(testString);
        }
    });

    btnSouth.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            tvIntro.setText("You go south");
            String testString = "nothing";
            try {
                testString = Encounter.EncounterGeneratorText();
            } catch (Exception e) {
                    e.printStackTrace();
                }
            testString = Encounter.EncounterGeneratorText();
            tvIntro.setText(testString);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            tvIntro.setText(testString);
        }
    });

    btnWest.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            tvIntro.setText("you go west");
            String testString = "nothing";
            try {
                testString = Encounter.EncounterGeneratorText();
            } catch (Exception e) {
                    e.printStackTrace();
                }
            testString = Encounter.EncounterGeneratorText();
            tvIntro.setText(testString);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            tvIntro.setText(testString);
        };
    });

Solution

You can create an onclicklistener that will be used by all buttons. In this onclicklistener, you will detect which button is clicked and perform direction specific tasks in the switch statement

Something like this:

private OnClickListener DirectionClickListner = new OnClickListener() {
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.north:
            //Set your strings for North
            break;
        case R.id.west:
            //Set your strings for West
            break;
        case R.id.east:
            //Set your strings for East
            break;
        case R.id.south:
            //Set your strings for South
            break;
        }
    }
};
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>