setting android button invisible but still having

2020-05-27 08:37发布

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();
    }
});

10条回答
何必那么认真
2楼-- · 2020-05-27 09:05

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

查看更多
太酷不给撩
3楼-- · 2020-05-27 09:10

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.

查看更多
4楼-- · 2020-05-27 09:11

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

In java code:

btn.setClickable(false);

In .xml layout:

android:clickable="false"
查看更多
Lonely孤独者°
5楼-- · 2020-05-27 09:14
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 ;))

查看更多
再贱就再见
6楼-- · 2020-05-27 09:20

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

myButton.setText("    ");
查看更多
干净又极端
7楼-- · 2020-05-27 09:20

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.

查看更多
登录 后发表回答