android.widget.Switch - on/off event listener?

前端 未结 11 1476
庸人自扰
庸人自扰 2020-12-02 04:56

I would like to implement a switch button, android.widget.Switch (available from API v.14).



        
相关标签:
11条回答
  • 2020-12-02 05:29

    The layout for Switch widget is something like this.

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:gravity="right"
            android:text="All"
            android:textStyle="bold"
            android:textColor="@color/black"
            android:textSize="20dp"
            android:id="@+id/list_toggle" />
    </LinearLayout>
    

    In the Activity class, you can code by two ways. Depends on the use you can code.

    First Way

    public class ActivityClass extends Activity implements CompoundButton.OnCheckedChangeListener {
    Switch list_toggle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.return_vehicle);
    
        list_toggle=(Switch)findViewById(R.id.list_toggle);
        list_toggle.setOnCheckedChangeListener(this);
        }
    }
    
    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
        if(isChecked) {
            list_toggle.setText("Only Today's");  //To change the text near to switch
            Log.d("You are :", "Checked");
        }
        else {
            list_toggle.setText("All List");   //To change the text near to switch
            Log.d("You are :", " Not Checked");
        }
    }
    

    Second way

    public class ActivityClass extends Activity {
    Switch list_toggle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.return_vehicle);
    
        list_toggle=(Switch)findViewById(R.id.list_toggle);
        list_toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
              if(isChecked) {
                 list_toggle.setText("Only Today's");  //To change the text near to switch
                 Log.d("You are :", "Checked");
              }
              else {
                 list_toggle.setText("All List");  //To change the text near to switch
                 Log.d("You are :", " Not Checked");
              }
           }       
         });
       }
    }
    
    0 讨论(0)
  • 2020-12-02 05:33

    there are two ways,

    1. using xml onclick view Add Switch in XML as below:

      <Switch
      android:id="@+id/switch1"
      android:onClick="toggle"/>
      

    In YourActivity Class (For E.g MainActivity.java)

        Switch toggle; //outside oncreate
        toggle =(Switch) findViewById(R.id.switch1); // inside oncreate
    
        public void toggle(View view) //outside oncreate
        {
            if( toggle.isChecked() ){
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    start.setBackgroundColor(getColor(R.color.gold));
                    stop.setBackgroundColor(getColor(R.color.white));
                }
            }
            else
            {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    stop.setBackgroundColor(getColor(R.color.gold));
                    start.setBackgroundColor(getColor(R.color.white));
                }
            }
        }
    
    1. using on click listener

    Add Switch in XML as below:

    In YourActivity Class (For E.g MainActivity.java)

        Switch toggle; // outside oncreate
        toggle =(Switch) findViewById(R.id.switch1);  // inside oncreate
    
    
        toggle.setOnClickListener(new View.OnClickListener() {   // inside oncreate
            @Override
            public void onClick(View view) {
    
                if( toggle.isChecked() ){
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        start.setBackgroundColor(getColor(R.color.gold));
                    }
                }
                else
                {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        stop.setBackgroundColor(getColor(R.color.gold));
                    }
                }
    
            }
    
        });
    
    0 讨论(0)
  • 2020-12-02 05:33

    In the Activity class

     switch.setOnCheckedChangeListener((CompoundButton.OnCheckedChangeListener) (buttonView, isChecked) -> {
                    if (isChecked){
                        // your code
                    } else {
                        // your code
                    }
                });
    

    layout for Switch widget

       <androidx.appcompat.widget.SwitchCompat
                    android:id="@+id/switch"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:checked="true"
                    android:layoutDirection="ltr"/>
    
    0 讨论(0)
  • 2020-12-02 05:35

    In Kotlin:

            switch_button.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                // The switch enabled
                text_view.text = "Switch on"
    
            } else {
                // The switch disabled
                text_view.text = "Switch off"
    
            }
        }
    
    0 讨论(0)
  • 2020-12-02 05:38

    Use the following snippet to add a Switch to your layout via XML:

    <Switch
         android:id="@+id/on_off_switch"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textOff="OFF"
         android:textOn="ON"/>
    

    Then in your Activity's onCreate method, get a reference to your Switch and set its OnCheckedChangeListener:

    Switch onOffSwitch = (Switch)  findViewById(R.id.on_off_switch); 
    onOffSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Log.v("Switch State=", ""+isChecked);
    }       
    
    });
    
    0 讨论(0)
  • 2020-12-02 05:41

    September 2020 - Programmatically Answer

    You can do it with programmatically for Switch Widget and Material Design:

    Switch yourSwitchButton = findViewById(R.id.switch_id);
    
    yourSwitchButton.setChecked(true); // true is open, false is close.
    
    yourSwitchButton.setOnCheckedChangeListener((compoundButton, b) -> {
            if (b){
              //open job.
            }  
            else  {
              //close job.
            }
        });
    

    @canerkaseler

    0 讨论(0)
提交回复
热议问题