【问题标题】:Get preferences of a checkbox to display view in seperate activity获取复选框的首选项以在单独的活动中显示视图
【发布时间】:2013-11-05 06:06:51
【问题描述】:

当复选框被选中时,我几乎有一个带有复选框的 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);

}}

【问题讨论】:

  • 您的 CheckBox 及其状态变化监听器在哪里?
  • 复选框位于共享首选项中,首选项屏幕我的答案底部的活动称为它

标签: android dynamic checkbox sharedpreferences


【解决方案1】:

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

【讨论】:

  • 问题是 sharedpreference 复选框只是一个首选项屏幕 XML,一个按钮的 OnClick 打开了共享首选项活动,但没有(R.id)供我参考,我也看不到我可以在哪里通过共享首选项复选框设置状态更改侦听器?,我对这一切都很陌生,所以感谢您的耐心。
  • 请先尝试了解如何使用 CheckBox 并处理其侦听器,然后您可以根据需要自定义代码。
猜你喜欢
  • 2013-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多