简体   繁体   English

两次单击按钮后显示Toast

[英]Toast is showing after clicking the button two times

I am creating android app using eclipce , I used a click listener for a button to show a toast .Every thing is working ok but the problem that I need to click two times in the button to show the toast .Is there any way to force one clicking to show the toast? 我正在使用eclipce创建android应用,我使用了按钮的单击侦听器来显示祝酒词。一切正常,但是我需要在按钮中单击两次以显示祝酒词的问题。是否有任何强制方法一键显示烤面包? This is the code that use 这是使用的代码

 public void showAnswer(View view) {

        Button b;
        b= findViewById(R.id.ans);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplication().getBaseContext(),"ال�يل",Toast.LENGTH_SHORT).show();
            }
        });

    }

Use only this if you are assigning showAnswer(View view) to your button onclick event: 如果showAnswer(View view)分配给button onclick事件,请仅使用此方法:

public void showAnswer(View view) {

     Toast.makeText(getApplication().getBaseContext(),"ال�يل",Toast.LENGTH_SHORT).show();

    }

It costs you 2 taps because with the first one, you define your listener and the second one, your listener is called. 这需要花费您2次点击,因为在第一次点击中,您定义了监听器,而在第二次点击中,将调用您的监听器。

You have to define your ClickListener only one time. 您只需定义一次ClickListener Generally, it's done in the onCreate() method. 通常,它是在onCreate()方法中完成的。

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

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