设置 android 按钮不可见,但仍附加 onClick 侦听器

发布于 2024-11-26 14:05:30 字数 855 浏览 3 评论 0原文

因此,目前我正在我的应用程序中放入一个复活节彩蛋,并且我希望按钮不可见,但在单击时(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

手心的海 2024-12-03 14:05:30

在布局中,使按钮具有特定的宽度,例如 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.

暮年慕年 2024-12-03 14:05:30

简单的答案是像这样将 alpha 设置为 0。

 <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:alpha="0"
                android:clickable="true"
                android:onClick="getAllImages"
                android:visibility="visible" />

它将是不可见的并且 onclick 将起作用。

Simple answer is set alpha to 0 like this.

 <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:alpha="0"
                android:clickable="true"
                android:onClick="getAllImages"
                android:visibility="visible" />

It will be invisible and onclick will work.

×纯※雪 2024-12-03 14:05:30

尝试在按钮“”中添加文本...

myButton.setText("    ");

try making the text in the button " "...

myButton.setText("    ");
山田美奈子 2024-12-03 14:05:30

您可以创建任何可单击的视图,例如 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.

倾城月光淡如水﹏ 2024-12-03 14:05:30

确保按钮的宽度和高度未设置为 wrap_content,因为如果文本为“”,这会导致按钮变得非常小。如果这不起作用,您还可以尝试将 onClick() 替换为 onTouch():

button1.setOnTouchListener(new OnTouchListener()
{
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        // TODO Auto-generated method stub
        return false;
    }
});

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():

button1.setOnTouchListener(new OnTouchListener()
{
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        // TODO Auto-generated method stub
        return false;
    }
});
忱杏 2024-12-03 14:05:30

不要使用按钮并覆盖 Activity 的dispatchTouchEvent 并以这种方式处理它。

Don't use a button and override your Activity's dispatchTouchEvent and handle it that way.

沙与沫 2024-12-03 14:05:30

您可以将 OnClickListener 添加到任何 View,因此请尝试创建一个带有透明图像的 ImageView 并将侦听器附加到它。

You can add an OnClickListener to any View, so try creating an ImageView with a transparent image and attach your listener to that.

以往的大感动 2024-12-03 14:05:30
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
     Button b = (Button) findViewById(R.id.button1);
     final CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);
     b.setBackgroundColor(Color.TRANSPARENT);

     b.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            cb.setChecked(true);

此代码按钮是不可见的,但它有效;))

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
     Button b = (Button) findViewById(R.id.button1);
     final CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);
     b.setBackgroundColor(Color.TRANSPARENT);

     b.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            cb.setChecked(true);

to this code button is invisible but it worked ;))

春夜浅 2024-12-03 14:05:30

您还可以禁用该按钮(它将不可点击)。

在 java 代码中:

btn.setClickable(false);

在 .xml 布局中:

android:clickable="false"

You can also disable the button (It will not be clickable).

In java code:

btn.setClickable(false);

In .xml layout:

android:clickable="false"
深海少女心 2024-12-03 14:05:30

这对我来说很有效:

  • btn.setClickable(false) on GONE 可见性。
  • btn.setClickable(true) 启用 VISIBLE 可见性。

This works properly for me:

  • btn.setClickable(false) on GONE visibility.
  • btn.setClickable(true) on VISIBLE visibility.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文