如何在OnCheckedChangeListener.onCheckedChanged中执行其他代码之前显示复选框的更改状态

时间:2013-07-22 08:49:14

标签: android

点击复选框后我需要添加新视图。我想要的是显示已更改的复选框状态,然后才显示新视图,但现在当我单击复选框时,ui会停止一秒或几秒,然后同时显示新视图和复选框显示为已选中。< / p>

我的代码很简单

{

checkbox = (CheckBox) findViewById(R.id.select_param_value_checkbox);

if (checkbox != null) {

    checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            onCheckboxChanged(isChecked);

        }

    });

}
}

protected void onCheckboxChanged(boolean isChecked) {
    //this listener is adding in constructor and creates in view where i want to add child
    on_change_listener.onFilterParamValueChanged(this);
}

该侦听器的代码是:

public void onFilterParamValueChanged(MyClass obj) {

    addView(new MyView(getContext(), obj.getTitle()));
}
MyView的代码:

class MyView extends LinearLayout{

    private TextView title;

    public MyView(Context context, String text) {
        super(context);
        ViewGroup.inflate(context, R.layout.my_view, this);
        title = (TextView) findViewById(R.id.selected_filter_param_value_title);
        if (title != null) {
            title.setText(filter_value_data.toString());
        }
    }

}

UPD 感谢@Diljeet,以下评论对我有用。我把addView放在Runnable

public void onFilterParamValueChanged(MyClass obj) {
    new Handler().post(new Runnable(){
        public void run() {
            addView(new MyView(getContext(), obj.getTitle()));
        }
    });

}    

0 个答案:

没有答案