你可以通过编程方式在 Android 中启用和禁用开关。

huangapple go评论66阅读模式
英文:

How can I programmatically enable and disable a switch in Android?

问题

I would like to programmatically change the enabled state of the Switch control (preferrably using the SwitchCompat class). Changing the state is something that is easy to do in the Layout Editor of Android Studio, but I would like to change it in response to an event in code. I found that I was not able to set the enabled attribute in code.

The closest question and answers that I found on Stackoverflow:
Switch button - disable swipe function

Although the setEnabled() method exists in the Button class (under Inherited Methods > android.view.View), it does not appear to extend to the Switch and SwitchCompat classes. I suspected that it was a decision made by Google devs about accessibility, but I have not been able to find anything online to back this theory up.

英文:

I would like to programmatically change the enabled state of the Switch control (preferrably using the SwitchCompat class). Changing the state is something that is easy to do in the Layout Editor of Android Studio, but I would like to change it in response to an event in code. I found that I was not able to set the enabled attribute in code.

The closest question and answers that I found on Stackoverflow:
Switch button - disable swipe function

Although the setEnabled() method exists in the Button class (under Inherited Methods > android.view.View), it does not appear to extend to the Switch and SwitchCompat classes. I suspected that it was a decision made by Google devs about accessibility, but I have not been able to find anything online to back this theory up.

答案1

得分: 1

Switch mySwitch = findViewById(R.id.mySwitch);

// 检查开关是否已启用
boolean isEnabled = mySwitch.isEnabled();

// 更新开关的启用状态
mySwitch.setClickable(!isEnabled);
mySwitch.setAlpha(isEnabled ? 0.5f : 1.0f);

英文:

Try This

Switch mySwitch = findViewById(R.id.mySwitch);

// Check if the Switch is enabled
boolean isEnabled = mySwitch.isEnabled();

// Update the enable state of the Switch
mySwitch.setClickable(!isEnabled);
mySwitch.setAlpha(isEnabled ? 0.5f : 1.0f);

答案2

得分: 0

"That works for me"

@BindView(R.id.Switch_Availabilty)
Switch SwitchAvailabilty;

if (status.equalsIgnoreCase("Yes")) {
SwitchAvailabilty.setChecked(true);
} else {
SwitchAvailabilty.setChecked(false);
}

If You are still getting Error please describe your error with code

英文:

That works for me

@BindView(R.id.Switch_Availabilty)
Switch SwitchAvailabilty;

if (status.equalsIgnoreCase("Yes")) {
   SwitchAvailabilty.setChecked(true);
  } else {
     SwitchAvailabilty.setChecked(false);
 }

If You are still getting Error please describe your error with code

答案3

得分: 0

以下是翻译好的部分:

Java:

// 这行代码出现在Activity类的内部或外部
public static Boolean switch1Enabled = true;

// 这个块出现在Activity类的onCreate()函数中
Button button1 = (Button) findViewById(R.id.button1);
Switch switch1 = (Switch) findViewById(R.id.switch1);
// 按钮监听器用于模拟变化事件
button1.setOnClickListener(new View.OnClickListener()){
    public void onClick(View v){
        switch1Enabled = !switch1Enabled;
        switch1.setClickable(switch1Enabled);
        switch1.setAlpha(switch1Enabled ? 1.0f : 0.5f);
    }
}

Kotlin:

// 这行代码出现在Activity类的内部或外部
var switch1Enabled: Boolean = true

// 这个块出现在Activity类的onCreate()函数中
val button1: Button = findViewById(R.id.button1)
val switch1: Switch = findViewById(R.id.switch1)
// 按钮监听器用于模拟变化事件
button1.setOnClickListener{
    switch1Enabled = !switch1Enabled
    switch1.isClickable = switch1Enabled
    switch1.alpha = if (switch1Enabled) 1.0f else 0.5f
}

布尔值应该设置为开关控件的初始启用状态。在这个示例中,控件在布局编辑器中已经启用,因此switch1Enabled变量被设置为true

我仍然困惑于为什么在代码中无法使用enabled属性来控制此控件。谁能回答为什么enabled属性在这个控件中不可用,我会给予点赞。

英文:

After reading one of Yogesh's answers, I have refined it to include the ability to toggle the enabled state. The following examples assumes that one button and one switch has been placed on an activity.

Java:

// This line appears either just inside the Activity class, or outside of it
public static Boolean switch1Enabled = true;

// This block appears in the onCreate() function of the Activity class
Button button1 = (Button) findViewById(R.id.button1);
Switch switch1 = (Switch) findViewById(R.id.switch1);
// Button listener used to simulate change event
button1.setOnClickListener(new View.OnClickListener()){
    public void onClick(View v){
        switch1Enabled = !switch1Enabled;
        switch1.isClickable = switch1Enabled;
        switch1.alpha = (switch1Enabled ? 1.0f : 0.5f);
    }
}

Kotlin:

// This line appears either just inside the Activity class, or outside of it
var switch1Enabled: Boolean = true

// This block appears in the onCreate() function of the Activity class
val button1: Button = findViewById(R.id.button1)
val switch1: Switch = findViewById(R.id.switch1)
// Button listener used to simulate change event
button1.setOnClickListener{
    switch1Enabled = !switch1Enabled
    switch1.isClickable = switch1Enabled
    switch1.alpha = if (switch1Enabled) 1.0f else 0.5f
}

The boolean value should be set to the initial enabled state of the switch control. In this example, the control is already enabled in the layout editor, so the switch1Enabled variable is set to true.

I am still perplexed as to why the enabled attribute is not available with this control in code. I will up-vote whoever can answer why.

huangapple
  • 本文由 发表于 2023年7月6日 17:05:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定