【问题标题】:Simple Quiz Android简单测验 Android
【发布时间】:2014-10-24 08:36:00
【问题描述】:

已编辑 *

现在的问题是我是否需要在我的设计中再创建 11 个 TextView,或者我可以为每个问题使用相同的 TextView?而且我已经初始化为1,因为它显示的TextView是TextView。

public class MyActivity4 extends ActionBarActivity implements View.OnClickListener {
    private int nivellactual=1;
    private int elementcorrecte;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_activity4);
    TextView tvJuego = (TextView) findViewById(R.id.tvJuego);
    tvJuego.setShadowLayer(1, 0, 3, Color.GRAY);
    ImageButton borojuego,berijuego,cobajuego,hidrojuego,indijuego,plutojuego,sodijuego,molijuego,zicrojuego
            ,promjuego,ununjuego,radijuego;



    berijuego=(ImageButton)findViewById(R.id.ibBeriJuego);
    borojuego=(ImageButton)findViewById(R.id.ibBoroJuego);
    cobajuego=(ImageButton)findViewById(R.id.ibCobaJuego);
    indijuego=(ImageButton)findViewById(R.id.ibIndiJuego);
    hidrojuego=(ImageButton)findViewById(R.id.ibHidroJuego);
    ununjuego=(ImageButton)findViewById(R.id.ibUnunJuego);
    plutojuego=(ImageButton)findViewById(R.id.ibPlutoJuego);
    radijuego=(ImageButton)findViewById(R.id.ibRadiJuego);
    promjuego=(ImageButton)findViewById(R.id.ibPromJuego);
    sodijuego=(ImageButton)findViewById(R.id.ibSodiJuego);
    zicrojuego=(ImageButton)findViewById(R.id.ibZicroJuego);
    molijuego=(ImageButton)findViewById(R.id.ibMoliJuego);
    berijuego.setOnClickListener(this);
    borojuego.setOnClickListener(this);
    cobajuego.setOnClickListener(this);
    indijuego.setOnClickListener(this);
    hidrojuego.setOnClickListener(this);
    ununjuego.setOnClickListener(this);
    plutojuego.setOnClickListener(this);
    radijuego.setOnClickListener(this);
    promjuego.setOnClickListener(this);
    sodijuego.setOnClickListener(this);
    zicrojuego.setOnClickListener(this);
    molijuego.setOnClickListener(this);





}
private void seguentelement() {

    switch (nivellactual) {

        case 1:

            TextView tvPregunta = (TextView) findViewById(R.id.tvPregunta)//WARNING ERROR;
            String pregunta = ("Quin element és el Hidrògen?");
            tvPregunta.setText(pregunta);
            //PENSANDO
            elementcorrecte = (R.id.ibHidroJuego);//load here the id of the correct answer of each level)
            break;
        case 2:

            TextView tvPregunta2 = (TextView) findViewById(R.id.tvPregunta)//WARNING ERROR;
            String pregunta2 = ("Quin element és el Indio?");
            tvPregunta2.setText(pregunta2);
            //PENSANDO
            elementcorrecte = (R.id.ibIndiJuego);//load here the id of the correct answer of each level)
            break;
        case 3:

            TextView tvPregunta3 = (TextView) findViewById(R.id.tvPregunta)//WARNING ERROR;
            String pregunta3 = ("Quin element és el Cobalto?");
            tvPregunta3.setText(pregunta3);
            //PENSANDO
            elementcorrecte = (R.id.ibCobaJuego);//load here the id of the correct answer of each level)
            break;
        case 4:

            TextView tvPregunta4 = (TextView) findViewById(R.id.tvPregunta);
            String pregunta4 = ("Quin element és el Berilio?");
            tvPregunta4.setText(pregunta4);
            //PENSANDO
            elementcorrecte = (R.id.ibBeriJuego);//load here the id of the correct answer of each level)
            break;
        case 5:

            TextView tvPregunta5 = (TextView) findViewById(R.id.tvPregunta)//WARNING ERROR;
            String pregunta5 = ("Quin element és el Prometio?");
            tvPregunta5.setText(pregunta5);
            //PENSANDO
            elementcorrecte = (R.id.ibPromJuego);//load here the id of the correct answer of each level)
            break;
        case 6:

            TextView tvPregunta6 = (TextView) findViewById(R.id.tvPregunta)//WARNING ERROR;
            String pregunta6 = ("Quin element és el Radio?");
            tvPregunta6.setText(pregunta6);
            //PENSANDO
            elementcorrecte = (R.id.ibRadiJuego);//load here the id of the correct answer of each level)
            break;
        case 7:

            TextView tvPregunta7 = (TextView) findViewById(R.id.tvPregunta)//WARNING ERROR;
            String pregunta7 = ("Quin element és el Plutonio?");
            tvPregunta7.setText(pregunta7);
            //PENSANDO
            elementcorrecte = (R.id.ibPlutoJuego);//load here the id of the correct answer of each level)
            break;
        case 8:

            TextView tvPregunta8 = (TextView) findViewById(R.id.tvPregunta);
            String pregunta8 = ("Quin element és el Ununstrio?");
            tvPregunta8.setText(pregunta8);
            //PENSANDO
            elementcorrecte = (R.id.ibUnunJuego);//load here the id of the correct answer of each level)
            break;
        case 9:

            TextView tvPregunta9 = (TextView) findViewById(R.id.tvPregunta)//WARNING ERROR;
            String pregunta9 = ("Quin element és el Sodio?");
            tvPregunta9.setText(pregunta9);
            //PENSANDO
            elementcorrecte = (R.id.ibSodiJuego);//load here the id of the correct answer of each level)
            break;
        case 10:

            TextView tvPregunta10 = (TextView) findViewById(R.id.tvPregunta)//WARNING ERROR;
            String pregunta10 = ("Quin element és el Boro?");
            tvPregunta10.setText(pregunta10);
            //PENSANDO
            elementcorrecte = (R.id.ibBoroJuego);//load here the id of the correct answer of each level)
            break;
        case 11:

            TextView tvPregunta11 = (TextView) findViewById(R.id.tvPregunta)//WARNING ERROR;
            String pregunta11 = ("Quin element és el Zicronio?");
            tvPregunta11.setText(pregunta11);
            //PENSANDO
            elementcorrecte = (R.id.ibZicroJuego);//load here the id of the correct answer of each level)
            break;
        case 12:

            TextView tvPregunta12 = (TextView) findViewById(R.id.tvPregunta)//WARNING ERROR;
            String pregunta12 = ("Quin element és el Molibdeno?");
            tvPregunta12.setText(pregunta12);
            //PENSANDO
            elementcorrecte = (R.id.ibMoliJuego);//load here the id of the correct answer of each level)
            break;
    }
}
    @Override
    public void onClick (View v){

        //when clicking the correct id, transict to other level, and load new data
        if (v.getId() == elementcorrecte) {
            Toast.makeText(getBaseContext(), "Correcte!", Toast.LENGTH_SHORT).show();
            nivellactual++;
            seguentelement();
        } else {
            Toast.makeText(getBaseContext(), "Error!! :(", Toast.LENGTH_SHORT).show();

        }
    }

