Java android:按钮 onClick(),无法判断是否发生任何事情

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3636852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 03:01:11  来源:igfitidea点击:

android: button onClick(), cant tell if anything is happening

javaandroideclipseonclick

提问by JoshuaBen

My Activity onClick() below doesn't appear to be doing anything (not seeing any string appear), yet I dont get any errors. What am I missing? Is there a way to trace the function?

我下面的 Activity onClick() 似乎没有做任何事情(没有看到任何字符串出现),但我没有收到任何错误。我错过了什么?有没有办法跟踪功能?

package com.HelloTabWidget2;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class AlbumsActivity extends Activity {

    private Button closeButton;

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.tab1);
        this.closeButton = (Button)this.findViewById(R.id.button);
        this.closeButton.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
           Toast.makeText(AlbumsActivity.this, "You clicked the button", Toast.LENGTH_SHORT).show();

          }
        });


    }
}

Thanks!

谢谢!

回答by Travis

I believe the issue is with the anonymous method, I get an error when trying to use your code. Just add the impliments OnClickListenter.

我相信问题出在匿名方法上,尝试使用您的代码时出现错误。只需添加实现 OnClickListenter。

If you have more than one button, you'll need to add a switch or something on v.getId().

如果您有多个按钮,则需要在 v.getId() 上添加一个开关或其他内容。

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class AlbumsActivity extends Activity implements OnClickListener {
    private Button closeButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        this.closeButton = (Button)this.findViewById(R.id.button);
        this.closeButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
         Toast.makeText(this, "You clicked the button", Toast.LENGTH_SHORT).show();

    }
}

回答by Tsunaze

I would say that instead of writing the below code

我会说,而不是编写下面的代码

...setOnClickListener(new OnClickListener() {..."

I would prefer the following ocde

我更喜欢以下 ocde

"...setOnClickListener(new View.OnClickListener() {..."

回答by JibW

You can simply use following code

您可以简单地使用以下代码

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

    closeButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Onclick works", Toast.LENGTH_LONG).show();

        }
    });

If it provides exception, go and do Project -> Clean. Things will work properly

如果它提供异常,请执行 Project -> Clean。事情会正常进行