【问题标题】:OnCheckedChanged not working in Fragment AndroidOnCheckedChanged 在 Fragment Android 中不起作用
【发布时间】:2015-03-24 10:10:45
【问题描述】:

嗨,在片段中使用 OnCheckedChanged 获取复选框的值,但它不起作用,就像我在检查 CheckBox 时没有调用该方法一样。下面是它的实现代码。

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;
    }
}

}

【问题讨论】:

标签: android performance android-fragments android-xml android-checkbox


【解决方案1】:

你没有在你的代码中映射你的复选框..尝试这样做..

Checkbox mCb=(Checkbox)profView.findViewById(R.id.cbDefault);
mCb.setOnCheckedChangeListener(this);

必须实现接口OnCheckedChangeListener

【讨论】:

  • 收到以下错误。 CompoundButton 类型中的方法 setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener) 不适用于参数(Activity)
【解决方案2】:

将您的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;
}

【讨论】:

  • 我认为 setOnCheckedChangeListener 不应使用 getActivity 调用,而应使用 ProfileFrag.this 调用,因为正是此类实现了 OnCheckedChangeListener(而不是父活动)
  • @GaëtanMaisse 感谢您的纠正,我已经更新了答案。
【解决方案3】:

对于片段首先我们需要实现 android.widget.CompoundButton.OnCheckedChangeListener 看这个例子

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());
    }
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    • 2023-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多