我已经试着在我的应用程序中添加一些首选项几个星期了
我真的只需要复选框功能。
我试图通过选中复选框来控制单个单选按钮的可见性
这是我的首选项.java

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.Preference.OnPreferenceChangeListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.view.View;


public class Preferences extends PreferenceActivity {

private RadioButton btn01;


@Override
protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       addPreferencesFromResource(R.xml.prefs);
       btn01 = (RadioButton)findViewById(R.id.RadioButton01);

       Preference customPref = (Preference) findPreference("customPref");

                      customPref.setOnPreferenceClickListener(new OnPreferenceClickListener(){

                          public boolean onPreferenceClick(Preference preference) {
                          Toast.makeText(getBaseContext(),
                                                      "The Custom Preference Has Been Clicked",
                                                      Toast.LENGTH_LONG).show();
                          SharedPreferences customSharedPreference = getSharedPreferences(
                                           "myCutomSharedPrefs", Activity.MODE_PRIVATE);
                          SharedPreferences.Editor editor = customSharedPreference
                                           .edit();
                          editor.putString("myCustomPref",
                                           "The preference has been clicked");
                          editor.commit();
                          return true;}

                          public void CheckBox() {

                          final CheckBox ThisCheckBox = (CheckBox) findViewById (R.id.checkboxPref);{
                          ThisCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){

                              public boolean OnCheckedChange (CompoundButton compoundButton) {
                          if (ThisCheckBox.isChecked()){

                          {
                          btn01.setVisibility(0);
                          }{

                          btn01.setVisibility(2);

                          }}
}


;
});


};}});}}

无论如何,我很确定这是完全错误的,我在这条线上得到了一个错误
此复选框.setOnCheckedChangeListener(新的onCheckedChangeListener(){
知道为什么吗?我认为这与我调用上面这一行的方式有关,但是无论我使用final、boolean还是void,它仍然会产生错误
这是错误信息
类型new compoundbutton.oncheckedchangeletener(){}必须实现继承的抽象方法compoundbutton.oncheckedchangeletener.oncheckedchanged(compoundbutton,boolean)
所以我想我要问的是
我做的选择对吗
如何正确执行if-else命令??

最佳答案

我认为您试图重写的方法的签名不正确。替换为:

public boolean OnCheckedChange (CompoundButton compoundButton) {

用这个:
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

编辑
好的,这里有一个尝试来修复整个类。备份当前代码,然后选择下面的所有代码并复制它。选择类中的所有代码并将其替换为:
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class Preferences extends PreferenceActivity {

    private RadioButton btn01;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);
        btn01 = (RadioButton)findViewById(R.id.RadioButton01);
        Preference customPref = (Preference) findPreference("customPref");

        customPref.setOnPreferenceClickListener(new OnPreferenceClickListener(){

            public boolean onPreferenceClick(Preference preference) {
                Toast.makeText(getBaseContext(),"The Custom Preference Has Been Clicked",Toast.LENGTH_LONG).show();
                SharedPreferences customSharedPreference = getSharedPreferences("myCutomSharedPrefs", Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor = customSharedPreference.edit();
                editor.putString("myCustomPref","The preference has been clicked");
                editor.commit();
                return true;
            }

            public void CheckBox() {
                final CheckBox ThisCheckBox = (CheckBox) findViewById (R.id.checkboxPref);
                ThisCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton,boolean test) {
                        if (ThisCheckBox.isChecked()){
                            btn01.setVisibility(0);
                        } else {
                            btn01.setVisibility(2);
                        }
                    }
                });
            };
        });
    }
}

上面的代码在eclipse中编译时对我来说没有问题

10-08 18:38