如何知道用户是否更改了切换按钮的状态?

时间:2011-08-25 08:32:04

标签: android android-togglebutton

我有十个切换按钮。我想在单击主页按钮时保存其中五个按钮的状态。但我想保存它只有当用户对任何按钮的状态进行了更改时。有没有办法在不使用setOnClickListener()的情况下知道状态的变化?

8 个答案:

答案 0 :(得分:30)

我已经完成了以下操作,它不太好,但它确实有效:

ttsButton = (ToggleButton) findViewById(R.id.solution_ttsbutton);
ttsButton.setOnCheckedChangeListener(toggleButtonChangeListener);
...
// gets called, if the button state changes
final CompoundButton.OnCheckedChangeListener toggleButtonChangeListener = new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // The user changed the button, do something
    }
};

如果我想以编程方式更改按钮而不执行更改侦听器,则执行以下操作:

ttsButton.setOnCheckedChangeListener(null);
ttsButton.setChecked(false);
ttsButton.setOnCheckedChangeListener(toggleButtonChangeListener);

答案 1 :(得分:15)

使用CompoundButton.OnCheckedChangeListener类。

ToggleButton button = /* ... */;
button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // Save the state here
    }
});

编辑:如果您想使用单个侦听器:

CompoundButton.OnCheckedChangeListener listener =
        new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        String key = null;
        switch(buttonView.getId()) {
            case R.id.button1:
                key = "key1";
                break;
            case R.id.button2:
                key = "key2";
                break;
            default:
                return;
        }
        // Save the state here using key
    }
});

ToggleButton button1 = /* ... */;    
button1.setOnCheckedChangeListener(listener);

ToggleButton button2 = /* ... */;
button2.setOnCheckedChangeListener(listener);

但是有很多方法可以实现这一点。因此,您可以构建另一种更适合您需求的方法。

答案 2 :(得分:9)

有一种简单的方法可以知道用户是否点击了CompoundButton(CheckBox,Switch,RadioButton,ToggleButton),使用以下内容:

new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { if (compoundButton.isPressed()) { // do something related to user click/tap } else { // do something related to programmatically state changes (checked/unchecked) } } }

答案 3 :(得分:1)

使用自定义视图

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Switch;

public class CustomSwitch extends Switch {


    private OnCheckedChangeListener mListener;

    public CustomSwitch(Context context) {
        super(context);
    }

    public CustomSwitch(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomSwitch(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CustomSwitch(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        // Do not call supper method
        mListener = listener;
    }

    @Override
    public void setChecked(boolean checked) {
        super.setChecked(checked);

        if (mListener != null) {
            mListener.onCheckedChanged(this, checked);
        }
    }

    public void setCheckedProgrammatically(boolean checked) {
        // You can call super method, it doesn't have a listener... he he :)
        super.setChecked(checked);
    }

}

示例XML用法

<com.your.package.CustomSwitch
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

现在的想法是在代码中调用方法 setCheckedProgrammatically 。当用户更改了合并按钮的状态时,Android会调用 setChecked

请注意,我正在使用一个扩展了compund按钮的开关,你可以在任何其他代码上使用基本相同的代码(复选框,...)

答案 4 :(得分:0)

首先查看此链接http://developer.android.com/resources/tutorials/views/hello-formstuff.html#ToggleButton

一个简单的onChangeListener将执行:

         public class TestProjectActivity extends Activity {

      ToggleButton one; 
     ToggleButton two;
    ToggleButton three;
     ToggleButton four;

    /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


one = (ToggleButton) findViewById(R.id.toggleButton1);
two = (ToggleButton) findViewById(R.id.toggleButton2);
three = (ToggleButton) findViewById(R.id.toggleButton3);
four = (ToggleButton) findViewById(R.id.toggleButton4);

one.setOnCheckedChangeListener(changeChecker);
two.setOnCheckedChangeListener(changeChecker);
three.setOnCheckedChangeListener(changeChecker);
four.setOnCheckedChangeListener(changeChecker);
      }

      OnCheckedChangeListener changeChecker = new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked){
        if (buttonView == one) {
            two.setChecked(false);
            three.setChecked(false);
            four.setChecked(false);
        }
        if (buttonView == two) {
            one.setChecked(false);
            three.setChecked(false);
            four.setChecked(false);
        }
        if (buttonView == three) {
            two.setChecked(false);
            one.setChecked(false);
            four.setChecked(false);
        }
        if (buttonView == four) {
            two.setChecked(false);
            three.setChecked(false);
            one.setChecked(false);
        }
    }
}
};

}

答案 5 :(得分:0)

使用 View.OnTouchListener 来侦听用户互动。 我在我的适配器类中实现了它。

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<ViewHolder>
implements AccountUtils.OnAccountStateChange ,View.OnTouchListener{

// variable to store whether screen id pressed or not
public static boolean pressed=false;


//If User touches the screen 
//MotionEvent.ACTION_DOWN is triggred
//and when user lifts his hand (takes away from screen)
//MotionEvent.ACTION_UP is triggred
// Here you should the change the value of pressed accordingly
@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:
        pressed = true;// Screen Touched
        break;
        case MotionEvent.ACTION_UP:
        pressed = false;// Screen 
        break;
    }
    return false;
}

static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    final SwitchCompat btnToggle;

    public ViewHolder(final View itemView) {
        super(itemView);

        btnToggle = (SwitchCompat) itemView.findViewById(R.id.btn_toggle);
        btnToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // IF the value in Var pressed is true 
                // only then trigger this event
                if (pressed) {

                    // YOUR METHOD GOES HERE
                    Toast.makeText(context, "Checked Changed",   Toast.LENGTH_SHORT).show();

                }
            }
        });

    }

}

}

答案 6 :(得分:0)

我建议使用此解决方案:https://stackoverflow.com/a/9130007/2708288

简单地说,使用&#34; 布尔CheckBox#isShown()&#34;要确定哪种情况,即使您修改了活动的onStart()或onResume()内的检查状态,它也会返回false。 (我还没有测试过Fragment场景。)

答案 7 :(得分:0)

对于Kotlin,请使用以下扩展名:


fun CompoundButton.setOnUserCheckedChangeListener(callback: (isChecked: Boolean) -> Unit) {
    setOnCheckedChangeListener { buttonView, isChecked ->
        if (buttonView.isPressed) {
            callback.invoke(isChecked)
        }
    }
}