设置 android 按钮不可见,但仍附加 onClick 侦听器
因此,目前我正在我的应用程序中放入一个复活节彩蛋,并且我希望按钮不可见,但在单击时(Rick roll)。到目前为止,当我说:
Button.setVisibility(view.VISIBLE);
Button.setBackgroundColor(Color.TRANSPARENT);
然后是我的 onClickListener
时,我可以让它工作。唯一的问题是我必须在 Button
上添加文本才能使其可点击。当我取出文本并使其完全不可见时,由于某种原因永远不会调用 onClickListener
?
这是我的 OnClickListener
wonderWhatThisDoes.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
mMediaPlayer = MediaPlayer.create(About.this, R.raw.surprise);
mMediaPlayer.start();
Context context = getApplicationContext();
CharSequence text = "Congrats on finding our easter egg! Enjoy... :]";
Toast toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
toast.show();
}
});
So currently I'm putting an Easter egg inside my app and I want the Button
to be invisible, but when clicked(Rick roll). So far I can make it work when I say:
Button.setVisibility(view.VISIBLE);
Button.setBackgroundColor(Color.TRANSPARENT);
and then my onClickListener
. The only problem with this is that I have to have text on the Button
for it to be clickable. When I take the text out and make it completely invisible then the onClickListener
is never called for some reason?
Here is my OnClickListener
wonderWhatThisDoes.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
mMediaPlayer = MediaPlayer.create(About.this, R.raw.surprise);
mMediaPlayer.start();
Context context = getApplicationContext();
CharSequence text = "Congrats on finding our easter egg! Enjoy... :]";
Toast toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
toast.show();
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
在布局中,使按钮具有特定的宽度,例如
android:layout_width="40dp"
。如果您的宽度设置为带有透明背景且无文本的
wrap_content
,Android 会将该视图测量为宽度为 0dp。你永远无法点击它。In your layout, make your button have a specific width, like
android:layout_width="40dp"
.If your width is set to
wrap_content
with a transparent background and no text, Android will measure that view as having a width of 0dp. You'll never be able to click on that.简单的答案是像这样将 alpha 设置为 0。
它将是不可见的并且 onclick 将起作用。
Simple answer is set alpha to 0 like this.
It will be invisible and onclick will work.
尝试在按钮“”中添加文本...
try making the text in the button " "...
您可以创建任何可单击的视图,例如 LinearLayout。创建一个与按钮尺寸相同的 LinearLayout 并将其 onClick 侦听器设置为处理该事件的任何内容。由于它本质上是不可见的,因此它应该具有相同的效果。
You can create any view, such as LinearLayout, as clickable. Make a LinearLayout with the same dimensions as the button and set it's onClick listener to whatever handles the event. Since it inherently isn't visible, it should hold the same effect.
确保按钮的宽度和高度未设置为
wrap_content
,因为如果文本为“”,这会导致按钮变得非常小。如果这不起作用,您还可以尝试将 onClick() 替换为 onTouch():Make sure that your button's width and height are not set to
wrap_content
because that would cause the button to be extremely small if the text is " ". If that doesn't work, you could also try replacing onClick() with onTouch():不要使用按钮并覆盖 Activity 的dispatchTouchEvent 并以这种方式处理它。
Don't use a button and override your Activity's dispatchTouchEvent and handle it that way.
您可以将
OnClickListener
添加到任何View
,因此请尝试创建一个带有透明图像的ImageView
并将侦听器附加到它。You can add an
OnClickListener
to anyView
, so try creating anImageView
with a transparent image and attach your listener to that.此代码按钮是不可见的,但它有效;))
to this code button is invisible but it worked ;))
您还可以禁用该按钮(它将不可点击)。
在 java 代码中:
在 .xml 布局中:
You can also disable the button (It will not be clickable).
In java code:
In .xml layout:
这对我来说很有效:
btn.setClickable(false)
onGONE
可见性。btn.setClickable(true)
启用VISIBLE
可见性。This works properly for me:
btn.setClickable(false)
onGONE
visibility.btn.setClickable(true)
onVISIBLE
visibility.