如何让Android偏好工作?

时间:2011-01-22 11:34:44

标签: android checkbox android-activity radio-button preferences

对不起这个家伙。这必须像我不得不单独询问这个项目的第三个问题。 =(

我希望能够使用首选项活动中的复选框切换单选按钮的可见性。

我的代码中没有错误,两个活动都运行良好,但选中或取消选中复选框后,可见性没有变化。

这是我使用radiobuttons的活动

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import com.medialets.android.analytics.MMAnalyticsManager;

public class Temperature extends Activity {
MMAnalyticsManager mManager;
private EditText text;

public void onStart(Bundle savedInstanceState){
    super.onStart();
    getPrefs();
}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.temperature);
        text = (EditText)findViewById(R.id.EditText01);

mManager = MMAnalyticsManager.sharedInstance(this);
mManager.start("codeHere", "1.0",
    false);
}

@Override
public void onPause() {
super.onPause();

mManager.pause();
}

@Override
public void onResume() {
super.onResume();

mManager.resume();
}

@Override
public void onDestroy() {
super.onDestroy();
mManager.stop();
}






    public void myClickHandler(View view){
        switch (view.getId()) {
        case R.id.Button01:
            RadioButton celsiustokelvinButton = (RadioButton) findViewById (R.id.RadioButton05);
            RadioButton celsiustofarenheitButton = (RadioButton) findViewById(R.id.RadioButton01);
            RadioButton farenheittocelsiusButton = (RadioButton) findViewById(R.id.RadioButton02);
            RadioButton farenheittokelvinButton = (RadioButton) findViewById(R.id.RadioButton06);
            RadioButton kelvintocelsiusButton = (RadioButton) findViewById (R.id.RadioButton03);
            RadioButton kelvintofarenheitButton=(RadioButton) findViewById (R.id.RadioButton04);
            float inputValue = Float.parseFloat(text.getText().toString());

              if (celsiustofarenheitButton.isChecked())  {
                text.setText(String
                        .valueOf(convertCelsiusToFarenheit(inputValue)));
            } if (farenheittocelsiusButton.isChecked()) {
                text.setText(String
                        .valueOf(convertFarenheitToCelsius(inputValue)));
            } if (celsiustokelvinButton.isChecked()) {
                text.setText(String
                        .valueOf(convertCelsiusToKelvin(inputValue)));
            } if (farenheittokelvinButton.isChecked()) {
                text.setText(String
                        .valueOf(convertFarenheitToKelvin(inputValue)));
            } if (kelvintocelsiusButton.isChecked()) {
                text.setText(String
                        .valueOf(convertKelvinToCelsius(inputValue)));
            } if (kelvintofarenheitButton.isChecked()) {
                text.setText(String
                        .valueOf(convertKelvinToFarenheit(inputValue)));
            }
            break;


        }}

    private float convertFarenheitToCelsius(float fahrenheit){
        return ((fahrenheit - 32) * 5 / 9);
    }
    private float convertCelsiusToFarenheit(float celsius){
        return ((celsius * 9) / 5) + 32;       
    }
    private double convertCelsiusToKelvin(float celsius){
        return celsius + 273.15;
    }
    private double convertFarenheitToKelvin(float farenheit){
        return ((farenheit-32)/(1.8));
    }
    private double convertKelvinToCelsius(float kelvin){
        return kelvin-273.1;
    }
    private double convertKelvinToFarenheit(float kelvin){
        return kelvin*1.8-459.67;
    }
    boolean CheckboxPreference;


    private void getPrefs() {
            // Get the xml/preferences.xml preferences
            SharedPreferences prefs = PreferenceManager
                            .getDefaultSharedPreferences(getBaseContext());
            CheckboxPreference = prefs.getBoolean("checkboxPref", true);
}

}

这是我的偏好活动

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.preferences);
    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();

            CheckBox();

            return true;




        }
        private 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(View.VISIBLE);
                    } else {
                        btn01.setVisibility(View.GONE);
                    }
                }
            });
        };
    });

}}

很抱歉,还有其他内容,例如

中的分析插件

任何人都知道为什么这不起作用

(只是旁注。当看到logcat时,单击复选框返回没有结果)

编辑:好的我刚刚按照建议修改了我的代码,但我担心首选项仍然不会影响任何内容。有人有什么想法? =(

1 个答案:

答案 0 :(得分:0)

它不起作用,因为可见性值应使用(reference)

 0 = VISIBLE
 4 = INVISIBLE
 8 = GONE