If and else in android settings

I’ve been trying to add some settings to my application for a couple of weeks now.

I really need the checkbox function.

I am trying to control the visibility of a single checkbox check box

Here is my Preferences.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);

                          }}                                    
}                            


;
});


};}});}}

In any case, I am sure that this is completely wrong, and I get an error on this line

ThisCheckBox.setOnCheckedChangeListener (new OnCheckedChangeListener () {

any idea why? I think this is due to the way I named the line above, but whether I use a finite, logical, or void that still generates errors.

this error message

Type new CompoundButton.OnCheckedChangeListener () {} should implement the inherited abstract method CompoundButton.OnCheckedChangeListener.onCheckedChanged (CompoundButton, boolean)

So, I think I ask

Preferences Right If else?

+3
2

, , , . :

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

+2

CheckBoxPreference :

http://www.kaloer.com/android-preferences

, ( , ), if/else "else", (, ). ok:

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;

PreferenceActivity {    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.setOnPreferenceChangeListener(new OnPreferenceChangeListener()
      {
          @Override
          public boolean onPreferenceChange(Preference preference, Object object)
          {
              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 isChecked)
          {
              if (ThisCheckBox.isChecked())
              {
                  btn01.setVisibility(0);           
              }
              else
              {
                 btn01.setVisibility(2);

              }                                    
         }
      });
  } 

}

0

Source: https://habr.com/ru/post/1783393/


All Articles