创建一个字符串数组以随机调用多个活动

时间:2015-07-11 22:49:02

标签: android

我正在创建一个包含100个活动的应用。每个活动有10个问题,用户猜对了。这是很多编码。如何使编码变得简单 - 使用数组,重构或一些我不知道的设计模式?请参阅下面的代码。

每项活动都很相似,但有不同的问题和答案。

任何帮助都将不胜感激。

private TextView whoami3;
private TextView isa1,isa2,isa3,isa4,isa5,isa6,isa7,isa8,isa9,isa10;
private Button myclick;
private EditText answer;
private TextView mycorrect;
private TextView mytryagain;
private Button next_button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_isaiah);

    whoami3 = (TextView) findViewById(R.id.whoami3);
    isa1 = (TextView) findViewById(R.id.isa1);
    isa2 = (TextView) findViewById(R.id.isa2);
    isa3 = (TextView) findViewById(R.id.isa3);
    isa4 = (TextView) findViewById(R.id.isa4);
    isa5 = (TextView) findViewById(R.id.isa5);
    isa6 = (TextView) findViewById(R.id.isa6);
    isa7 = (TextView) findViewById(R.id.isa7);
    isa8 = (TextView) findViewById(R.id.isa8);
    isa9 = (TextView) findViewById(R.id.isa9);
    isa10 = (TextView) findViewById(R.id.isa10);
    myclick = (Button) findViewById(R.id.myclick);
    answer = (EditText) findViewById(R.id.answer);
    mycorrect = (TextView) findViewById(R.id.mycorrect);
    mytryagain = (TextView) findViewById(R.id.mytryagain);
    next_button = (Button) findViewById(R.id.next_button);



    whoami3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            TextView whoami3 = (TextView) findViewById(R.id.whoami3);
            TextView isa1 = (TextView) findViewById(R.id.isa1);
            TextView isa2 = (TextView) findViewById(R.id.isa2);
            TextView isa3 = (TextView) findViewById(R.id.isa3);
            TextView isa4 = (TextView) findViewById(R.id.isa4);
            TextView isa5 = (TextView) findViewById(R.id.isa5);
            TextView isa6 = (TextView) findViewById(R.id.isa6);
            TextView isa7 = (TextView) findViewById(R.id.isa7);
            TextView isa8 = (TextView) findViewById(R.id.isa8);
            TextView isa9 = (TextView) findViewById(R.id.isa9);
            TextView isa10 = (TextView) findViewById(R.id.isa10);
            Button ansewer = (Button) findViewById(R.id.myclick);
            TextView mycorrect = (TextView) findViewById(R.id.mycorrect);
            TextView mytryagain = (TextView) findViewById(R.id.mytryagain);

            if (whoami3.isShown()) {
                whoami3.setVisibility(View.INVISIBLE);
                isa1.setVisibility(View.VISIBLE);
            } else {
                whoami3.setVisibility(View.VISIBLE);
            }
        }
    });


    isa1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isa1.isShown()) {
                isa1.setVisibility(View.INVISIBLE);
                isa2.setVisibility(View.VISIBLE);

            } else {
                isa1.setVisibility(View.VISIBLE);
            }
        }
    });

    isa2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isa2.isShown()) {
                isa2.setVisibility(View.INVISIBLE);
                isa3.setVisibility(View.VISIBLE);

            } else {
                isa2.setVisibility(View.VISIBLE);
            }
        }
    });

    isa3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isa3.isShown()) {
                isa3.setVisibility(View.INVISIBLE);
                isa4.setVisibility(View.VISIBLE);

            } else {
                isa3.setVisibility(View.VISIBLE);
            }
        }
    });

    isa4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isa4.isShown()) {
                isa4.setVisibility(View.INVISIBLE);
                isa5.setVisibility(View.VISIBLE);

            } else {
                isa4.setVisibility(View.VISIBLE);
            }
        }
    });

    isa5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isa5.isShown()) {
                isa5.setVisibility(View.INVISIBLE);
                isa6.setVisibility(View.VISIBLE);

            } else {
                isa5.setVisibility(View.VISIBLE);
            }
        }
    });

    isa6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isa6.isShown()) {
                isa6.setVisibility(View.INVISIBLE);
                isa7.setVisibility(View.VISIBLE);

            } else {
                isa6.setVisibility(View.VISIBLE);
            }
        }
    });

    isa7.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isa7.isShown()) {
                isa7.setVisibility(View.INVISIBLE);
                isa8.setVisibility(View.VISIBLE);

            } else {
                isa7.setVisibility(View.VISIBLE);
            }
        }
    });

    isa8.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isa8.isShown()) {
                isa8.setVisibility(View.INVISIBLE);
                isa9.setVisibility(View.VISIBLE);

            } else {
                isa8.setVisibility(View.VISIBLE);
            }
        }
    });

    isa9.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isa9.isShown()) {
                isa9.setVisibility(View.INVISIBLE);
                isa10.setVisibility(View.VISIBLE);

            } else {
                isa9.setVisibility(View.VISIBLE);
            }
        }
    });

    myclick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String yourString = "ISAIAH";

            if (answer.getText().toString().trim().toLowerCase().equals("ISAIAH".toString().trim().toLowerCase())) {
                mycorrect.setVisibility(View.VISIBLE);
            } else {

                mytryagain.setVisibility(View.VISIBLE);

                if (answer.getText().toString().trim().toLowerCase()!=("MARK".toString().trim().toLowerCase())) {
                    answer.setText("");
                }

            }
            mytryagain.setText("Sorry, Try again");
            mytryagain.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mytryagain.setVisibility(View.INVISIBLE);
                }
            }, 2000);
        }


    });

}
public void onClick(View view){
    Random rnd = new Random();
    int x=rnd.nextInt(4)+1;
    Intent intent = new Intent();

    switch (x){
        case 1:
            intent.setClass(this,John.class);
            break;
        case 2:
            intent.setClass(this,Caleb.class);
            break;
        case 3:
            intent.setClass(this,Mark.class);
            break;
        case 4:
            intent.setClass(this,Jacob.class);
            break;
    }
    startActivity(intent);
    finish();
}

1 个答案:

答案 0 :(得分:0)

您可以使用Class.forName()方法,因此onClick方法可以像这样重写:

Random rnd = new Random();
int x=rnd.nextInt(4);
String[] clazzes = {John.class.getName(), Caleb.class.getName(), Mark.class.getName(), Jacob.class.getName()};
Intent intent = new Intent();
try {
intent.setClass(this, Class.forName(clazzes[x]));

startActivity(intent);
finish();
} catch (Exception e) {  
// your exception here when class not found here
}

但如果你有相同的UI,100个活动的功能,我建议你只使用1个活动,但通过Intent发送差异数据。