【问题讨论】:

    标签: java android class button android-studio


    【解决方案1】:

    希望这个逻辑有帮助:

    //currentLevel and correctAnswer vars
    private int currentlevel;
    private int correctAnswer;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       //load all your UI
    }
    
    private void levelTransition()
    {
       //for each level, load specific text and images
       switch(currentlevel)
       case 1:
       String pregunta=("Quin element és el Hidrògen?");
       tvPregunta.setText(pregunta);
       //... and other things
       correctAnswer = (...) //load here the id of the correct answer of each level
       break;
       case 2:
       //...
    }
    
    @Override
    public void onClick(View v) {
    
        //when clicking the correct id, transict to other level, and load new data
        if(v.getId() == correctAnswer)
                {
                Toast.makeText(getBaseContext(), "Correcte!", Toast.LENGTH_SHORT).show();
                currentlevel++;
                levelTransition();
                }
        else
                {
                Toast.makeText(getBaseContext(), "Error!! :(", Toast.LENGTH_SHORT).show();
                break;
                }
    

    【讨论】:

    • 我认为这对我很有帮助,非常感谢@PedroHawk,我希望你不介意我在实现代码时问你,我是新手,我'正在努力学习.. 我不知道该怎么做的第一件事是在 correctAnswer=(...) 里面的 "()" 我怎样才能把 ImageButton 的答案?或者,如果我输入 ImageButton 的 id,它将起作用
    • 关于您的问题,您有一个带有正确或错误图像按钮的 R.id.* 的开关盒。逻辑是相同的,但您在加载特定关卡时将 R.id.*(正确的)存储在 correctAnswer var 中。
    • 但是如何将 ImageButton 或 ID 设置为 int?因为正确答案是一个 Int,如果我尝试输入 ID / ImageButton / etc.. 它不起作用,这就是为什么我问你如何将正确 ImageButton 的 ID 放入 int
    • switch case上检查您自己的问题。 int id = v.getId(); 该结果是按钮、图像按钮或您单击的任何内容的整数。如果与正确答案 ID(之前存储的)匹配,则正确!
    • 如果此答案对您有帮助,请随时投票或将其标记为正确答案。谢谢
    【解决方案2】:

    XML:
    您可以将问题和答案用作文本视图。 在 XML 中为您的视图提供 ID,例如(questionView、answer1View、answer2View...)。
    Java:
    使用计数器来了解当前的问题。 在柜台上做一个开关盒。 对于每种情况,您都可以使用 questionView.setText("blabla")。 还可以在答案视图上实现 onClickListener,这将使操作正确或错误,最重要的是会增加计数器并使用 switch 案例调用方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-24
      • 1970-01-01
      相关资源
      最近更新 更多