0

所以几乎我有一个带有复选框的 sharedPreferences 设置,当该框被选中时,它会创建一个动态 ImageButton。我的问题是,当我检查它时,它似乎没有问题,但是当我取消它时,它仍然存在?

我的源代码:

package org.iimed.www;    

import java.util.ArrayList;

import android.R.string;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import android.view.View;  

import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Toast;


import android.view.View.OnClickListener;
public class Sundayopen extends Activity implements OnClickListener {  

int i = 0;    

public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.sundayopen);
        super.onResume();
        RelativeLayout    ll = (RelativeLayout)findViewById(R.id.sundayopen);
        ImageButton ab = new ImageButton(this);
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(this);{



                  if(prefs.getBoolean("checkBox",true)) {
                      ll.addView(ab);
                      ab.setImageResource(R.drawable.adaba);
                      ab.setOnClickListener(this);
                      ab.setBackgroundColor(View.GONE);
                      }else{
                if(prefs.getBoolean("checkbox", false)){
                    ll.removeView(ab);

                }}}}

    // TODO Auto-generated method stub

                @Override
                protected void onPause(){
                    super.onPause();
                      RelativeLayout    ll = (RelativeLayout)findViewById(R.id.sundayopen);
                        ImageButton ab = new ImageButton(this);
                    SharedPreferences prefs = PreferenceManager
                            .getDefaultSharedPreferences(this);{ 

                              if(prefs.getBoolean("checkbox", true)) {
                                  ll.addView(ab);
                                  ab.setImageResource(R.drawable.adaba);
                                  ab.setOnClickListener(this);
                                  ab.setBackgroundColor(View.GONE);
                                  }else{
                            if(prefs.getBoolean("checkbox", false)){
                                ll.addView(ab);
                                ab.setVisibility(View.GONE);
                            }}}
                }
                            @Override
                            protected void onResume(){
                                super.onResume();
                                  RelativeLayout    ll = (RelativeLayout)findViewById(R.id.sundayopen);
                                    ImageButton ab = new ImageButton(this);
                                    SharedPreferences prefs = PreferenceManager
                                            .getDefaultSharedPreferences(this);{ 

                                              if(prefs.getBoolean("checkbox", true)) {
                                                  ll.addView(ab);
                                                  ab.setImageResource(R.drawable.adaba);
                                                  ab.setOnClickListener(this);
                                                  ab.setBackgroundColor(View.GONE);
                                                  }else{
                                            if(prefs.getBoolean("checkbox", false)){
                                                ll.addView(ab);
                                                ab.setVisibility(View.GONE);

                            }

   ImageButton hb1 = (ImageButton) findViewById(R.id.homebutton1);

   ImageButton sbc = (ImageButton) findViewById(R.id.satlidopen);
   ImageButton abb = (ImageButton) findViewById(R.id.abbutton);
   sbc.setOnClickListener(this);
   hb1.setOnClickListener(this);
   abb.setOnClickListener(this);


                }}

}                   
public void onClick(View v){
             switch (v.getId()) {
             case R.id.homebutton1:
                startActivity(new Intent(this, MainActivity.class));
                 break;
             case R.id.satlidopen:
                 MediaPlayer media = MediaPlayer.create(Sundayopen.this, R.raw.openlid);
                 media.start();
                startActivity(new Intent(this,Iimedja.class));
                break;
             case R.id.abbutton:

                    startActivity(new Intent(this, Listact.class));
             break;

             }
         }

}

编辑

package org.iimed.www;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TableLayout.LayoutParams;

public class Prefsdemo extends Activity {
TextView textView;






@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.penicillin);
   ImageButton addmed = (ImageButton)findViewById(R.id.addmed);



   View.OnClickListener listener = new View.OnClickListener() {



   @Override
   public void onClick(View v) {
   switch (v.getId()) {
   case R.id.addmed:

      Intent intent = new Intent(Prefsdemo.this,
      PrefsActivity.class);
      startActivity(intent);

   default:
     break;
   }
   }


   };

 addmed.setOnClickListener(listener);
}

编辑

package org.iimed.www;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageButton;

public class PrefsActivity extends PreferenceActivity{


@Override
protected void onCreate(Bundle SavedInstanceState) {
   super.onCreate(SavedInstanceState);
   addPreferencesFromResource(R.xml.prefs);

}}
4

1 回答 1

0

取消选中时,检查是否在共享首选项中将复选框值设置为false

于 2013-11-05T06:33:26.703 回答