ToggleButton handling


Last Updated:

  1. v1nsai

    v1nsai Well-Known Member This Topic's Starter

    Joined:
    Mar 24, 2010
    Messages:
    60
    Likes Received:
    3
    **SOLVED**
    corrected code is below


    I'm having a little trouble handling events from my ToggleButton. I can't figure out where to use the setOnClickListener() call and how to set up handlers for the button being turned on and off. Here's what I have, can anyone suggest a fix?


    Code (Text):
    1. package com.v1nsai.dataoff;
    2.  
    3. import android.app.Activity;
    4. import android.content.Intent;
    5. import android.os.Bundle;
    6. import android.os.Debug;
    7. import android.view.View;
    8. import android.view.View.OnClickListener;
    9. import android.widget.Button;
    10. import android.widget.CompoundButton;
    11. import android.widget.Toast;
    12. import android.widget.ToggleButton;
    13.  
    14. public class Settings extends Activity implements OnClickListener
    15. {
    16.     /** Called when the activity is first created. */
    17.     @Override
    18.     public void onCreate(Bundle savedInstanceState)
    19.     {
    20.         super.onCreate(savedInstanceState);
    21.         setContentView(R.layout.settings);
    22.         //ToggleButton wifitoggle = (ToggleButton)findViewById(R.id.wifitoggle);
    23.         //wifitoggle.setOnClickListener(this);
    24.     }
    25.        
    26.     public void onClick( View v )
    27.     {        
    28.         //Debug.startMethodTracing("settings");
    29.         ToggleButton wifitoggle = (ToggleButton)findViewById(R.id.wifitoggle);
    30.         wifitoggle.setOnClickListener(this);
    31.         switch( v.getId() )
    32.         {
    33.             case R.id.wifitoggle:
    34.                 if( wifitoggle.isChecked() == true )
    35.                 {
    36.                     startService(new Intent( this, WifiService.class ) );
    37.                     Toast.makeText( this, "Wifi kill service on", 3 ).show();
    38.                 }
    39.                 if( wifitoggle.isChecked() == false )
    40.                 {
    41.                     stopService( new Intent( this, WifiService.class ) );
    42.                     Toast.makeText( this, "Wifi kill service off", 3 ).show();
    43.                 }
    44.                 break;
    45.                
    46.             default:
    47.                 break;
    48.         }
    49.         //Debug.stopMethodTracing();
    50.     }
    51. }
     

    Advertisement

Share This Page

Loading...