[英]Button not calling OnClickListener
I've just created a ScrollView
that has a LinearLayout
inside. 我刚刚创建了一个内部包含LinearLayout
的ScrollView
。 This LinearLayout
has a button on the top, and there is nothing I can do. 这个LinearLayout
顶部有一个按钮,我无能为力。 The button's onClickListener is never called. 永远不会调用按钮的onClickListener。
I want to detect when a user presses the LinearLayout
and when presses the Button. 我想检测用户何时按下LinearLayout
并按下Button。
Here is my code: 这是我的代码:
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(layout);
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layoutClick(v);
}
});
LinearLayout.LayoutParams headerParam = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,getDpFromPixel(50));
//bottone per l'header
Button headerButton = new Button(this);
headerButton.setId(findId());
headerButton.setLayoutParams(headerParam);
//headerButton.setBackground(getResources().getColor(R.color.button_backgroud));
//headerButton.setBackground(getResources().getColor(R.color.button_backgroud));
Drawable d = getResources().getDrawable(R.color.button_backgroud);
headerButton.setBackground(d);
headerButton.setText(title);
headerButton.setTextColor(getResources().getColor(R.color.white));
headerButton.setGravity(Gravity.CENTER_VERTICAL);
headerButton.setPadding(70, 0, 0, 0);
headerButton.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
headerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonPressed(v);
System.out.println("button pressed!!");
}
});
layout.addView(headerButton);
The "layoutClick" function is working, the headerButton is never calling the "buttonPressed" function or printing to console.. “layoutClick”函数正常工作,headerButton永远不会调用“buttonPressed”函数或打印到控制台。
Try making focusable the button 尝试使按钮可聚焦
headerButton.setFocusable(true);
headerButton.setClickable(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.