简体   繁体   English

显示 toast 消息

[英]Displaying toast messages

I am developing an quiz based app in that when the user clicks any of the options it should the a suitable messages such as "your ans is correct","your ans is wrong".What i wanted is我正在开发一个基于测验的应用程序,当用户单击任何选项时,它应该显示一条合适的消息,例如“你的答案是正确的”、“你的答案是错误的”。我想要的是

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

Any help is appreciable.任何帮助都是可观的。

why don't you use你为什么不使用

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

What you have written in your code will give you an error saying This toast was never created using Toast.makeText() .您在代码中编写的内容会给您一个错误提示 This toast was never created using Toast.makeText() So it is better for you to use this method.所以你最好使用这个方法。

If you want to use custom toast message, then one thing is compulsory what you have not done in your code.如果您想使用自定义吐司消息,那么您必须在代码中完成一件事。 After creating your toast object , you have to set the view to that toast using setView(your inflated layout object) .在创建toast object之后,您必须使用setView(your inflated layout object)将视图设置为该 toast。 and then you have to invoke toast.show() at last.然后你必须最后调用toast.show() But remember, you have to get the inflated layout and pass it to setView() .但请记住,您必须获取膨胀后的布局并将其传递给setView()

For more about custom toast see this link有关自定义吐司的更多信息,请参阅此链接

v.getTag()

I am not finding any setTag(object) in your code.我在您的代码中没有找到任何setTag(object)

once you setTag() to the view, then and then only you can getTag() of the same view.一旦您将 setTag() 设置到视图,那么只有您可以获取同一视图的 getTag() 。

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

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

Error will come here before you showing Toast在显示Toast之前会出现错误

you also missing, to view the Toast toast.show()你还缺,查看Toast toast.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM