如何在android中设置开关小部件的默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12250021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:11:23  来源:igfitidea点击:

How to set the default value of the switch widget in android

androidxml

提问by Ricky Zheng

I am coding my xml file with the couple switch widgets. but I don't know how to set the default value of them as ON. And I just want to get this problem solved within the xml file so no Java.

我正在使用一对开关小部件编写我的 xml 文件。但我不知道如何将它们的默认值设置为 ON。我只想在 xml 文件中解决这个问题,所以没有 Java。

回答by Marcin Orlowski

To make Switch set "ON" in you XML file use this:

要在 XML 文件中将 Switch 设置为“ON”,请使用以下命令:

android:checked="true" 

回答by shellym

To set the Switch default as ON in XML file :

在 XML 文件中将 Switch 默认设置为 ON:

android:checked = "true"

Don't try using android:pressed = "true" . It will throw an error while running the app.

不要尝试使用 android:pressed = "true" 。运行应用程序时会抛出错误。