在我的片段中,我有以下代码:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Routes_Checkbox_Pickup.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Routes_EditText_AlarmPickup.setEnabled(isChecked);
Routes_Spinner_Pickup_Routes.setEnabled(isChecked);
Routes_Spinner_Pickup_TrackingLocations.setEnabled(isChecked);
if(isChecked==true)
Routes_EditText_AlarmPickup.requestFocus();
}
});
Routes_Checkbox_Drop.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Routes_EditText_AlarmDrop.setEnabled(isChecked);
Routes_Spinner_Drop_Routes.setEnabled(isChecked);
Routes_Spinner_Drop_TrackingLocations.setEnabled(isChecked);
if(isChecked==true)
Routes_EditText_AlarmDrop.requestFocus();
}
});
}
在按钮上单击我有以下代码:
Routes_Checkbox_Pickup.setChecked(false);
但是上面的代码不会触发上面设置的事件。怎么了?
修改
完整代码:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.routes_fragment, container, false);
Routes_Checkbox_Pickup = (CheckBox) view.findViewById(R.id.Routes_Checkbox_Pickup);
Routes_Spinner_Pickup_Routes = (Spinner) view.findViewById(R.id.Routes_Spinner_Pickup_Routes);
Routes_TextInputLayout_AlarmPickup = (TextInputLayout) view.findViewById(R.id.Routes_TextInputLayout_AlarmPickup);
Routes_EditText_AlarmPickup = (EditText) view.findViewById(R.id.Routes_EditText_AlarmPickup);
Routes_Spinner_Pickup_TrackingLocations = (Spinner) view.findViewById(R.id.Routes_Spinner_Pickup_TrackingLocations);
Routes_Checkbox_Drop = (CheckBox) view.findViewById(R.id.Routes_Checkbox_Drop);
Routes_Spinner_Drop_Routes = (Spinner) view.findViewById(R.id.Routes_Spinner_Drop_Routes);
Routes_TextInputLayout_AlarmDrop = (TextInputLayout) view.findViewById(R.id.Routes_TextInputLayout_AlarmDrop);
Routes_EditText_AlarmDrop = (EditText) view.findViewById(R.id.Routes_EditText_AlarmDrop);
Routes_Spinner_Drop_TrackingLocations = (Spinner) view.findViewById(R.id.Routes_Spinner_Drop_TrackingLocations);
Routes_Button_Save = (Button) view.findViewById(R.id.Routes_Button_Save);
Routes_Button_Save.setBackgroundResource(R.drawable.button_enabled);
Routes_EditText_AlarmPickup.setSingleLine(true);
Routes_EditText_AlarmDrop.setSingleLine(true);
Routes_Checkbox_Pickup.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Routes_EditText_AlarmPickup.setEnabled(isChecked);
Routes_Spinner_Pickup_Routes.setEnabled(isChecked);
Routes_Spinner_Pickup_TrackingLocations.setEnabled(isChecked);
if(isChecked==true)
Routes_EditText_AlarmPickup.requestFocus();
}
});
Routes_Checkbox_Drop.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Routes_EditText_AlarmDrop.setEnabled(isChecked);
Routes_Spinner_Drop_Routes.setEnabled(isChecked);
Routes_Spinner_Drop_TrackingLocations.setEnabled(isChecked);
if(isChecked==true)
Routes_EditText_AlarmDrop.requestFocus();
}
});
}
答案 0 :(得分:0)
我猜测viewHolder不在那里更新UI
在onCreateView()之前添加
ViewHolder holder = new ViewHolder();
然后更改
Routes_Checkbox_Pickup.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
holder.Routes_EditText_AlarmPickup.setEnabled(isChecked);
holder.Routes_Spinner_Pickup_Routes.setEnabled(isChecked);
holder.Routes_Spinner_Pickup_TrackingLocations.setEnabled(isChecked);
if(isChecked==true)
holder.Routes_EditText_AlarmPickup.requestFocus();
}
});
Routes_Checkbox_Drop.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
holder.Routes_EditText_AlarmDrop.setEnabled(isChecked);
holder.Routes_Spinner_Drop_Routes.setEnabled(isChecked);
holder.Routes_Spinner_Drop_TrackingLocations.setEnabled(isChecked);
if(isChecked==true)
holder.Routes_EditText_AlarmDrop.requestFocus();
}
});
还使用holder来启动onCreateView()中的按钮视图