【问题标题】:how to change state of a Switch programmatically [Android]如何以编程方式更改 Switch 的状态 [Android]
【发布时间】:2019-01-29 16:05:18
【问题描述】:

我必须切换 A 和 B。我希望每当我点击开关 A 时,它都会改变 B 的状态,反之亦然。 如何在 Android Studio 中做到这一点?

这是我的开关代码

    //first switch
        flw_Rate_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {


            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {

}...


//2nd switch
        lqd_followed_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {


}...

【问题讨论】:

    标签: android android-studio android-widget


    【解决方案1】:

    首先你需要定义你的开关:

    Switch a; // define your switch
    Switch b;
    

    开关可以在点击时自行打开或关闭,所以我们需要告诉他们切换另一个:

    a.setOnClickListener(
         new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                   b.setChecked(!b.isChecked);
             }
          });
    b.setOnClickListener(
         new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                   a.setChecked(!a.isChecked);
             }
          });
    

    在onClick方法中,我们告诉开关如果用户点击了它,将另一个开关检查状态设置为反向,也就是说,我们告诉他们另一个是否关闭,打开它,如果它打开,关闭它. setChecked 方法执行此操作。和论点,说反转开关状态。

    【讨论】:

    • 它不工作。我也在android studio中使用SwitchCompat,它的覆盖方法是@onCheckedChanged
    • @tayyaba debian 你能在你的活动中发布你的整个代码吗?
    • @tayyaba debian 谢谢,但我需要查看您活动中的完整代码以提供更好的帮助,您可以发布吗?
    • 不能添加太多代码。请您仅举例说明您的答案。这将是很大的帮助。
    • 我不知道你在你的代码中做了什么,问题可能是每一件事,除非你得到一个错误。因此,如果您遇到错误,请告诉我。
    【解决方案2】:

    文件-activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.hina.myapplication.MainActivity">
    
    
        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/container" >
    
                <Button
                    android:id="@+id/on1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:text="ON" />
    
                <Button
                    android:id="@+id/on2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_toRightOf="@+id/testbutton"
                    android:text="OFF"/>
            </LinearLayout>
    
            <ListView android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/LstPeriodOptions"
                android:layout_alignParentTop="true"
                android:layout_above="@id/container" />
    
        </RelativeLayout>
    
    </android.support.constraint.ConstraintLayout>
    

    文件-MainActivity.java

    package com.example.hina.myapplication;
    import android.app.Activity;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    import static com.example.hina.myapplication.R.id.on2;
    
    public class MainActivity extends Activity implements View.OnClickListener {
        Button on1;
        Button off1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
             on1= (Button) findViewById(R.id.on1);
             off1= (Button) findViewById(on2);
            on1.setOnClickListener(this);
            off1.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            if (on1.isPressed()){
                on1.setEnabled(false);
                off1.setEnabled(true);
            }
            else if (off1.isPressed()){
                off1.setEnabled(false);
                on1.setEnabled(true);
            }
    
        }
    }
    

    【讨论】:

    • 请在您的回答中提供解释。
    猜你喜欢
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多