我有十个切换按钮。单击主页按钮时,我想保存其中五个按钮的状态。但我只想在用户更改任何按钮的状态时才保存它。有没有办法在不使用 setOnClickListener() 的情况下知道状态的变化?
8 回答
我做了以下,它不是很好,但它的工作原理:
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);
有一种简单的方法可以知道用户是否单击了复合按钮(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)
}
}
}
使用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);
但是有很多方法可以真正实现这一点。因此,您可以制作另一种更适合您需要的方法。
使用自定义视图
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
请注意,我使用的是扩展复合按钮的开关,您可以在任何其他(复选框,...)上使用基本相同的代码
对于 kotlin,请使用此扩展:
fun CompoundButton.setOnUserCheckedChangeListener(callback: (isChecked: Boolean) -> Unit) {
setOnCheckedChangeListener { buttonView, isChecked ->
if (buttonView.isPressed) {
callback.invoke(isChecked)
}
}
}
首先检查这个链接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);
}
}
}
};
}
使用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();
}
}
});
}
}
}
我会推荐这个解决方案:https ://stackoverflow.com/a/9130007/2708288
简而言之,使用“ boolean CheckBox#isShown() ”来确定哪种情况,即使您在活动的 onStart() 或 onResume() 中修改检查状态,它也会返回 false。(我还没有测试过 Fragment 场景。)