简体   繁体   中英

Toast not appearing on Android Studio

I am trying to make a toast appear when I click a button. I have already set the button's on click to be the 'cli function'. However when I press the button nothing appears. Could you please help me.

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

Don't use onClick on XML, as it's buggy and may not work on all versions:

Buttons onClick Force closes app on Android 4.1 device

Better do it in code:

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
       // code here
   } 
});

In xml,make sure you declared onClick in your Button attribute

android:onClick="cli"

Also, in onCreate , declare your Button id

Button button = (Button) findViewById(R.id.button);

Don't use onClick on XML.

Use OnClickListeners . Here is the code:

Button button = findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
       cli(v);
   } 
});

The above code will help you with detecting clicks on the button. Prefer not using android:onClick in XML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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