2014-12-05 43 views
1

我在我的应用程序中使用了CompoundButton。我在CompoundButton.OnCheckedChangeListeneronCheckedChange()回调中调用名为newsLetterUpdate()的方法。我以编程方式更改了此CompoundButton的选中状态,但onCheckedChange()回调被调用。我希望只有在View上切换选中状态时才会触发onCheckedChange()。 请给我一个解决方案,以改变复合按钮的状态,而不需要调用onCheckedChange()回调。如何在不影响onCheckedChangeListener中的onCheckedChanged()的情况下以编程方式打开/关闭计算按钮

+0

取一个标志,并且当你以编程方式改变状态复合按钮时设置为false,并在onCheckedChanged()基础上检查这个标志值。 – 2014-12-05 06:43:00

回答

0

方法1:如果你想在checkChangeListener同时使用与视图进行交互,然后implemenet onClickListenernewsLetterUpdate方法在该类

Mehtod 2只应调用:注销监听器之前,你programmaticaly改变复选框并在您完成后再次注册

方法3:使用布尔标志...设置它当您更改复选框时编程并设置为false如果您完成... guard th e代码与该标志,你想执行用户交互只

+0

它应该在checkChangeListener中使用newsLetterUpdate()方法。然后?? – 2014-12-05 07:44:03

+0

在你编程之前取消注册监听器复选框并在完成后再次注册..使用布尔型标志...当你更改复选框时进行编程设置,并且设置是否为false,该标志,你不想执行用户交互只有 – 2014-12-05 07:51:20

2

因此,根据您的要求,我相信你只需要执行onCheckedChange()回调中的代码,只有当用户启动的行动。对?

那你为什么要用onCheckedChange(),你可以用onClickListener()来代替你的目标。

目前你可能写你的代码中:

compundbutton.setOnCheckedChangeListener(your_current_listener);

此举代码从那里到:

compundbutton.setOnClickListener(your_new_listener);

然后onCheckedChange()听众将不会在设置选中状态programiccally调用。

compundbutton.setChecked(checked);

希望它可以帮助你..! :)

+0

我觉得,取消注册和注册监听器,这种方法更有效率。谢谢 – 2014-12-31 07:09:02

0

方法4:使用自定义视图

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代码。 setChecked得到由Android当用户改变复方按钮

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

1

我有类似的问题。我有RadioGroup,当用户打开应用程序时,我设置了初始状态,并且它在OnCheckedChangeListener中进行,在我的情况下是更新数据库中非常糟糕的数据。我已经解决了它,在OnCheckedChangedListener我正在按钮,并在该方法中设置一个OnClickListener:

viberPermissionView.getAppPermissions().setOnCheckedChangeListener(
     new RadioGroup.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(RadioGroup radioGroup, 
      int checkedRadioButtonId) { 
       final RadioButton checkedButton =  
      viberPermissionView.findViewById(checkedRadioButtonId); 
       checkedButton.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         startService(checkedButton, 
          App.VIBER); 
        } 
       }); 
      } 
     }); 

随着那的onCreate被称为它打算在OnCheckedChangedListener时,但它不是在OnClickListener去。只有当用户按下按钮时,它才会进入OnClick,并在我的情况下执行startService。希望这会有所帮助。

相关问题