简体   繁体   中英

How to prevent user input?

My activity_main.xml 's interface is shared by MainActivity.java (which contains the actual code for buttons), and SecondActivity.java .

The problem is , It would let the user play around with the buttons, when they are in MainActivity , but If they are in SecondActivity , and they were to press a button, It would crash my app, because It doesn't have the code for the buttons.

So how do I prevent user clicking the screen in SecondActivity ?

To disable button presses and preventing an OnClick event to fire you can use the following code on your respective buttons

((Button) findViewById(R.id.your_button_id)).setEnabled(false);

The code simply disables the button, to activate it again, you can use the same syntax, but replace the boolean parameter with true

((Button) findViewById(R.id.your_button_id)).setEnabled(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM