如何使用此代码修饰符将复选框值存储到共享首选项中?

时间:2016-05-14 15:11:21

标签: android checkbox sharedpreferences

我在Android应用程序开发人员的初学者...花了将近两天的修饰符和搜索解决方案这种复选框值存储到共享偏好...但失败了。 gt任何其他方法来重写此编码,因此复选框值可以存储在共享首选项中。希望专家可以帮忙...

CheckBox blockAll_cb;
BroadcastReceiver CallBlocker;
TelephonyManager telephonyManager;
ITelephony telephonyService;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setting);

    initviews();
    blockAll_cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            CallBlocker =new BroadcastReceiver()
            {
                @Override
                public void onReceive(Context context, Intent intent) {
                    telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
                    Class c = null;
                    try {
                        c = Class.forName(telephonyManager.getClass().getName());
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    Method m = null;
                    try {
                        m = c.getDeclaredMethod("getITelephony");
                    } catch (SecurityException e) {
                        e.printStackTrace();
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    }
                    m.setAccessible(true);
                    try {
                        telephonyService = (ITelephony)m.invoke(telephonyManager);
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                    telephonyManager.listen(callBlockListener, PhoneStateListener.LISTEN_CALL_STATE);
                }
                PhoneStateListener callBlockListener = new PhoneStateListener()
                {
                    public void onCallStateChanged(int state, String incomingNumber)
                    {
                        if(state==TelephonyManager.CALL_STATE_RINGING)
                        {
                            if(blockAll_cb.isChecked())
                            {
                                try {
                                    telephonyService.endCall();
                                } catch (RemoteException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                };
            };//BroadcastReceiver
            IntentFilter filter= new IntentFilter("android.intent.action.PHONE_STATE");
            registerReceiver(CallBlocker, filter);
        }
    });

}
public void initviews()
{
    blockAll_cb=(CheckBox)findViewById(R.id.cbBlockAll);
    //blockcontacts_cb=(CheckBox)findViewById(R.id.cbBlockContacts);
}
@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    if (CallBlocker != null)
    {
        unregisterReceiver(CallBlocker);
        CallBlocker = null;
    }
}

}

2 个答案:

答案 0 :(得分:0)

  public boolean getFromSP(String key){
        SharedPreferences preferences = ctx.getSharedPreferences("Contacts", android.content.Context.MODE_PRIVATE);
        return preferences.getBoolean(key, false);
        }
  private void saveInSp(String key,boolean value){
        SharedPreferences preferences = ctx.getSharedPreferences("Contacts", android.content.Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
        }
在您的视图中

写如下所示

 holder.checkBox.setOnClickListener(new View.OnClickListener() {

               public void onClick(View v) {
                if(((CheckBox)v).isChecked())
                { 
                    saveInSp("check"+position,true);
                    Log.i("pos", ""+position);   

                }       
                else
                {
                    saveInSp("check"+position,false);
                }

在加载listview时检索已选中复选框的状态,请在您需要的方法中使用它 -

 holder.checkBox.setChecked(getFromSP("check"+position));  

答案 1 :(得分:0)

 SharedPreferences preferences =   PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = preferences.edit();
    // value to store
     editor.putBoolean(Value, key); 
    editor.commit();

在想要存储值的地方使用它,以便稍后检索它