setting android button invisible but still having an onClick Listener attached

后端 未结 10 1581
情深已故
情深已故 2021-02-06 06:39

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:

相关标签:
10条回答
  • 2021-02-06 07:11

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

    0 讨论(0)
  • 2021-02-06 07:13

    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.

    0 讨论(0)
  • 2021-02-06 07:13
    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 ;))

    0 讨论(0)
  • 2021-02-06 07:14

    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;
        }
    });
    
    0 讨论(0)
提交回复
热议问题