Example on ToggleButton

后端 未结 7 1294
悲哀的现实
悲哀的现实 2021-01-12 07:08

I am developing an application using a toggle button, I entered 1 or 0 in EditText. When button is clicked, the toggle button has to change if I enter 1 the tog

相关标签:
7条回答
  • 2021-01-12 07:17

    Try this Toggle Buttons

    test_activity.xml

    <ToggleButton 
    android:id="@+id/togglebutton" 
    android:layout_width="100px" 
    android:layout_height="50px" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true"
    android:onClick="toggleclick"/>
    

    Test.java

    public class Test extends Activity {
    
    private ToggleButton togglebutton;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        togglebutton = (ToggleButton) findViewById(R.id.togglebutton);
    }
    
    public void toggleclick(View v){
        if(togglebutton.isChecked())
            Toast.makeText(TestActivity.this, "ON", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(TestActivity.this, "OFF", Toast.LENGTH_SHORT).show();
        }
    }
    
    0 讨论(0)
  • 2021-01-12 07:19

    Just remove the line toggle.toggle(); from your click listener toggle() method will always reset your toggle button value.

    And as you are trying to take the value of EditText in string variable which always remains same as you are getting value in onCreate() so better directly use the EditText to get the value of it in your onClick listener.

    Just change your code as below its working fine now.

      btn.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                           //toggle.toggle();
                if ( ed.getText().toString().equalsIgnoreCase("1")) {
    
                    toggle.setTextOff("TOGGLE ON");
                    toggle.setChecked(true);
                } else if ( ed.getText().toString().equalsIgnoreCase("0")) {
    
                    toggle.setTextOn("TOGGLE OFF");
                    toggle.setChecked(false);
    
                }
            }
        });
    
    0 讨论(0)
  • 2021-01-12 07:20
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        editString = ed.getText().toString();
        if(editString.equals("1")){
    
            toggle.setTextOff("TOGGLE ON");
            toggle.setChecked(true);
    
        }
        else if(editString.equals("0")){
    
            toggle.setTextOn("TOGGLE OFF");
            toggle.setChecked(false);
    
        }
    }
    
    0 讨论(0)
  • 2021-01-12 07:24

    You should follow the Google guide;

    ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                // The toggle is enabled
            } else {
                // The toggle is disabled
            }
        }
    });
    

    You can check the documentation here

    0 讨论(0)
  • 2021-01-12 07:30

    I think what are attempting is semantically same as a radio button when 1 is when one of the options is selected and 0 is the other option.

    I suggest using the radio button provided by Android by default.

    Here is how to use it- http://www.mkyong.com/android/android-radio-buttons-example/

    and the android documentation is here-

    http://developer.android.com/guide/topics/ui/controls/radiobutton.html

    Thanks.

    0 讨论(0)
  • 2021-01-12 07:31
    <ToggleButton 
        android:id="@+id/togglebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="Vibrate on"
        android:textOff="Vibrate off"
        android:onClick="onToggleClicked"/>
    

    Within the Activity that hosts this layout, the following method handles the click event:

    public void onToggleClicked(View view) {
        // Is the toggle on?
        boolean on = ((ToggleButton) view).isChecked();
    
        if (on) {
            // Enable vibrate
        } else {
            // Disable vibrate
        }
    }
    
    0 讨论(0)
提交回复
热议问题