Android 动态创建的按钮:setOnClickListener 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1653617/
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-20 03:25:32  来源:igfitidea点击:

Android Dynamically Created Button: setOnClickListener doesn't work

androidbuttononclick

提问by Andrew

The onClicknever fires! Why not? Please help me.

onClick永远不会触发!为什么不?请帮我。

for(int i = 0; i < 12; i++) {
    String title = "Button" + i;
    Button sliderButton = new Button(this);
    sliderButton.setText(title);
    glideMenuTray.addView(sliderButton,100,40);

    sliderButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.d("gm", "Tapped ");
        }
    });
}

回答by Eric Mill

I'm no expert at stuff like this, but it's probably something to do with garbage collection, and the OnClickListeners passing out of scope.

我不是这方面的专家,但这可能与垃圾收集和 OnClickListeners 超出范围有关。

Though I don't think you can use the super-easy approach to onClickListeners that Dimitar mentions, you can probably use the middle approach that the section he links to discusses, even though it's not a new approach. To repeat the example code here, it's:

尽管我不认为您可以使用 Dimitar 提到的超级简单的 onClickListeners 方法,但您可能可以使用他链接到的部分讨论的中间方法,即使它不是一种新方法。在这里重复示例代码,它是:

View.OnClickListener handler = View.OnClickListener() {
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.myButton: // doStuff
                break;
            case R.id.myOtherButton: // doStuff
                break;
        }
    }
}

findViewById(R.id.myButton).setOnClickListener(handler);
findViewById(R.id.myOtherButton).setOnClickListener(handler);

If the only thing distinguishing the buttons is their title text, well, you could use that to distinguish between them in the master onClick method.

如果唯一区分按钮的是它们的标题文本,那么您可以在主 onClick 方法中使用它来区分它们。

回答by J-Rou

Also, not shure, I once had a problem like that on a TextView and it was because I didnt add setClickable(true)

另外,不舒尔,我曾经在 TextView 上遇到过这样的问题,那是因为我没有添加 setClickable(true)

My code was something like

我的代码是这样的

TextView text = new TextView(this);
text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
text.setText("***");
text.setClickable(true);

text.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        //My action
    }
});

myViewGroup.addView(text );

Hope this helps

希望这可以帮助

回答by gardarh

Am I right in assuming that the following line:

我是否正确假设以下行:

glideMenuTray.addView(sliderButton,100,40);

Adds the view to the coords x:100,y:40 onto some View extending ViewGroup?

将视图添加到坐标 x:100,y:40 到一些扩展 ViewGroup 的视图上?

In that case you are stacking 12 buttons on top of each other, only the last Button (labeled Button11) will be visible (and clickable).

在这种情况下,您将 12 个按钮堆叠在一起,只有最后一个按钮(标记为 Button11)是可见的(并且可点击)。

And provided that the question is 3 years old I really hope you already resolved this by now :)

如果这个问题已经有 3 年了,我真的希望你现在已经解决了这个问题:)

回答by joecizac

set the setOnClickListener before adding the view.

在添加视图之前设置 setOnClickListener。

for(int i = 0; i < 12; i++) {
    String title = "Button" + i;
    Button sliderButton = new Button(this);
    sliderButton.setText(title);
    sliderButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.d("gm", "Tapped ");
        }
    glideMenuTray.addView(sliderButton,100,40);
}

回答by Dimitar Dimitrov

If you are using Donut or Eclair, you can use common click listener, registered in your Activity and hooked with your buttons in the layout XML.

如果您使用 Donut 或 Eclair,您可以使用公共点击侦听器,在您的 Activity 中注册并与布局 XML 中的按钮挂钩。

For reference, look here, the category Easier click listeners.

作为参考,请看这里,类别更容易点击侦听器