OnCheckedChanged 在 Fragment Android 中不起作用
OnCheckedChanged not working in Fragment Android
您好,在片段中使用 OnCheckedChanged 获取复选框的值,但它不起作用,就像在我选中复选框时没有调用该方法一样。下面是代码是如何实现的。
CheckBox 用法的 XML 文件。
<CheckBox
android:id="@+id/cbDefault"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".2" />
片段的Java代码如下
import android.widget.CompoundButton.OnCheckedChangeListener;
public class ProfileFrag extends AbsFragment implements OnCheckedChangeListener {
private EditText defaultIdText;
private EditText customText;
private EditText randomText;
private TextView phoneText;
private Button setButton;
protected ProfileUpdate profUpdate;
protected String sDefaultId;
protected String sCustomId;
protected String sRandomId;
protected String sPhoneId;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View profView = inflater.inflate(R.layout.intermediate_screen,
container, false);
defaultIdText = (EditText) profView.findViewById(R.id.etDefault);
customText = (EditText) profView.findViewById(R.id.etCustom);
randomText = (EditText) profView.findViewById(R.id.etUname);
phoneText = (EditText) profView.findViewById(R.id.etPhoneNumber);
return profView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
mSharedData = new SharedData(getActivity());
//
// phoneNumber = DeviceIdGen.DevicePhoneNum(getActivity());
phoneText.setText(mSharedData.getPhNum());
randomText.setText(mSharedData.getRandomId());
customText.setText(mSharedData.getCustomId());
defaultIdText.setText(mSharedData.getDefaultId());
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
switch (buttonView.getId()) {
case R.id.cbDefault:
sDefaultId = defaultIdText.getText().toString();
mSharedData.setUniqueid(sDefaultId);
mSharedData.commit();
break;
case R.id.cbCustom:
sCustomId = customText.getText().toString();
mSharedData.setUniqueid(sCustomId);
mSharedData.commit();
break;
case R.id.cbRandom:
mSharedData.setUniqueid(sRandomId);
mSharedData.commit();
default:
break;
}
}
}
将您的onCreateView
方法修改为以下内容并检查文档中的代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View profView = inflater.inflate(R.layout.intermediate_screen,
container, false);
// Initialize your checkbox
Checkbox mChkbx=(Checkbox)profView.findViewById(R.id.cbDefault);
// Assign listener to the checkbox
mChkbx.setOnCheckedChangeListener(this);
defaultIdText = (EditText) profView.findViewById(R.id.etDefault);
customText = (EditText) profView.findViewById(R.id.etCustom);
randomText = (EditText) profView.findViewById(R.id.etUname);
phoneText = (EditText) profView.findViewById(R.id.etPhoneNumber);
return profView;
}
你没有在你的代码中映射你的复选框..尝试这样做..
Checkbox mCb=(Checkbox)profView.findViewById(R.id.cbDefault);
mCb.setOnCheckedChangeListener(this);
必须实现接口OnCheckedChangeListener
for fragment first we need to implements android.widget.CompoundButton.OnCheckedChangeListener see this example
public class TabAdvertiser extends Fragment implements android.widget.CompoundButton.OnCheckedChangeListener {
CheckBox mCbShowPwd;
EditText userName,password;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FacebookSdk.sdkInitialize(getActivity());
View view = inflater.inflate(R.layout.login, container, false);
userName=(EditText)view.findViewById(R.id.input_email);
password=(EditText)view.findViewById(R.id.input_password);
// get the show/hide password Checkbox
mCbShowPwd = (CheckBox)view.findViewById(R.id.cbShowPwd);
mCbShowPwd.setOnCheckedChangeListener(TabAdvertiser.this);
return view;
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// checkbox status is changed from uncheck to checked.
if (!isChecked) {
// show password
password.setTransformationMethod(PasswordTransformationMethod.getInstance());
} else {
// hide password
password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
}
}
您好,在片段中使用 OnCheckedChanged 获取复选框的值,但它不起作用,就像在我选中复选框时没有调用该方法一样。下面是代码是如何实现的。
CheckBox 用法的 XML 文件。
<CheckBox
android:id="@+id/cbDefault"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".2" />
片段的Java代码如下
import android.widget.CompoundButton.OnCheckedChangeListener;
public class ProfileFrag extends AbsFragment implements OnCheckedChangeListener {
private EditText defaultIdText;
private EditText customText;
private EditText randomText;
private TextView phoneText;
private Button setButton;
protected ProfileUpdate profUpdate;
protected String sDefaultId;
protected String sCustomId;
protected String sRandomId;
protected String sPhoneId;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View profView = inflater.inflate(R.layout.intermediate_screen,
container, false);
defaultIdText = (EditText) profView.findViewById(R.id.etDefault);
customText = (EditText) profView.findViewById(R.id.etCustom);
randomText = (EditText) profView.findViewById(R.id.etUname);
phoneText = (EditText) profView.findViewById(R.id.etPhoneNumber);
return profView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
mSharedData = new SharedData(getActivity());
//
// phoneNumber = DeviceIdGen.DevicePhoneNum(getActivity());
phoneText.setText(mSharedData.getPhNum());
randomText.setText(mSharedData.getRandomId());
customText.setText(mSharedData.getCustomId());
defaultIdText.setText(mSharedData.getDefaultId());
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
switch (buttonView.getId()) {
case R.id.cbDefault:
sDefaultId = defaultIdText.getText().toString();
mSharedData.setUniqueid(sDefaultId);
mSharedData.commit();
break;
case R.id.cbCustom:
sCustomId = customText.getText().toString();
mSharedData.setUniqueid(sCustomId);
mSharedData.commit();
break;
case R.id.cbRandom:
mSharedData.setUniqueid(sRandomId);
mSharedData.commit();
default:
break;
}
}
}
将您的onCreateView
方法修改为以下内容并检查文档中的代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View profView = inflater.inflate(R.layout.intermediate_screen,
container, false);
// Initialize your checkbox
Checkbox mChkbx=(Checkbox)profView.findViewById(R.id.cbDefault);
// Assign listener to the checkbox
mChkbx.setOnCheckedChangeListener(this);
defaultIdText = (EditText) profView.findViewById(R.id.etDefault);
customText = (EditText) profView.findViewById(R.id.etCustom);
randomText = (EditText) profView.findViewById(R.id.etUname);
phoneText = (EditText) profView.findViewById(R.id.etPhoneNumber);
return profView;
}
你没有在你的代码中映射你的复选框..尝试这样做..
Checkbox mCb=(Checkbox)profView.findViewById(R.id.cbDefault);
mCb.setOnCheckedChangeListener(this);
必须实现接口OnCheckedChangeListener
for fragment first we need to implements android.widget.CompoundButton.OnCheckedChangeListener see this example
public class TabAdvertiser extends Fragment implements android.widget.CompoundButton.OnCheckedChangeListener {
CheckBox mCbShowPwd;
EditText userName,password;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FacebookSdk.sdkInitialize(getActivity());
View view = inflater.inflate(R.layout.login, container, false);
userName=(EditText)view.findViewById(R.id.input_email);
password=(EditText)view.findViewById(R.id.input_password);
// get the show/hide password Checkbox
mCbShowPwd = (CheckBox)view.findViewById(R.id.cbShowPwd);
mCbShowPwd.setOnCheckedChangeListener(TabAdvertiser.this);
return view;
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// checkbox status is changed from uncheck to checked.
if (!isChecked) {
// show password
password.setTransformationMethod(PasswordTransformationMethod.getInstance());
} else {
// hide password
password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
}
}