简体   繁体   English

单击按钮时如何制作Toast消息

[英]How to make toast message when button is clicked

I'm creating a quiz. 我正在创建一个测验。 I have 3 buttons (options) in the activity and its corresponding questions. 我在活动中有3个按钮(选项)及其相应的问题。 Now, my problem is I want to show the toast message, when the user chose the correct answer the toast message will appear in a few seconds, but when the user choose the wrong answer it will again display the toast message. 现在,我的问题是我想要显示吐司消息,当用户选择正确的答案时,吐司消息将在几秒钟内出现,但是当用户选择了错误的答案时,它将再次显示吐司消息。 I do not know how to do that. 我不知道如何去做。

I have done many research and read forums, but seems I don't understand and it don't meet my needs. 我做了很多研究和阅读论坛,但似乎我不理解,它不符合我的需要。 Can someone please help? 有人可以帮忙吗? thanks in advance! 提前致谢!

So far, here is the code. 到目前为止,这是代码。 But it doesn't work. 但它不起作用。
Please correct me which code is wrong. 请纠正我的错误代码。 Thanks deeply. 非常感谢。

btn1.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View arg0) {
    // TODO Auto-generated method stub
    Button btn1 = (Button) findViewById(R.id.btnopt1_a);
    btn1.isClickable();
    switch(arg0.getId()){  
      case R.id.btnopt1_a:  
          if(btn1.isPressed()){  
             Toast.makeText(getBaseContext(), "Your answer is correct!" , Toast.LENGTH_SHORT ).show();  
           }  
           else btn1.setText("Your answer is wrong!, The correct answer is: Frog");  
             break;  
           }
      }
    });

To show toast message when you click button, use the following code. 要在单击按钮时显示Toast消息,请使用以下代码。 If you want to do some validations then use the code in your onclicklistener of button: 如果您想进行一些验证,请使用onclicklistener按钮中的代码:

Button btn1 = (Button) findViewById(R.id.btnopt1_a);
btn1.setOnClickListener(new OnClickListener() { 
    public void onClick(View v)  {
        Toast.makeText(getBaseContext(), "Your answer is correct!" , Toast.LENGTH_SHORT ).show();   
    }
});

Try this, First implements Your Activity OnClickListener . 试试这个,首先implements你的Activity OnClickListener And then each end every click check condition you will get solution. 然后每个结束每次点击检查条件,您将获得解决方案。

public class Deals extends Activity implements OnClickListener {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.yourxml);
        //declare ur buttons here
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.menu: {
                // Do your stuff here
                // call method
                if (isright) {
                    Toast.makeText(getBaseContext(), "Your answer is correct!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getBaseContext(), "Your answer wrong!", Toast.LENGTH_SHORT ).show();
                }
            }
        }
    }
}

If you have many conditions in which you have to show toast. 如果您有许多条件需要展示吐司。 Then it would be better to make a method which shows it. 然后最好制作一个显示它的方法。

private void showToast(String msg) {
    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}

And call the above method in the onClick() or wherever you want to show. 并在onClick()或您想要显示的任何位置调用上述方法。

 btn1.setOnClickListener(new OnClickListener() {
   public void onClick(View v)  {
       ..................
       ..........................

       case R.id.btnopt1_a:  
           if (btn1.isPressed()) {
               showToast("Your answer is correct!");
           } else {
               showToast("Your answer is wrong!, The correct answer is: Frog");
           }

           break;
       });

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

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