【问题标题】:I need to make Toggle Switches for Java [closed]我需要为 Java 制作切换开关 [关闭]
【发布时间】:2018-08-17 09:48:24
【问题描述】:

大家好,有人可以告诉我如何在 Java 中制作一些拨动开关吗?具体来说,三分之二的开关会在一个打开时关闭?

LIKE THIS

【问题讨论】:

标签: java android switch-statement toggle radio


【解决方案1】:

假设您有swt1swt2

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;

public class MainActivity extends AppCompatActivity {

    private Switch swt1;
    private Switch swt2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        swt1 = (Switch)findViewById(R.id.swt1);
        swt2 = (Switch)findViewById(R.id.swt2);


        swt1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if(b == true)
                    swt2.setChecked(false);
                else
                    swt2.setChecked(true);
            }
        });

        swt2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if(b == true)
                    swt1.setChecked(false);
                else
                    swt1.setChecked(true);
            }
        });
    }
}

您可以根据需要添加任意数量的开关。只需使用switch.setChecked(true) 打开开关,使用switch.setChecked(false) 关闭开关。

请务必提出任何问题!

【讨论】:

  • 非常感谢你终于成功了:)
猜你喜欢
  • 1970-01-01
  • 2014-11-26
  • 2023-01-31
  • 2022-01-05
  • 2020-09-23
  • 2014-10-25
  • 1970-01-01
  • 2013-01-10
  • 2019-11-30
相关资源
最近更新 更多