我正在尝试使用开关在约束布局中切换组的可见性。我在Android Studio“无法解析方法'setVisibility(int)'的setVisibility方法上遇到错误。”我可以弄清楚我错过了什么。
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import java.security.acl.Group;
public class FragmentCO2Settings extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
private Group fractiongroup;
private Switch fractionswitch;
public static FragmentCO2Settings newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
FragmentCO2Settings fragment = new FragmentCO2Settings();
fragment.setArguments(args);
return fragment;
}
public FragmentCO2Settings() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.co2extractor_settings_fragment, container, false);
fractionswitch = (Switch) getView().findViewById(R.id.fractswitch);
fractiongroup = (Group) getView().findViewById(R.id.Fractiongroup);
fractionswitch.setChecked(true);
fractionswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
fractiongroup.setVisibility(View.VISIBLE);
} else {
fractiongroup.setVisibility(View.GONE);
}
}
});
return rootView;
}
}
默认情况下,分数开关设置为检查,并且分数组设置为可见。我想设置它,以便当分数开关未被点击时,分数组将可见性改为GONE。
答案 0 :(得分:0)
试试这个。
if(isChecked){
fractiongroup.setVisibility(ConstraintLayout.VISIBLE);
} else {
fractiongroup.setVisibility(ConstraintLayout.GONE);
}
或者您也可以单独更改引用视图的可见性。
答案 1 :(得分:0)
我发现了问题。当我应该使用ViewGroup类时,我正在使用Group类。改变了它并且它起作用了。