2

我正在开发一个基于测验的应用程序,当用户单击任何选项时,它应该出现合适的消息,例如“你的答案是正确的”、“你的答案是错误的”。我想要的是

 1. how to display that sort of messages ?or is it the only way to display such msgs ?
 2. If the user clicks wrong option the it should show the correct answer as well as the msg

here is what i have done so far but its not working giving force close!!!


public void onClick(View v) 
    {
        // TODO Auto-generated method stub
        switch (v.getId()) 
        {
        case R.id.button1:
            Log.d("ERR", v.getTag().toString());
            if (v.getTag().toString().equalsIgnoreCase("right")) 
            {
                displayAnswer();

            }
            else
            {
                errorAnswer();

            }

            break;
        case R.id.button2:
            Log.d("ERR", v.getTag().toString());
            if (v.getTag().toString().equalsIgnoreCase("right")) 
            {
                displayAnswer();
            }
            else
            {
                errorAnswer();

            }
            break;

        case R.id.button3:
            Log.d("ERR", v.getTag().toString());
            if (v.getTag().toString().equalsIgnoreCase("right")) 
            {
                displayAnswer();
            }
            else
            {
                errorAnswer();

            }
            break;

        case R.id.button4:
            Log.d("ERR", v.getTag().toString());
            if (v.getTag().toString().equalsIgnoreCase("right")) 
            {
                displayAnswer();
            }
            else
            {
                errorAnswer();


            }
            break;

        case R.id.btn_next:
//          lyt_ans.setVisibility(View.GONE);
//          lyt_quest.setVisibility(View.VISIBLE);
            prev = counter;
            counter += 1;

            if (counter >= SIZE) 
            {
                Collections.shuffle(quizIndexList);
                counter = 0;
            }
            getInfoFromDB(quizIndexList.get(counter));
            reLoad();

            break;

        case R.id.btn_bck:
                getInfoFromDB(quizIndexList.get(prev));
                    reLoad();
//          counter --;




        }
    }

    private void errorAnswer() 
    {
        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(SIZE);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setText("your answer is wrong");
        // TODO Auto-generated method stub

    }

    private void displayAnswer() {

        // TODO Auto-generated method stub
        lyt_quest = (LinearLayout) findViewById(R.id.lyt_quest);
        lyt_ans = (LinearLayout) findViewById(R.id.lyt_ans);
        lyt_quest.setVisibility(View.VISIBLE);
        lyt_ans.setVisibility(View.VISIBLE);
//      TextView txt1 = null;
        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(SIZE);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setText("Your answer is correct!!");
    }

任何帮助都是可观的。

4

2 回答 2

5

你为什么不使用

Toast.makeText(getApplicationContext(), "Your message", Toast.LENGTH_SHORT).show();

您在代码中编写的内容会给您一个错误,提示 This toast is never created using Toast.makeText()。所以你最好使用这种方法。

如果您想使用自定义 toast 消息,那么您在代码中没有做的一件事是强制性的。创建toast 对象后,您必须使用setView(your inflated layout object)将视图设置为该 toast 。然后你必须最后调用toast.show()。但请记住,您必须获取膨胀的布局并将其传递给setView()

有关自定义吐司的更多信息,请参阅此链接

于 2012-04-10T05:01:15.543 回答
4
v.getTag()

setTag(object)在您的代码中找不到任何内容。

一旦你 setTag() 到视图,然后只有你可以 getTag() 相同的视图。

Log.d("ERR", v.getTag().toString());

if (v.getTag().toString().equalsIgnoreCase("right"))

错误会在你展示之前出现Toast

你也错过了,查看吐司toast.show()

于 2012-04-10T04:59:08.140 回答